Skip to content

Commit 1eabf1c

Browse files
authored
MD5 hash result's length support 16 and 32.
1 parent 3bda7b6 commit 1eabf1c

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/NETCore.Encrypt/EncryptProvider.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,30 +423,35 @@ public static RSAKey CreateRsaKey(RsaSize rsaSize = RsaSize.R2048)
423423
#endregion
424424

425425
#region MD5
426-
427426
/// <summary>
428427
/// MD5 hash
429428
/// </summary>
430-
/// <param name="srcString">The string to be encrypted</param>
429+
/// <param name="srcString">The string to be encrypted.</param>
430+
/// <param name="length">The length of hash result , default value is <see cref="MD5Length.L32"/>.</param>
431431
/// <returns></returns>
432-
public static string Md5(string srcString)
432+
public static string Md5(string srcString, MD5Length length = MD5Length.L32)
433433
{
434434
Check.Argument.IsNotEmpty(srcString, nameof(srcString));
435435

436+
string str_md5_out = string.Empty;
436437
using (MD5 md5 = MD5.Create())
437438
{
438439
byte[] bytes_md5_in = Encoding.UTF8.GetBytes(srcString);
439440
byte[] bytes_md5_out = md5.ComputeHash(bytes_md5_in);
440-
string str_md5_out = BitConverter.ToString(bytes_md5_out);
441+
442+
str_md5_out = length == MD5Length.L32
443+
? BitConverter.ToString(bytes_md5_out)
444+
: BitConverter.ToString(bytes_md5_out, 4, 8);
445+
441446
str_md5_out = str_md5_out.Replace("-", "");
442447
return str_md5_out;
443448
}
444-
}
449+
}
445450
#endregion
446451

447452
#region HMACMD5
448453
/// <summary>
449-
/// MD5 hash
454+
/// HMACMD5 hash
450455
/// </summary>
451456
/// <param name="srcString">The string to be encrypted</param>
452457
/// <param name="key">encrypte key</param>

0 commit comments

Comments
 (0)