Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
PasswordDeriveBytes.cs
Go to the documentation of this file.
3using System.IO;
5using System.Text;
8
10
11[EditorBrowsable(EditorBrowsableState.Never)]
13{
14 private int _extraCount;
15
16 private int _prefix;
17
18 private int _iterations;
19
20 private byte[] _baseValue;
21
22 private byte[] _extra;
23
24 private byte[] _salt;
25
26 private readonly byte[] _password;
27
28 private string _hashName;
29
31
32 private readonly CspParameters _cspParams;
33
35
36 public string HashName
37 {
38 get
39 {
40 return _hashName;
41 }
42 [RequiresUnreferencedCode("The hash implementation might be removed. Ensure the referenced hash algorithm is not trimmed.")]
43 set
44 {
45 if (_baseValue != null)
46 {
48 }
51 }
52 }
53
54 public int IterationCount
55 {
56 get
57 {
58 return _iterations;
59 }
60 set
61 {
62 if (value <= 0)
63 {
65 }
66 if (_baseValue != null)
67 {
69 }
71 }
72 }
73
74 public byte[]? Salt
75 {
76 get
77 {
78 return (byte[])_salt?.Clone();
79 }
80 set
81 {
82 if (_baseValue != null)
83 {
85 }
86 _salt = (byte[])value?.Clone();
87 }
88 }
89
91 {
92 get
93 {
94 if (_safeProvHandle == null)
95 {
96 lock (this)
97 {
98 if (_safeProvHandle == null)
99 {
102 _safeProvHandle = safeProvHandle;
103 }
104 }
105 }
106 return _safeProvHandle;
107 }
108 }
109
110 public PasswordDeriveBytes(string strPassword, byte[]? rgbSalt)
111 : this(strPassword, rgbSalt, new CspParameters())
112 {
113 }
114
115 public PasswordDeriveBytes(byte[] password, byte[]? salt)
116 : this(password, salt, new CspParameters())
117 {
118 }
119
120 [RequiresUnreferencedCode("The hash implementation might be removed. Ensure the referenced hash algorithm is not trimmed.")]
121 public PasswordDeriveBytes(string strPassword, byte[]? rgbSalt, string strHashName, int iterations)
122 : this(strPassword, rgbSalt, strHashName, iterations, new CspParameters())
123 {
124 }
125
126 [RequiresUnreferencedCode("The hash implementation might be removed. Ensure the referenced hash algorithm is not trimmed.")]
127 public PasswordDeriveBytes(byte[] password, byte[]? salt, string hashName, int iterations)
128 : this(password, salt, hashName, iterations, new CspParameters())
129 {
130 }
131
132 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The correct hash algorithm is being preserved by the DynamicDependency.")]
133 [DynamicDependency(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof(SHA1CryptoServiceProvider))]
134 public PasswordDeriveBytes(string strPassword, byte[]? rgbSalt, CspParameters? cspParams)
135 : this(strPassword, rgbSalt, "SHA1", 100, cspParams)
136 {
137 }
138
139 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The correct hash algorithm is being preserved by the DynamicDependency.")]
140 [DynamicDependency(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof(SHA1CryptoServiceProvider))]
141 public PasswordDeriveBytes(byte[] password, byte[]? salt, CspParameters? cspParams)
142 : this(password, salt, "SHA1", 100, cspParams)
143 {
144 }
145
146 [RequiresUnreferencedCode("The hash implementation might be removed. Ensure the referenced hash algorithm is not trimmed.")]
147 public PasswordDeriveBytes(string strPassword, byte[]? rgbSalt, string strHashName, int iterations, CspParameters? cspParams)
148 : this(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false).GetBytes(strPassword), rgbSalt, strHashName, iterations, cspParams)
149 {
150 }
151
152 [RequiresUnreferencedCode("The hash implementation might be removed. Ensure the referenced hash algorithm is not trimmed.")]
153 public PasswordDeriveBytes(byte[] password, byte[]? salt, string hashName, int iterations, CspParameters? cspParams)
154 {
155 IterationCount = iterations;
156 Salt = salt;
157 HashName = hashName;
158 _password = password;
159 _cspParams = cspParams;
160 }
161
162 [Obsolete("Rfc2898DeriveBytes replaces PasswordDeriveBytes for deriving key material from a password and is preferred in new applications.")]
163 public override byte[] GetBytes(int cb)
164 {
165 int num = 0;
166 byte[] array = new byte[cb];
167 if (_baseValue == null)
168 {
170 }
171 else if (_extra != null)
172 {
173 num = _extra.Length - _extraCount;
174 if (num >= cb)
175 {
177 if (num > cb)
178 {
179 _extraCount += cb;
180 }
181 else
182 {
183 _extra = null;
184 }
185 return array;
186 }
187 Buffer.BlockCopy(_extra, num, array, 0, num);
188 _extra = null;
189 }
190 byte[] array2 = ComputeBytes(cb - num);
191 Buffer.BlockCopy(array2, 0, array, num, cb - num);
192 if (array2.Length + num > cb)
193 {
194 _extra = array2;
195 _extraCount = cb - num;
196 }
197 return array;
198 }
199
200 public override void Reset()
201 {
202 _prefix = 0;
203 _extra = null;
204 _baseValue = null;
205 }
206
207 protected override void Dispose(bool disposing)
208 {
209 base.Dispose(disposing);
210 if (disposing)
211 {
212 _hash?.Dispose();
213 if (_baseValue != null)
214 {
216 }
217 if (_extra != null)
218 {
220 }
221 if (_password != null)
222 {
224 }
225 if (_salt != null)
226 {
228 }
229 }
230 }
231
232 private byte[] ComputeBaseValue()
233 {
236 if (_salt != null)
237 {
238 _hash.TransformBlock(_salt, 0, _salt.Length, _salt, 0);
239 }
240 _hash.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
243 for (int i = 1; i < _iterations - 1; i++)
244 {
247 }
248 return _baseValue;
249 }
250
251 private byte[] ComputeBytes(int cb)
252 {
253 int num = 0;
255 int num2 = _hash.HashSize / 8;
256 byte[] array = new byte[(cb + num2 - 1) / num2 * num2];
257 using (CryptoStream cryptoStream = new CryptoStream(Stream.Null, _hash, CryptoStreamMode.Write))
258 {
259 HashPrefix(cryptoStream);
260 cryptoStream.Write(_baseValue, 0, _baseValue.Length);
261 cryptoStream.Close();
262 }
263 Buffer.BlockCopy(_hash.Hash, 0, array, num, num2);
264 for (num += num2; cb > num; num += num2)
265 {
267 using (CryptoStream cryptoStream2 = new CryptoStream(Stream.Null, _hash, CryptoStreamMode.Write))
268 {
269 HashPrefix(cryptoStream2);
270 cryptoStream2.Write(_baseValue, 0, _baseValue.Length);
271 cryptoStream2.Close();
272 }
273 Buffer.BlockCopy(_hash.Hash, 0, array, num, num2);
274 }
275 return array;
276 }
277
278 private void HashPrefix(CryptoStream cs)
279 {
280 if (_prefix > 999)
281 {
283 }
284 int num = 0;
285 byte[] array = new byte[3] { 48, 48, 48 };
286 if (_prefix >= 100)
287 {
288 array[0] += (byte)(_prefix / 100);
289 num++;
290 }
291 if (_prefix >= 10)
292 {
293 array[num] += (byte)(_prefix % 100 / 10);
294 num++;
295 }
296 if (_prefix > 0)
297 {
298 array[num] += (byte)(_prefix % 10);
299 num++;
300 cs.Write(array, 0, num);
301 }
302 _prefix++;
303 }
304
305 [SupportedOSPlatform("windows")]
306 public byte[] CryptDeriveKey(string? algname, string? alghashname, int keySize, byte[] rgbIV)
307 {
308 if (keySize < 0)
309 {
311 }
312 int num = CapiHelper.NameOrOidToHashAlgId(alghashname, OidGroup.HashAlgorithm);
313 if (num == 0)
314 {
316 }
317 int num2 = CapiHelper.NameOrOidToHashAlgId(algname, OidGroup.All);
318 if (num2 == 0)
319 {
321 }
322 if (rgbIV == null)
323 {
325 }
326 byte[] pbKey = null;
327 CapiHelper.DeriveKey(ProvHandle, num2, num, _password, _password.Length, keySize << 16, rgbIV, rgbIV.Length, ref pbKey);
328 return pbKey;
329 }
330
332 {
333 if (cspParams == null)
334 {
335 cspParams = new CspParameters(24);
336 }
337 CapiHelper.AcquireCsp(cspParams, out var safeProvHandle);
338 return safeProvHandle;
339 }
340}
static void AcquireCsp(CspParameters cspParameters, out SafeProvHandle safeProvHandle)
static int NameOrOidToHashAlgId(string nameOrOid, OidGroup oidGroup)
static void DeriveKey(SafeProvHandle hProv, int algid, int algidHash, byte[] password, int cbPassword, int dwFlags, byte[] IV_Out, int cbIV_In, [NotNull] ref byte[] pbKey)
static unsafe void Clear(Array array)
Definition Array.cs:755
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
virtual void Close()
Definition Stream.cs:644
static readonly Stream Null
Definition Stream.cs:488
static string Cryptography_PasswordDerivedBytes_InvalidAlgorithm
Definition SR.cs:56
static string ArgumentOutOfRange_NeedPosNum
Definition SR.cs:20
static string Cryptography_PasswordDerivedBytes_ValuesFixed
Definition SR.cs:62
static string Cryptography_InvalidKeySize
Definition SR.cs:92
static string Cryptography_PasswordDerivedBytes_TooManyBytes
Definition SR.cs:60
static string Cryptography_PasswordDerivedBytes_InvalidIV
Definition SR.cs:58
Definition SR.cs:7
static ? object CreateFromName(string name, params object?[]? args)
override void Write(byte[] buffer, int offset, int count)
byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[]? outputBuffer, int outputOffset)
PasswordDeriveBytes(string strPassword, byte[]? rgbSalt, string strHashName, int iterations)
PasswordDeriveBytes(byte[] password, byte[]? salt, CspParameters? cspParams)
PasswordDeriveBytes(string strPassword, byte[]? rgbSalt)
PasswordDeriveBytes(string strPassword, byte[]? rgbSalt, CspParameters? cspParams)
static SafeProvHandle AcquireSafeProviderHandle(CspParameters cspParams)
PasswordDeriveBytes(byte[] password, byte[]? salt, string hashName, int iterations, CspParameters? cspParams)
byte[] CryptDeriveKey(string? algname, string? alghashname, int keySize, byte[] rgbIV)
PasswordDeriveBytes(string strPassword, byte[]? rgbSalt, string strHashName, int iterations, CspParameters? cspParams)
PasswordDeriveBytes(byte[] password, byte[]? salt, string hashName, int iterations)
static void MemoryBarrier()
Definition Thread.cs:827