Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UniversalCryptoDecryptor.cs
Go to the documentation of this file.
1using System;
3
5
6internal sealed class UniversalCryptoDecryptor : Internal.Cryptography.UniversalCryptoTransform
7{
8 private byte[] _heldoverCipher;
9
10 private bool DepaddingRequired
11 {
12 get
13 {
14 switch (base.PaddingMode)
15 {
16 case PaddingMode.PKCS7:
17 case PaddingMode.ANSIX923:
18 case PaddingMode.ISO10126:
19 return true;
20 case PaddingMode.None:
21 case PaddingMode.Zeros:
22 return false;
23 default:
25 }
26 }
27 }
28
30 : base(paddingMode, basicSymmetricCipher)
31 {
32 }
33
34 protected override int UncheckedTransformBlock(ReadOnlySpan<byte> inputBuffer, Span<byte> outputBuffer)
35 {
36 int num = 0;
38 {
39 if (_heldoverCipher != null)
40 {
41 int num2 = base.BasicSymmetricCipher.Transform(_heldoverCipher, outputBuffer);
42 outputBuffer = outputBuffer.Slice(num2);
43 num += num2;
44 }
45 else
46 {
47 _heldoverCipher = new byte[base.InputBlockSize];
48 }
49 inputBuffer.Slice(inputBuffer.Length - _heldoverCipher.Length).CopyTo(_heldoverCipher);
50 inputBuffer = inputBuffer.Slice(0, inputBuffer.Length - _heldoverCipher.Length);
51 }
52 if (inputBuffer.Length > 0)
53 {
54 num += base.BasicSymmetricCipher.Transform(inputBuffer, outputBuffer);
55 }
56 return num;
57 }
58
59 protected unsafe override int UncheckedTransformFinalBlock(ReadOnlySpan<byte> inputBuffer, Span<byte> outputBuffer)
60 {
61 if (inputBuffer.Length % base.PaddingSizeBytes != 0)
62 {
64 }
65 byte[] array = null;
66 int num = 0;
67 try
68 {
69 Span<byte> span;
71 if (_heldoverCipher == null)
72 {
73 num = inputBuffer.Length;
75 span = array.AsSpan(0, inputBuffer.Length);
76 input = inputBuffer;
77 }
78 else
79 {
80 num = _heldoverCipher.Length + inputBuffer.Length;
82 span = array.AsSpan(0, num);
83 _heldoverCipher.AsSpan().CopyTo(span);
84 inputBuffer.CopyTo(span.Slice(_heldoverCipher.Length));
85 input = span;
86 }
87 int num2 = 0;
88 fixed (byte* ptr = span)
89 {
90 Span<byte> span2 = span[..base.BasicSymmetricCipher.TransformFinal(input, span)];
91 if (span2.Length > 0)
92 {
93 num2 = GetPaddingLength(span2);
94 span2.Slice(0, num2).CopyTo(outputBuffer);
95 }
96 }
97 Reset();
98 return num2;
99 }
100 finally
101 {
102 if (array != null)
103 {
105 }
106 }
107 }
108
109 protected unsafe override byte[] UncheckedTransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
110 {
112 {
113 byte[] array = System.Security.Cryptography.CryptoPool.Rent(inputCount + base.InputBlockSize);
114 int num = 0;
115 fixed (byte* ptr = array)
116 {
117 try
118 {
119 num = UncheckedTransformFinalBlock(inputBuffer.AsSpan(inputOffset, inputCount), array);
120 return array.AsSpan(0, num).ToArray();
121 }
122 finally
123 {
125 }
126 }
127 }
128 byte[] array2 = GC.AllocateUninitializedArray<byte>(inputCount);
129 int num2 = UncheckedTransformFinalBlock(inputBuffer.AsSpan(inputOffset, inputCount), array2);
130 return array2;
131 }
132
133 protected sealed override void Dispose(bool disposing)
134 {
135 if (disposing)
136 {
137 byte[] heldoverCipher = _heldoverCipher;
138 _heldoverCipher = null;
139 if (heldoverCipher != null)
140 {
141 Array.Clear(heldoverCipher);
142 }
143 }
144 base.Dispose(disposing);
145 }
146
147 private void Reset()
148 {
149 if (_heldoverCipher != null)
150 {
152 _heldoverCipher = null;
153 }
154 }
155
157 {
158 int num = 0;
159 switch (base.PaddingMode)
160 {
161 case PaddingMode.ANSIX923:
162 {
163 num = block[^1];
164 if (num <= 0 || num > base.InputBlockSize)
165 {
167 }
168 for (int j = block.Length - num; j < block.Length - 1; j++)
169 {
170 if (block[j] != 0)
171 {
173 }
174 }
175 break;
176 }
177 case PaddingMode.ISO10126:
178 num = block[^1];
179 if (num <= 0 || num > base.InputBlockSize)
180 {
182 }
183 break;
184 case PaddingMode.PKCS7:
185 {
186 num = block[^1];
187 if (num <= 0 || num > base.InputBlockSize)
188 {
190 }
191 for (int i = block.Length - num; i < block.Length - 1; i++)
192 {
193 if (block[i] != num)
194 {
196 }
197 }
198 break;
199 }
200 case PaddingMode.None:
201 case PaddingMode.Zeros:
202 num = 0;
203 break;
204 default:
206 }
207 return block.Length - num;
208 }
209}
override int UncheckedTransformBlock(ReadOnlySpan< byte > inputBuffer, Span< byte > outputBuffer)
UniversalCryptoDecryptor(PaddingMode paddingMode, Internal.Cryptography.BasicSymmetricCipher basicSymmetricCipher)
unsafe override byte[] UncheckedTransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
unsafe override int UncheckedTransformFinalBlock(ReadOnlySpan< byte > inputBuffer, Span< byte > outputBuffer)
static unsafe void Clear(Array array)
Definition Array.cs:755
Definition GC.cs:8
static string Cryptography_UnknownPaddingMode
Definition SR.cs:154
static string Cryptography_InvalidPadding
Definition SR.cs:106
static string Cryptography_PartialBlock
Definition SR.cs:124
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
void CopyTo(Span< T > destination)
ReadOnlySpan< T > Slice(int start)
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70