-
Notifications
You must be signed in to change notification settings - Fork 3
/
SHA1Helper.cs
120 lines (116 loc) · 4.21 KB
/
SHA1Helper.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using Utils.Helper.TXT;
namespace Utils.Helper.Encryption
{
/// <summary>
/// SHA1 加密帮助类
/// 创建日期:2017年06月16日
/// </summary>
public class SHA1Helper
{
/// <summary>
/// SHA1 加密(40位小写)
/// </summary>
/// <param name="strPlaintext">明文</param>
/// <returns>SHA1 密文(40位小写)</returns>
public static string SHA1Encrypt_40Lower(string strPlaintext)
{
try
{
SHA1 sha1Crypto = new SHA1CryptoServiceProvider();
byte[] bytes_sha1_in = Encoding.Default.GetBytes(strPlaintext);
byte[] bytes_sha1_out = sha1Crypto.ComputeHash(bytes_sha1_in);
string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
str_sha1_out = str_sha1_out.Replace("-", "");
str_sha1_out = str_sha1_out.ToLower();
return str_sha1_out;
}
catch (Exception ex)
{
TXTHelper.Logs(ex.ToString());
return string.Empty;
}
}
/// <summary>
/// SHA1 加密(40位大写)
/// </summary>
/// <param name="strPlaintext">明文</param>
/// <returns>SHA1 密文(40位大写)</returns>
public static string SHA1Encrypt_40Upper(string strPlaintext)
{
try
{
SHA1 sha1Crypto = new SHA1CryptoServiceProvider();
byte[] bytes_sha1_in = Encoding.Default.GetBytes(strPlaintext);
byte[] bytes_sha1_out = sha1Crypto.ComputeHash(bytes_sha1_in);
string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
str_sha1_out = str_sha1_out.Replace("-", "");
str_sha1_out = str_sha1_out.ToUpper();
return str_sha1_out;
}
catch (Exception ex)
{
TXTHelper.Logs(ex.ToString());
return string.Empty;
}
}
/// <summary>
/// 获取文件 SHA1 值(40位小写)
/// </summary>
/// <param name="strFilePath">文件路径</param>
/// <returns>文件 SHA1 值(40位小写)</returns>
public static string FileSHA1Encrypt_40Lower(string strFilePath)
{
try
{
FileStream fileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] byteHash = sha1.ComputeHash(fileStream);
fileStream.Close();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < byteHash.Length; i++)
{
stringBuilder.Append(byteHash[i].ToString("x2"));
}
return stringBuilder.ToString();
}
catch (Exception ex)
{
TXTHelper.Logs(ex.ToString());
return string.Empty;
}
}
/// <summary>
/// 获取文件 SHA1 值(40位大写)
/// </summary>
/// <param name="strFilePath">文件路径</param>
/// <returns>文件 SHA1 值(40位大写)</returns>
public static string FileSHA1Encrypt_40Upper(string strFilePath)
{
try
{
FileStream fileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] byteHash = sha1.ComputeHash(fileStream);
fileStream.Close();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < byteHash.Length; i++)
{
stringBuilder.Append(byteHash[i].ToString("X2"));
}
return stringBuilder.ToString();
}
catch (Exception ex)
{
TXTHelper.Logs(ex.ToString());
return string.Empty;
}
}
}
}