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

6
namespace trips7
{8
static class CryptClass9
{10
public enum HashType : int11
{12
SHA1,13
SHA256,14
SHA384,15
SHA512,16
MD5,17
RIPEMD16018
}19

20

21
public static string FromString(string input, HashType hashtype)22
{23
Byte[] clearBytes;24
Byte[] hashedBytes;25
string output = String.Empty;26

27
switch (hashtype)28
{29
case HashType.RIPEMD160:30
clearBytes = new UTF8Encoding().GetBytes(input);31
RIPEMD160 myRIPEMD160 = RIPEMD160Managed.Create();32
hashedBytes = myRIPEMD160.ComputeHash(clearBytes);33
output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();34
break;35
case HashType.MD5:36
clearBytes = new UTF8Encoding().GetBytes(input);37
hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);38
output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();39
break;40
case HashType.SHA1:41
clearBytes = Encoding.UTF8.GetBytes(input);42
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();43
sha1.ComputeHash(clearBytes);44
hashedBytes = sha1.Hash;45
sha1.Clear();46
output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();47
break;48
case HashType.SHA256:49
clearBytes = Encoding.UTF8.GetBytes(input);50
SHA256 sha256 = new SHA256Managed();51
sha256.ComputeHash(clearBytes);52
hashedBytes = sha256.Hash;53
sha256.Clear();54
output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();55
break;56
case HashType.SHA384:57
clearBytes = Encoding.UTF8.GetBytes(input);58
SHA384 sha384 = new SHA384Managed();59
sha384.ComputeHash(clearBytes);60
hashedBytes = sha384.Hash;61
sha384.Clear();62
output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();63
break;64
case HashType.SHA512:65
clearBytes = Encoding.UTF8.GetBytes(input);66
SHA512 sha512 = new SHA512Managed();67
sha512.ComputeHash(clearBytes);68
hashedBytes = sha512.Hash;69
sha512.Clear();70
output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();71
break;72
}73
return output;74
}75

76
}77
}78

//End


浙公网安备 33010602011771号