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