Skip to content

Commit 6231ba8

Browse files
committed
fix
1 parent 1758c8d commit 6231ba8

File tree

1 file changed

+36
-0
lines changed
  • csharp/ToolGood.Algorithm/Internals

1 file changed

+36
-0
lines changed

csharp/ToolGood.Algorithm/Internals/Hash.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ public static string GetCrc32String(byte[] buffer)
1212
Crc32Hash crc32 = new Crc32Hash();
1313
crc32.Append(buffer);
1414
byte[] retVal = crc32.Finish();
15+
#if NETSTANDARD2_1
1516
return BitConverter.ToString(retVal).Replace("-", "");
17+
#else
18+
return Convert.ToHexString(retVal);
19+
#endif
1620
}
1721

1822
private class Crc32Hash
@@ -110,7 +114,11 @@ public static string GetMd5String(byte[] buffer)
110114
System.Security.Cryptography.MD5 md5 = MD5.Create();
111115
byte[] retVal = md5.ComputeHash(buffer);
112116
md5.Dispose();
117+
#if NETSTANDARD2_1
113118
return BitConverter.ToString(retVal).Replace("-", "");
119+
#else
120+
return Convert.ToHexString(retVal);
121+
#endif
114122
#endif
115123
}
116124

@@ -123,7 +131,11 @@ public static string GetSha1String(byte[] buffer)
123131
SHA1 sha512 = SHA1.Create();
124132
byte[] retVal = sha512.ComputeHash(buffer); //计算指定Stream 对象的哈希值
125133
sha512.Dispose();
134+
#if NETSTANDARD2_1
126135
return BitConverter.ToString(retVal).Replace("-", "");
136+
#else
137+
return Convert.ToHexString(retVal);
138+
#endif
127139
}
128140

129141
#endregion SHA1
@@ -135,7 +147,11 @@ public static string GetSha256String(byte[] buffer)
135147
SHA256 sha512 = SHA256.Create();
136148
byte[] retVal = sha512.ComputeHash(buffer); //计算指定Stream 对象的哈希值
137149
sha512.Dispose();
150+
#if NETSTANDARD2_1
138151
return BitConverter.ToString(retVal).Replace("-", "");
152+
#else
153+
return Convert.ToHexString(retVal);
154+
#endif
139155
}
140156

141157
#endregion SHA256
@@ -147,7 +163,11 @@ public static string GetSha512String(byte[] buffer)
147163
SHA512 sha512 = SHA512.Create();
148164
byte[] retVal = sha512.ComputeHash(buffer); //计算指定Stream 对象的哈希值
149165
sha512.Dispose();
166+
#if NETSTANDARD2_1
150167
return BitConverter.ToString(retVal).Replace("-", "");
168+
#else
169+
return Convert.ToHexString(retVal);
170+
#endif
151171
}
152172

153173
#endregion SHA512
@@ -163,7 +183,11 @@ public static string GetHmacMd5String(byte[] buffer, string secret)
163183
byte[] keyByte = System.Text.Encoding.UTF8.GetBytes(secret ?? "");
164184
using (var hmacsha256 = new HMACMD5(keyByte)) {
165185
byte[] hashmessage = hmacsha256.ComputeHash(buffer);
186+
#if NETSTANDARD2_1
166187
return BitConverter.ToString(hashmessage).Replace("-", "");
188+
#else
189+
return Convert.ToHexString(hashmessage);
190+
#endif
167191
}
168192
#endif
169193
}
@@ -177,7 +201,11 @@ public static string GetHmacSha1String(byte[] buffer, string secret)
177201
byte[] keyByte = System.Text.Encoding.UTF8.GetBytes(secret ?? "");
178202
using (var hmacsha256 = new HMACSHA1(keyByte)) {
179203
byte[] hashmessage = hmacsha256.ComputeHash(buffer);
204+
#if NETSTANDARD2_1
180205
return BitConverter.ToString(hashmessage).Replace("-", "");
206+
#else
207+
return Convert.ToHexString(hashmessage);
208+
#endif
181209
}
182210
}
183211

@@ -190,7 +218,11 @@ public static string GetHmacSha256String(byte[] buffer, string secret)
190218
byte[] keyByte = System.Text.Encoding.UTF8.GetBytes(secret ?? "");
191219
using (var hmacsha256 = new HMACSHA256(keyByte)) {
192220
byte[] hashmessage = hmacsha256.ComputeHash(buffer);
221+
#if NETSTANDARD2_1
193222
return BitConverter.ToString(hashmessage).Replace("-", "");
223+
#else
224+
return Convert.ToHexString(hashmessage);
225+
#endif
194226
}
195227
}
196228

@@ -203,7 +235,11 @@ public static string GetHmacSha512String(byte[] buffer, string secret)
203235
byte[] keyByte = System.Text.Encoding.UTF8.GetBytes(secret ?? "");
204236
using (var hmacsha256 = new HMACSHA512(keyByte)) {
205237
byte[] hashmessage = hmacsha256.ComputeHash(buffer);
238+
#if NETSTANDARD2_1
206239
return BitConverter.ToString(hashmessage).Replace("-", "");
240+
#else
241+
return Convert.ToHexString(hashmessage);
242+
#endif
207243
}
208244
}
209245

0 commit comments

Comments
 (0)