Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
IncrementalHash.cs
Go to the documentation of this file.
3
5
6public sealed class IncrementalHash : IDisposable
7{
9
11
13
14 private bool _disposed;
15
16 public int HashLengthInBytes { get; }
17
19
26
28 {
29 _algorithmName = new HashAlgorithmName("HMAC" + name.Name);
30 _hmac = hmac;
32 }
33
34 public void AppendData(byte[] data)
35 {
36 if (data == null)
37 {
38 throw new ArgumentNullException("data");
39 }
41 }
42
43 public void AppendData(byte[] data, int offset, int count)
44 {
45 if (data == null)
46 {
47 throw new ArgumentNullException("data");
48 }
49 if (offset < 0)
50 {
52 }
53 if (count < 0 || count > data.Length)
54 {
55 throw new ArgumentOutOfRangeException("count");
56 }
57 if (data.Length - count < offset)
58 {
60 }
61 if (_disposed)
62 {
63 throw new ObjectDisposedException("IncrementalHash");
64 }
66 }
67
69 {
70 if (_disposed)
71 {
72 throw new ObjectDisposedException("IncrementalHash");
73 }
74 if (_hash != null)
75 {
77 }
78 else
79 {
81 }
82 }
83
84 public byte[] GetHashAndReset()
85 {
86 if (_disposed)
87 {
88 throw new ObjectDisposedException("IncrementalHash");
89 }
90 byte[] array = new byte[HashLengthInBytes];
91 int hashAndResetCore = GetHashAndResetCore(array);
92 return array;
93 }
94
96 {
97 if (_disposed)
98 {
99 throw new ObjectDisposedException("IncrementalHash");
100 }
101 if (destination.Length < HashLengthInBytes)
102 {
104 }
106 }
107
108 public bool TryGetHashAndReset(Span<byte> destination, out int bytesWritten)
109 {
110 if (_disposed)
111 {
112 throw new ObjectDisposedException("IncrementalHash");
113 }
114 if (destination.Length < HashLengthInBytes)
115 {
116 bytesWritten = 0;
117 return false;
118 }
119 bytesWritten = GetHashAndResetCore(destination);
120 return true;
121 }
122
124 {
125 if (_hash == null)
126 {
128 }
130 }
131
132 public byte[] GetCurrentHash()
133 {
134 if (_disposed)
135 {
136 throw new ObjectDisposedException("IncrementalHash");
137 }
138 byte[] array = new byte[HashLengthInBytes];
139 int currentHashCore = GetCurrentHashCore(array);
140 return array;
141 }
142
144 {
145 if (_disposed)
146 {
147 throw new ObjectDisposedException("IncrementalHash");
148 }
149 if (destination.Length < HashLengthInBytes)
150 {
152 }
154 }
155
156 public bool TryGetCurrentHash(Span<byte> destination, out int bytesWritten)
157 {
158 if (_disposed)
159 {
160 throw new ObjectDisposedException("IncrementalHash");
161 }
162 if (destination.Length < HashLengthInBytes)
163 {
164 bytesWritten = 0;
165 return false;
166 }
167 bytesWritten = GetCurrentHashCore(destination);
168 return true;
169 }
170
172 {
173 if (_hash == null)
174 {
176 }
178 }
179
180 public void Dispose()
181 {
182 _disposed = true;
183 if (_hash != null)
184 {
185 _hash.Dispose();
186 _hash = null;
187 }
188 if (_hmac != null)
189 {
190 _hmac.Dispose(disposing: true);
191 _hmac = null;
192 }
193 }
194
195 public static IncrementalHash CreateHash(HashAlgorithmName hashAlgorithm)
196 {
197 if (string.IsNullOrEmpty(hashAlgorithm.Name))
198 {
200 }
201 return new IncrementalHash(hashAlgorithm, HashProviderDispenser.CreateHashProvider(hashAlgorithm.Name));
202 }
203
204 [UnsupportedOSPlatform("browser")]
205 public static IncrementalHash CreateHMAC(HashAlgorithmName hashAlgorithm, byte[] key)
206 {
207 if (key == null)
208 {
209 throw new ArgumentNullException("key");
210 }
211 return CreateHMAC(hashAlgorithm, (ReadOnlySpan<byte>)key);
212 }
213
214 [UnsupportedOSPlatform("browser")]
216 {
217 if (string.IsNullOrEmpty(hashAlgorithm.Name))
218 {
220 }
221 return new IncrementalHash(hashAlgorithm, new HMACCommon(hashAlgorithm.Name, key, -1));
222 }
223}
void Dispose(bool disposing)
Definition HMACCommon.cs:98
int GetCurrentHash(Span< byte > destination)
Definition HMACCommon.cs:88
void AppendHashData(byte[] data, int offset, int count)
Definition HMACCommon.cs:63
static HashProvider CreateHashProvider(string hashAlgorithmId)
void AppendHashData(byte[] data, int offset, int count)
int FinalizeHashAndReset(Span< byte > destination)
int GetCurrentHash(Span< byte > destination)
static string Argument_DestinationTooShort
Definition SR.cs:14
static string Argument_InvalidOffLen
Definition SR.cs:22
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
static string Cryptography_HashAlgorithmNameNullOrEmpty
Definition SR.cs:60
Definition SR.cs:7
IncrementalHash(HashAlgorithmName name, HashProvider hash)
static IncrementalHash CreateHash(HashAlgorithmName hashAlgorithm)
void AppendData(ReadOnlySpan< byte > data)
static IncrementalHash CreateHMAC(HashAlgorithmName hashAlgorithm, ReadOnlySpan< byte > key)
void AppendData(byte[] data, int offset, int count)
int GetCurrentHashCore(Span< byte > destination)
static IncrementalHash CreateHMAC(HashAlgorithmName hashAlgorithm, byte[] key)
bool TryGetHashAndReset(Span< byte > destination, out int bytesWritten)
IncrementalHash(HashAlgorithmName name, HMACCommon hmac)
int GetHashAndResetCore(Span< byte > destination)
int GetCurrentHash(Span< byte > destination)
bool TryGetCurrentHash(Span< byte > destination, out int bytesWritten)
int GetHashAndReset(Span< byte > destination)