Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HKDF.cs
Go to the documentation of this file.
2
4
5[UnsupportedOSPlatform("browser")]
6public static class HKDF
7{
8 public static byte[] Extract(HashAlgorithmName hashAlgorithmName, byte[] ikm, byte[]? salt = null)
9 {
10 if (ikm == null)
11 {
12 throw new ArgumentNullException("ikm");
13 }
14 int num = HashLength(hashAlgorithmName);
15 byte[] array = new byte[num];
16 Extract(hashAlgorithmName, num, ikm, salt, array);
17 return array;
18 }
19
20 public static int Extract(HashAlgorithmName hashAlgorithmName, ReadOnlySpan<byte> ikm, ReadOnlySpan<byte> salt, Span<byte> prk)
21 {
22 int num = HashLength(hashAlgorithmName);
23 if (prk.Length < num)
24 {
26 }
27 if (prk.Length > num)
28 {
29 prk = prk.Slice(0, num);
30 }
31 Extract(hashAlgorithmName, num, ikm, salt, prk);
32 return num;
33 }
34
35 private static void Extract(HashAlgorithmName hashAlgorithmName, int hashLength, ReadOnlySpan<byte> ikm, ReadOnlySpan<byte> salt, Span<byte> prk)
36 {
37 int num = HashOneShotHelpers.MacData(hashAlgorithmName, salt, ikm, prk);
38 }
39
40 public static byte[] Expand(HashAlgorithmName hashAlgorithmName, byte[] prk, int outputLength, byte[]? info = null)
41 {
42 if (prk == null)
43 {
44 throw new ArgumentNullException("prk");
45 }
46 if (outputLength <= 0)
47 {
49 }
50 int num = HashLength(hashAlgorithmName);
51 int num2 = 255 * num;
52 if (outputLength <= 0 || outputLength > num2)
53 {
55 }
56 byte[] array = new byte[outputLength];
57 Expand(hashAlgorithmName, num, prk, array, info);
58 return array;
59 }
60
61 public static void Expand(HashAlgorithmName hashAlgorithmName, ReadOnlySpan<byte> prk, Span<byte> output, ReadOnlySpan<byte> info)
62 {
63 int num = HashLength(hashAlgorithmName);
64 if (output.Length == 0)
65 {
67 }
68 int num2 = 255 * num;
69 if (output.Length > num2)
70 {
72 }
73 Expand(hashAlgorithmName, num, prk, output, info);
74 }
75
76 private static void Expand(HashAlgorithmName hashAlgorithmName, int hashLength, ReadOnlySpan<byte> prk, Span<byte> output, ReadOnlySpan<byte> info)
77 {
78 if (prk.Length < hashLength)
79 {
81 }
82 Span<byte> span = stackalloc byte[1];
83 ref byte reference = ref span[0];
85 Span<byte> destination = output;
86 Span<byte> span3 = stackalloc byte[64];
87 ReadOnlySpan<byte> readOnlySpan = default(Span<byte>);
88 byte[] array = null;
89 if (output.Overlaps(info))
90 {
91 if (info.Length > 64)
92 {
94 span3 = array;
95 }
96 span3 = span3.Slice(0, info.Length);
97 info.CopyTo(span3);
98 readOnlySpan = span3;
99 }
100 else
101 {
102 readOnlySpan = info;
103 }
104 using (IncrementalHash incrementalHash = IncrementalHash.CreateHMAC(hashAlgorithmName, prk))
105 {
106 int num = 1;
107 while (true)
108 {
109 incrementalHash.AppendData(span2);
110 incrementalHash.AppendData(readOnlySpan);
111 reference = (byte)num;
112 incrementalHash.AppendData(span);
113 if (destination.Length < hashLength)
114 {
115 break;
116 }
117 span2 = destination.Slice(0, hashLength);
118 destination = destination.Slice(hashLength);
119 GetHashAndReset(incrementalHash, span2);
120 num++;
121 }
122 if (destination.Length > 0)
123 {
124 Span<byte> output2 = stackalloc byte[hashLength];
125 GetHashAndReset(incrementalHash, output2);
126 output2.Slice(0, destination.Length).CopyTo(destination);
127 }
128 }
129 if (array != null)
130 {
132 }
133 }
134
135 public static byte[] DeriveKey(HashAlgorithmName hashAlgorithmName, byte[] ikm, int outputLength, byte[]? salt = null, byte[]? info = null)
136 {
137 if (ikm == null)
138 {
139 throw new ArgumentNullException("ikm");
140 }
141 if (outputLength <= 0)
142 {
144 }
145 int num = HashLength(hashAlgorithmName);
146 int num2 = 255 * num;
147 if (outputLength > num2)
148 {
150 }
151 Span<byte> span = stackalloc byte[num];
152 Extract(hashAlgorithmName, num, ikm, salt, span);
153 byte[] array = new byte[outputLength];
154 Expand(hashAlgorithmName, num, span, array, info);
155 return array;
156 }
157
159 {
160 int num = HashLength(hashAlgorithmName);
161 if (output.Length == 0)
162 {
164 }
165 int num2 = 255 * num;
166 if (output.Length > num2)
167 {
169 }
170 Span<byte> span = stackalloc byte[num];
171 Extract(hashAlgorithmName, num, ikm, salt, span);
172 Expand(hashAlgorithmName, num, span, output, info);
173 }
174
175 private static void GetHashAndReset(IncrementalHash hmac, Span<byte> output)
176 {
177 if (!hmac.TryGetHashAndReset(output, out var _))
178 {
180 }
181 }
182
183 private static int HashLength(HashAlgorithmName hashAlgorithmName)
184 {
185 if (hashAlgorithmName == HashAlgorithmName.SHA1)
186 {
187 return 20;
188 }
189 if (hashAlgorithmName == HashAlgorithmName.SHA256)
190 {
191 return 32;
192 }
193 if (hashAlgorithmName == HashAlgorithmName.SHA384)
194 {
195 return 48;
196 }
197 if (hashAlgorithmName == HashAlgorithmName.SHA512)
198 {
199 return 64;
200 }
201 if (hashAlgorithmName == HashAlgorithmName.MD5)
202 {
203 return 16;
204 }
205 throw new ArgumentOutOfRangeException("hashAlgorithmName");
206 }
207}
static string ArgumentOutOfRange_NeedPosNum
Definition SR.cs:20
static string Argument_DestinationTooShort
Definition SR.cs:14
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Cryptography_Prk_TooSmall
Definition SR.cs:170
static string Arg_CryptographyException
Definition SR.cs:108
static string Cryptography_Okm_TooLarge
Definition SR.cs:172
Definition SR.cs:7
static void Return(byte[] array, int clearSize=-1)
Definition CryptoPool.cs:12
static byte[] Rent(int minimumLength)
Definition CryptoPool.cs:7
static void GetHashAndReset(IncrementalHash hmac, Span< byte > output)
Definition HKDF.cs:175
static int Extract(HashAlgorithmName hashAlgorithmName, ReadOnlySpan< byte > ikm, ReadOnlySpan< byte > salt, Span< byte > prk)
Definition HKDF.cs:20
static byte[] DeriveKey(HashAlgorithmName hashAlgorithmName, byte[] ikm, int outputLength, byte[]? salt=null, byte[]? info=null)
Definition HKDF.cs:135
static void DeriveKey(HashAlgorithmName hashAlgorithmName, ReadOnlySpan< byte > ikm, Span< byte > output, ReadOnlySpan< byte > salt, ReadOnlySpan< byte > info)
Definition HKDF.cs:158
static void Expand(HashAlgorithmName hashAlgorithmName, ReadOnlySpan< byte > prk, Span< byte > output, ReadOnlySpan< byte > info)
Definition HKDF.cs:61
static void Expand(HashAlgorithmName hashAlgorithmName, int hashLength, ReadOnlySpan< byte > prk, Span< byte > output, ReadOnlySpan< byte > info)
Definition HKDF.cs:76
static void Extract(HashAlgorithmName hashAlgorithmName, int hashLength, ReadOnlySpan< byte > ikm, ReadOnlySpan< byte > salt, Span< byte > prk)
Definition HKDF.cs:35
static int HashLength(HashAlgorithmName hashAlgorithmName)
Definition HKDF.cs:183
static byte[] Extract(HashAlgorithmName hashAlgorithmName, byte[] ikm, byte[]? salt=null)
Definition HKDF.cs:8
static byte[] Expand(HashAlgorithmName hashAlgorithmName, byte[] prk, int outputLength, byte[]? info=null)
Definition HKDF.cs:40
static int MacData(HashAlgorithmName hashAlgorithm, ReadOnlySpan< byte > key, ReadOnlySpan< byte > source, Span< byte > destination)
static IncrementalHash CreateHMAC(HashAlgorithmName hashAlgorithm, byte[] key)
bool TryGetHashAndReset(Span< byte > destination, out int bytesWritten)
Span< T > Slice(int start)
Definition Span.cs:271
static Span< T > Empty
Definition Span.cs:87
int Length
Definition Span.cs:70