File tree 4 files changed +37
-6
lines changed
test/NETCore.Encrypt.Tests
4 files changed +37
-6
lines changed Original file line number Diff line number Diff line change @@ -139,6 +139,13 @@ Install-Package NETCore.Encrypt -Version 2.0.2
139
139
140
140
```
141
141
142
+ ``` csharp
143
+
144
+ var srcString = " Md5 hash" ;
145
+ var hashed = EncryptProvider .Md5 (srcString , MD5Length .L16 );
146
+
147
+ ```
148
+
142
149
## SHA
143
150
144
151
- #### SHA1
Original file line number Diff line number Diff line change @@ -423,30 +423,35 @@ public static RSAKey CreateRsaKey(RsaSize rsaSize = RsaSize.R2048)
423
423
#endregion
424
424
425
425
#region MD5
426
-
427
426
/// <summary>
428
427
/// MD5 hash
429
428
/// </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>
431
431
/// <returns></returns>
432
- public static string Md5 ( string srcString )
432
+ public static string Md5 ( string srcString , MD5Length length = MD5Length . L32 )
433
433
{
434
434
Check . Argument . IsNotEmpty ( srcString , nameof ( srcString ) ) ;
435
435
436
+ string str_md5_out = string . Empty ;
436
437
using ( MD5 md5 = MD5 . Create ( ) )
437
438
{
438
439
byte [ ] bytes_md5_in = Encoding . UTF8 . GetBytes ( srcString ) ;
439
440
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
+
441
446
str_md5_out = str_md5_out . Replace ( "-" , "" ) ;
442
447
return str_md5_out ;
443
448
}
444
- }
449
+ }
445
450
#endregion
446
451
447
452
#region HMACMD5
448
453
/// <summary>
449
- /// MD5 hash
454
+ /// HMACMD5 hash
450
455
/// </summary>
451
456
/// <param name="srcString">The string to be encrypted</param>
452
457
/// <param name="key">encrypte key</param>
Original file line number Diff line number Diff line change
1
+ namespace NETCore . Encrypt
2
+ {
3
+ public enum MD5Length
4
+ {
5
+ L16 = 16 ,
6
+ L32 = 32
7
+ }
8
+ }
Original file line number Diff line number Diff line change @@ -25,5 +25,16 @@ public void MD5_Error_Test()
25
25
{
26
26
Assert . Throws < ArgumentException > ( ( ) => EncryptProvider . Md5 ( string . Empty ) ) ;
27
27
}
28
+
29
+ [ Fact ( DisplayName = "MD5 with length success test" ) ]
30
+ public void MD5_With_Length_Success_Test ( )
31
+ {
32
+ var srcString = "Md5 test" ;
33
+
34
+ var hashed = EncryptProvider . Md5 ( srcString , MD5Length . L16 ) ;
35
+
36
+ Assert . NotEmpty ( hashed ) ;
37
+ Assert . Equal ( "69c6ecadb32f5492" , hashed . ToLower ( ) ) ;
38
+ }
28
39
}
29
40
}
You can’t perform that action at this time.
0 commit comments