| | | 1 | | using System; |
| | | 2 | | using Renci.SshNet.Security.Cryptography; |
| | | 3 | | |
| | | 4 | | namespace Renci.SshNet.Abstractions |
| | | 5 | | { |
| | | 6 | | internal static class CryptoAbstraction |
| | | 7 | | { |
| | 4 | 8 | | private static readonly System.Security.Cryptography.RandomNumberGenerator Randomizer = CreateRandomNumberGenera |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Generates a <see cref="byte"/> array of the specified length, and fills it with a |
| | | 12 | | /// cryptographically strong random sequence of values. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="length">The length of the array generate.</param> |
| | | 15 | | public static byte[] GenerateRandom(int length) |
| | 1304 | 16 | | { |
| | 1304 | 17 | | var random = new byte[length]; |
| | 1304 | 18 | | GenerateRandom(random); |
| | 1304 | 19 | | return random; |
| | 1304 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Fills an array of bytes with a cryptographically strong random sequence of values. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="data">The array to fill with cryptographically strong random bytes.</param> |
| | | 26 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception> |
| | | 27 | | /// <remarks> |
| | | 28 | | /// The length of the byte array determines how many random bytes are produced. |
| | | 29 | | /// </remarks> |
| | | 30 | | public static void GenerateRandom(byte[] data) |
| | 60175 | 31 | | { |
| | 60175 | 32 | | Randomizer.GetBytes(data); |
| | 60175 | 33 | | } |
| | | 34 | | |
| | | 35 | | public static System.Security.Cryptography.RandomNumberGenerator CreateRandomNumberGenerator() |
| | 5 | 36 | | { |
| | 5 | 37 | | return System.Security.Cryptography.RandomNumberGenerator.Create(); |
| | 5 | 38 | | } |
| | | 39 | | |
| | | 40 | | public static System.Security.Cryptography.MD5 CreateMD5() |
| | 61 | 41 | | { |
| | | 42 | | #pragma warning disable CA5351 // Do not use broken cryptographic algorithms |
| | 61 | 43 | | return System.Security.Cryptography.MD5.Create(); |
| | | 44 | | #pragma warning restore CA5351 // Do not use broken cryptographic algorithms |
| | 61 | 45 | | } |
| | | 46 | | |
| | | 47 | | public static System.Security.Cryptography.SHA1 CreateSHA1() |
| | 88 | 48 | | { |
| | | 49 | | #pragma warning disable CA5350 // Do not use weak cryptographic algorithms |
| | 88 | 50 | | return System.Security.Cryptography.SHA1.Create(); |
| | | 51 | | #pragma warning restore CA5350 // Do not use weak cryptographic algorithms |
| | 88 | 52 | | } |
| | | 53 | | |
| | | 54 | | public static System.Security.Cryptography.SHA256 CreateSHA256() |
| | 9451 | 55 | | { |
| | 9451 | 56 | | return System.Security.Cryptography.SHA256.Create(); |
| | 9451 | 57 | | } |
| | | 58 | | |
| | | 59 | | public static System.Security.Cryptography.SHA384 CreateSHA384() |
| | 24 | 60 | | { |
| | 24 | 61 | | return System.Security.Cryptography.SHA384.Create(); |
| | 24 | 62 | | } |
| | | 63 | | |
| | | 64 | | public static System.Security.Cryptography.SHA512 CreateSHA512() |
| | 66 | 65 | | { |
| | 66 | 66 | | return System.Security.Cryptography.SHA512.Create(); |
| | 66 | 67 | | } |
| | | 68 | | |
| | | 69 | | #if FEATURE_HASH_RIPEMD160_CREATE || FEATURE_HASH_RIPEMD160_MANAGED |
| | | 70 | | public static System.Security.Cryptography.RIPEMD160 CreateRIPEMD160() |
| | 0 | 71 | | { |
| | | 72 | | #if FEATURE_HASH_RIPEMD160_CREATE |
| | | 73 | | #pragma warning disable CA5350 // Do not use weak cryptographic algorithms |
| | 0 | 74 | | return System.Security.Cryptography.RIPEMD160.Create(); |
| | | 75 | | #pragma warning restore CA5350 // Do not use weak cryptographic algorithms |
| | | 76 | | #else |
| | | 77 | | return new System.Security.Cryptography.RIPEMD160Managed(); |
| | | 78 | | #endif |
| | 0 | 79 | | } |
| | | 80 | | #else |
| | | 81 | | public static global::SshNet.Security.Cryptography.RIPEMD160 CreateRIPEMD160() |
| | 0 | 82 | | { |
| | 0 | 83 | | return new global::SshNet.Security.Cryptography.RIPEMD160(); |
| | 0 | 84 | | } |
| | | 85 | | #endif // FEATURE_HASH_RIPEMD160 |
| | | 86 | | |
| | | 87 | | public static System.Security.Cryptography.HMACMD5 CreateHMACMD5(byte[] key) |
| | 6 | 88 | | { |
| | | 89 | | #pragma warning disable CA5351 // Do not use broken cryptographic algorithms |
| | 6 | 90 | | return new System.Security.Cryptography.HMACMD5(key); |
| | | 91 | | #pragma warning restore CA5351 // Do not use broken cryptographic algorithms |
| | 6 | 92 | | } |
| | | 93 | | |
| | | 94 | | public static HMACMD5 CreateHMACMD5(byte[] key, int hashSize) |
| | 6 | 95 | | { |
| | | 96 | | #pragma warning disable CA5351 // Do not use broken cryptographic algorithms |
| | 6 | 97 | | return new HMACMD5(key, hashSize); |
| | | 98 | | #pragma warning restore CA5351 // Do not use broken cryptographic algorithms |
| | 6 | 99 | | } |
| | | 100 | | |
| | | 101 | | public static System.Security.Cryptography.HMACSHA1 CreateHMACSHA1(byte[] key) |
| | 2366 | 102 | | { |
| | | 103 | | #pragma warning disable CA5350 // Do not use weak cryptographic algorithms |
| | 2366 | 104 | | return new System.Security.Cryptography.HMACSHA1(key); |
| | | 105 | | #pragma warning restore CA5350 // Do not use weak cryptographic algorithms |
| | 2366 | 106 | | } |
| | | 107 | | |
| | | 108 | | public static HMACSHA1 CreateHMACSHA1(byte[] key, int hashSize) |
| | 6 | 109 | | { |
| | | 110 | | #pragma warning disable CA5350 // Do not use weak cryptographic algorithms |
| | 6 | 111 | | return new HMACSHA1(key, hashSize); |
| | | 112 | | #pragma warning restore CA5350 // Do not use weak cryptographic algorithms |
| | 6 | 113 | | } |
| | | 114 | | |
| | | 115 | | public static System.Security.Cryptography.HMACSHA256 CreateHMACSHA256(byte[] key) |
| | 6 | 116 | | { |
| | 6 | 117 | | return new System.Security.Cryptography.HMACSHA256(key); |
| | 6 | 118 | | } |
| | | 119 | | |
| | | 120 | | public static HMACSHA256 CreateHMACSHA256(byte[] key, int hashSize) |
| | 0 | 121 | | { |
| | 0 | 122 | | return new HMACSHA256(key, hashSize); |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | public static System.Security.Cryptography.HMACSHA384 CreateHMACSHA384(byte[] key) |
| | 0 | 126 | | { |
| | 0 | 127 | | return new System.Security.Cryptography.HMACSHA384(key); |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | public static HMACSHA384 CreateHMACSHA384(byte[] key, int hashSize) |
| | 0 | 131 | | { |
| | 0 | 132 | | return new HMACSHA384(key, hashSize); |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | public static System.Security.Cryptography.HMACSHA512 CreateHMACSHA512(byte[] key) |
| | 6 | 136 | | { |
| | 6 | 137 | | return new System.Security.Cryptography.HMACSHA512(key); |
| | 6 | 138 | | } |
| | | 139 | | |
| | | 140 | | public static HMACSHA512 CreateHMACSHA512(byte[] key, int hashSize) |
| | 0 | 141 | | { |
| | 0 | 142 | | return new HMACSHA512(key, hashSize); |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | #if FEATURE_HMAC_RIPEMD160 |
| | | 146 | | public static System.Security.Cryptography.HMACRIPEMD160 CreateHMACRIPEMD160(byte[] key) |
| | 0 | 147 | | { |
| | | 148 | | #pragma warning disable CA5350 // Do not use weak cryptographic algorithms |
| | 0 | 149 | | return new System.Security.Cryptography.HMACRIPEMD160(key); |
| | | 150 | | #pragma warning restore CA5350 // Do not use weak cryptographic algorithms |
| | 0 | 151 | | } |
| | | 152 | | #else |
| | | 153 | | public static global::SshNet.Security.Cryptography.HMACRIPEMD160 CreateHMACRIPEMD160(byte[] key) |
| | 0 | 154 | | { |
| | 0 | 155 | | return new global::SshNet.Security.Cryptography.HMACRIPEMD160(key); |
| | 0 | 156 | | } |
| | | 157 | | #endif // FEATURE_HMAC_RIPEMD160 |
| | | 158 | | } |
| | | 159 | | } |