(转)封装了MD5,SHA1等加密算法的类
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![](/Images/OutliningIndicators/InBlock.gif)
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
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
72
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
73
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
http://www.spdevelop.com/forum/Answer.aspx?QuestionId=47