1
- using System ;
1
+ using System . Security . Cryptography ;
2
2
3
3
namespace Renci . SshNet . Abstractions
4
4
{
5
5
internal static class CryptoAbstraction
6
6
{
7
- private static readonly System . Security . Cryptography . RandomNumberGenerator Randomizer = CreateRandomNumberGenerator ( ) ;
7
+ private static readonly RandomNumberGenerator Randomizer = RandomNumberGenerator . Create ( ) ;
8
8
9
9
/// <summary>
10
10
/// Generates a <see cref="byte"/> array of the specified length, and fills it with a
@@ -14,51 +14,68 @@ internal static class CryptoAbstraction
14
14
public static byte [ ] GenerateRandom ( int length )
15
15
{
16
16
var random = new byte [ length ] ;
17
- GenerateRandom ( random ) ;
17
+ Randomizer . GetBytes ( random ) ;
18
18
return random ;
19
19
}
20
20
21
- /// <summary>
22
- /// Fills an array of bytes with a cryptographically strong random sequence of values.
23
- /// </summary>
24
- /// <param name="data">The array to fill with cryptographically strong random bytes.</param>
25
- /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception>
26
- /// <remarks>
27
- /// The length of the byte array determines how many random bytes are produced.
28
- /// </remarks>
29
- public static void GenerateRandom ( byte [ ] data )
30
- {
31
- Randomizer . GetBytes ( data ) ;
32
- }
33
-
34
- public static System . Security . Cryptography . RandomNumberGenerator CreateRandomNumberGenerator ( )
35
- {
36
- return System . Security . Cryptography . RandomNumberGenerator . Create ( ) ;
37
- }
38
-
39
- public static System . Security . Cryptography . MD5 CreateMD5 ( )
21
+ public static byte [ ] HashMD5 ( byte [ ] source )
40
22
{
41
- return System . Security . Cryptography . MD5 . Create ( ) ;
23
+ #if NET
24
+ return MD5 . HashData ( source ) ;
25
+ #else
26
+ using ( var md5 = MD5 . Create ( ) )
27
+ {
28
+ return md5 . ComputeHash ( source ) ;
29
+ }
30
+ #endif
42
31
}
43
32
44
- public static System . Security . Cryptography . SHA1 CreateSHA1 ( )
33
+ public static byte [ ] HashSHA1 ( byte [ ] source )
45
34
{
46
- return System . Security . Cryptography . SHA1 . Create ( ) ;
35
+ #if NET
36
+ return SHA1 . HashData ( source ) ;
37
+ #else
38
+ using ( var sha1 = SHA1 . Create ( ) )
39
+ {
40
+ return sha1 . ComputeHash ( source ) ;
41
+ }
42
+ #endif
47
43
}
48
44
49
- public static System . Security . Cryptography . SHA256 CreateSHA256 ( )
45
+ public static byte [ ] HashSHA256 ( byte [ ] source )
50
46
{
51
- return System . Security . Cryptography . SHA256 . Create ( ) ;
47
+ #if NET
48
+ return SHA256 . HashData ( source ) ;
49
+ #else
50
+ using ( var sha256 = SHA256 . Create ( ) )
51
+ {
52
+ return sha256 . ComputeHash ( source ) ;
53
+ }
54
+ #endif
52
55
}
53
56
54
- public static System . Security . Cryptography . SHA384 CreateSHA384 ( )
57
+ public static byte [ ] HashSHA384 ( byte [ ] source )
55
58
{
56
- return System . Security . Cryptography . SHA384 . Create ( ) ;
59
+ #if NET
60
+ return SHA384 . HashData ( source ) ;
61
+ #else
62
+ using ( var sha384 = SHA384 . Create ( ) )
63
+ {
64
+ return sha384 . ComputeHash ( source ) ;
65
+ }
66
+ #endif
57
67
}
58
68
59
- public static System . Security . Cryptography . SHA512 CreateSHA512 ( )
69
+ public static byte [ ] HashSHA512 ( byte [ ] source )
60
70
{
61
- return System . Security . Cryptography . SHA512 . Create ( ) ;
71
+ #if NET
72
+ return SHA512 . HashData ( source ) ;
73
+ #else
74
+ using ( var sha512 = SHA512 . Create ( ) )
75
+ {
76
+ return sha512 . ComputeHash ( source ) ;
77
+ }
78
+ #endif
62
79
}
63
80
}
64
81
}
0 commit comments