-
Notifications
You must be signed in to change notification settings - Fork 2
/
Hashs.cs
88 lines (78 loc) · 2.57 KB
/
Hashs.cs
1
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Security.Cryptography;
using System.Text;
namespace MSC.Hash
{
public enum HashType
{
MD5,
SHA1,
SHA256,
SHA512,
Base64
}
public class Hashs
{
public static string MD5(string input)
{
using (MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString().ToLower();
}
}
public static string SHA1(string input)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
sb.Append(b.ToString("X2"));
}
return sb.ToString().ToLower();
}
}
public static string SHA256(string input)
{
using (SHA256Managed sha1 = new SHA256Managed())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
sb.Append(b.ToString("X2"));
}
return sb.ToString().ToLower();
}
}
public static string SHA512(string input)
{
using (SHA512Managed sha1 = new SHA512Managed())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
sb.Append(b.ToString("X2"));
}
return sb.ToString().ToLower();
}
}
public static string Base64ToString(string input)
{
return Encoding.UTF8.GetString(Convert.FromBase64String(input));
}
public static string StringToBase64(string input)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
}
}
}