1- using System ;
1+ using System . Security . Cryptography ;
22
33namespace Renci . SshNet . Abstractions
44{
55 internal static class CryptoAbstraction
66 {
7- private static readonly System . Security . Cryptography . RandomNumberGenerator Randomizer = CreateRandomNumberGenerator ( ) ;
7+ private static readonly RandomNumberGenerator Randomizer = RandomNumberGenerator . Create ( ) ;
88
99 /// <summary>
1010 /// Generates a <see cref="byte"/> array of the specified length, and fills it with a
@@ -14,51 +14,68 @@ internal static class CryptoAbstraction
1414 public static byte [ ] GenerateRandom ( int length )
1515 {
1616 var random = new byte [ length ] ;
17- GenerateRandom ( random ) ;
17+ Randomizer . GetBytes ( random ) ;
1818 return random ;
1919 }
2020
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 )
4022 {
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
4231 }
4332
44- public static System . Security . Cryptography . SHA1 CreateSHA1 ( )
33+ public static byte [ ] HashSHA1 ( byte [ ] source )
4534 {
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
4743 }
4844
49- public static System . Security . Cryptography . SHA256 CreateSHA256 ( )
45+ public static byte [ ] HashSHA256 ( byte [ ] source )
5046 {
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
5255 }
5356
54- public static System . Security . Cryptography . SHA384 CreateSHA384 ( )
57+ public static byte [ ] HashSHA384 ( byte [ ] source )
5558 {
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
5767 }
5868
59- public static System . Security . Cryptography . SHA512 CreateSHA512 ( )
69+ public static byte [ ] HashSHA512 ( byte [ ] source )
6070 {
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
6279 }
6380 }
6481}
0 commit comments