Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BasicSymmetricCipherBCrypt.cs
Go to the documentation of this file.
1using System;
4
6
7internal sealed class BasicSymmetricCipherBCrypt : Internal.Cryptography.BasicSymmetricCipher
8{
9 private readonly bool _encrypting;
10
12
13 private byte[] _currentIv;
14
15 public BasicSymmetricCipherBCrypt(Internal.NativeCrypto.SafeAlgorithmHandle algorithm, CipherMode cipherMode, int blockSizeInBytes, int paddingSizeInBytes, byte[] key, bool ownsParentHandle, byte[] iv, bool encrypting)
16 : base(cipherMode.GetCipherIv(iv), blockSizeInBytes, paddingSizeInBytes)
17 {
18 _encrypting = encrypting;
19 if (base.IV != null)
20 {
21 _currentIv = new byte[base.IV.Length];
22 }
23 _hKey = global::Interop.BCrypt.BCryptImportKey(algorithm, key);
24 if (ownsParentHandle)
25 {
26 _hKey.SetParentHandle(algorithm);
27 }
28 Reset();
29 }
30
31 protected override void Dispose(bool disposing)
32 {
33 if (disposing)
34 {
36 _hKey = null;
37 hKey?.Dispose();
38 byte[] currentIv = _currentIv;
39 _currentIv = null;
40 if (currentIv != null)
41 {
42 Array.Clear(currentIv);
43 }
44 }
45 base.Dispose(disposing);
46 }
47
48 public override int Transform(ReadOnlySpan<byte> input, Span<byte> output)
49 {
50 int num = 0;
51 if (input.Overlaps(output, out var elementOffset) && elementOffset != 0)
52 {
54 try
55 {
56 num = BCryptTransform(input, array);
57 array.AsSpan(0, num).CopyTo(output);
58 }
59 finally
60 {
62 }
63 }
64 else
65 {
66 num = BCryptTransform(input, output);
67 }
68 if (num != input.Length)
69 {
71 }
72 return num;
73 int BCryptTransform(ReadOnlySpan<byte> input, Span<byte> output)
74 {
75 if (!_encrypting)
76 {
77 return global::Interop.BCrypt.BCryptDecrypt(_hKey, input, _currentIv, output);
78 }
79 return global::Interop.BCrypt.BCryptEncrypt(_hKey, input, _currentIv, output);
80 }
81 }
82
84 {
85 int result = 0;
86 if (input.Length != 0)
87 {
88 result = Transform(input, output);
89 }
90 Reset();
91 return result;
92 }
93
94 private void Reset()
95 {
96 if (base.IV != null)
97 {
98 Buffer.BlockCopy(base.IV, 0, _currentIv, 0, base.IV.Length);
99 }
100 }
101}
override int TransformFinal(ReadOnlySpan< byte > input, Span< byte > output)
BasicSymmetricCipherBCrypt(Internal.NativeCrypto.SafeAlgorithmHandle algorithm, CipherMode cipherMode, int blockSizeInBytes, int paddingSizeInBytes, byte[] key, bool ownsParentHandle, byte[] iv, bool encrypting)
override int Transform(ReadOnlySpan< byte > input, Span< byte > output)
void SetParentHandle(SafeAlgorithmHandle parentHandle)
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
static string Cryptography_UnexpectedTransformTruncation
Definition SR.cs:158
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
int Length
Definition Span.cs:70