1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Security.Cryptography;
5
namespace trips
6

{
7
public static class CryptClass
8
{
9
public enum HashType : int
10
{
11
SHA1,
12
SHA256,
13
SHA384,
14
SHA512,
15
MD5,
16
RIPEMD160
17
}
18
public static string FromString(string input, HashType hashtype)
19
{
20
Byte[] clearBytes;
21
Byte[] hashedBytes;
22
string output = String.Empty;
23
24
switch (hashtype)
25
{
26
case HashType.RIPEMD160:
27
clearBytes = new UTF8Encoding().GetBytes(input);
28
RIPEMD160 myRIPEMD160 = RIPEMD160Managed.Create();
29
hashedBytes = myRIPEMD160.ComputeHash(clearBytes);
30
output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
31
break;
32
case HashType.MD5:
33
clearBytes = new UTF8Encoding().GetBytes(input);
34
hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
35
output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
36
break;
37
case HashType.SHA1:
38
clearBytes = Encoding.UTF8.GetBytes(input);
39
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
40
sha1.ComputeHash(clearBytes);
41
hashedBytes = sha1.Hash;
42
sha1.Clear();
43
output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
44
break;
45
case HashType.SHA256:
46
clearBytes = Encoding.UTF8.GetBytes(input);
47
SHA256 sha256 = new SHA256Managed();
48
sha256.ComputeHash(clearBytes);
49
hashedBytes = sha256.Hash;
50
sha256.Clear();
51
output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
52
break;
53
case HashType.SHA384:
54
clearBytes = Encoding.UTF8.GetBytes(input);
55
SHA384 sha384 = new SHA384Managed();
56
sha384.ComputeHash(clearBytes);
57
hashedBytes = sha384.Hash;
58
sha384.Clear();
59
output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
60
break;
61
case HashType.SHA512:
62
clearBytes = Encoding.UTF8.GetBytes(input);
63
SHA512 sha512 = new SHA512Managed();
64
sha512.ComputeHash(clearBytes);
65
hashedBytes = sha512.Hash;
66
sha512.Clear();
67
output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
68
break;
69
}
70
return output;
71
}
72
}
73
}
http://www.spdevelop.com/forum/Answer.aspx?QuestionId=47

2

3

4

5

6



7

8



9

10



11

12

13

14

15

16

17

18

19



20

21

22

23

24

25



26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

http://www.spdevelop.com/forum/Answer.aspx?QuestionId=47