Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ Derive() [2/2]

static void System.Security.Cryptography.Pkcs.Pkcs12Kdf.Derive ( ReadOnlySpan< char > password,
HashAlgorithmName hashAlgorithm,
int iterationCount,
byte id,
ReadOnlySpan< byte > salt,
Span< byte > destination )
inlinestaticprivate

Definition at line 26 of file Pkcs12Kdf.cs.

27 {
28 int num = -1;
29 int num2 = -1;
30 (HashAlgorithmName, int, int)[] array = s_uvLookup;
31 for (int i = 0; i < array.Length; i++)
32 {
33 (HashAlgorithmName, int, int) tuple = array[i];
34 if (tuple.Item1 == hashAlgorithm)
35 {
36 num = tuple.Item2;
37 num2 = tuple.Item3;
38 break;
39 }
40 }
41 if (num == -1)
42 {
43 throw new CryptographicException(System.SR.Cryptography_UnknownHashAlgorithm, hashAlgorithm.Name);
44 }
45 int num3 = num2 >> 3;
46 Span<byte> span = stackalloc byte[num3];
47 span.Fill(id);
48 int num4 = (salt.Length - 1 + num3) / num3 * num3;
49 int num5 = checked((password.Length + 1) * 2);
50 if (password == default(ReadOnlySpan<char>))
51 {
52 num5 = 0;
53 }
54 int num6 = (num5 - 1 + num3) / num3 * num3;
55 int num7 = num4 + num6;
56 Span<byte> span2 = default(Span<byte>);
57 byte[] array2 = null;
58 if (num7 <= 1024)
59 {
60 span2 = stackalloc byte[num7];
61 }
62 else
63 {
65 span2 = array2.AsSpan(0, num7);
66 }
67 IncrementalHash incrementalHash = IncrementalHash.CreateHash(hashAlgorithm);
68 try
69 {
70 CircularCopy(salt, span2.Slice(0, num4));
71 CircularCopyUtf16BE(password, span2.Slice(num4));
72 Span<byte> span3 = stackalloc byte[num >> 3];
73 Span<byte> span4 = stackalloc byte[num3];
74 while (true)
75 {
76 incrementalHash.AppendData(span);
77 incrementalHash.AppendData(span2);
78 for (int num8 = iterationCount; num8 > 0; num8--)
79 {
80 if (!incrementalHash.TryGetHashAndReset(span3, out var bytesWritten) || bytesWritten != span3.Length)
81 {
82 throw new CryptographicException();
83 }
84 if (num8 != 1)
85 {
86 incrementalHash.AppendData(span3);
87 }
88 }
89 if (span3.Length >= destination.Length)
90 {
91 break;
92 }
93 span3.CopyTo(destination);
94 destination = destination.Slice(span3.Length);
95 CircularCopy(span3, span4);
96 for (int num9 = span2.Length / num3 - 1; num9 >= 0; num9--)
97 {
98 Span<byte> into = span2.Slice(num9 * num3, num3);
99 AddPlusOne(into, span4);
100 }
101 }
102 span3.Slice(0, destination.Length).CopyTo(destination);
103 }
104 finally
105 {
106 CryptographicOperations.ZeroMemory(span2);
107 if (array2 != null)
108 {
110 }
111 incrementalHash.Dispose();
112 }
113 }
static string Cryptography_UnknownHashAlgorithm
Definition SR.cs:152
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 AddPlusOne(Span< byte > into, Span< byte > addend)
Definition Pkcs12Kdf.cs:115
static void CircularCopy(ReadOnlySpan< byte > bytes, Span< byte > destination)
Definition Pkcs12Kdf.cs:126
static void CircularCopyUtf16BE(ReadOnlySpan< char > password, Span< byte > destination)
Definition Pkcs12Kdf.cs:141

References System.Security.Cryptography.Pkcs.Pkcs12Kdf.AddPlusOne(), System.Security.Cryptography.IncrementalHash.AppendData(), System.array, System.Security.Cryptography.Pkcs.Pkcs12Kdf.CircularCopy(), System.Security.Cryptography.Pkcs.Pkcs12Kdf.CircularCopyUtf16BE(), System.Span< T >.CopyTo(), System.Security.Cryptography.IncrementalHash.CreateHash(), System.SR.Cryptography_UnknownHashAlgorithm, System.destination, System.Security.Cryptography.IncrementalHash.Dispose(), System.Span< T >.Fill(), System.ReadOnlySpan< T >.Length, System.Span< T >.Length, System.Security.Cryptography.HashAlgorithmName.Name, System.Security.Cryptography.CryptoPool.Rent(), System.Security.Cryptography.CryptoPool.Return(), System.Span< T >.Slice(), System.Security.Cryptography.IncrementalHash.TryGetHashAndReset(), and System.Security.Cryptography.CryptographicOperations.ZeroMemory().