Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. namespace System.Security.Cryptography
  2. {
  3. [ComVisible(true)]
  4. public abstract class HashAlgorithm : ICryptoTransform, IDisposable
  5. {
  6. protected int HashSizeValue;
  7. protected internal byte[] HashValue;
  8. protected int State;
  9. private bool m_bDisposed;
  10.  
  11. public virtual int HashSize
  12. {
  13. get
  14. {
  15. return this.HashSizeValue;
  16. }
  17. }
  18.  
  19. public virtual byte[] Hash
  20. {
  21. get
  22. {
  23. if (this.m_bDisposed)
  24. throw new ObjectDisposedException((string) null, Environment.GetResourceString("ObjectDisposed_Generic"));
  25. if (this.State != 0)
  26. throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_HashNotYetFinalized"));
  27. return (byte[]) this.HashValue.Clone();
  28. }
  29. }
  30.  
  31. public static HashAlgorithm Create()
  32. {
  33. return HashAlgorithm.Create("System.Security.Cryptography.HashAlgorithm");
  34. }
  35.  
  36. public static HashAlgorithm Create(string hashName)
  37. {
  38. return (HashAlgorithm) CryptoConfig.CreateFromName(hashName);
  39. }
  40.  
  41. public byte[] ComputeHash(Stream inputStream)
  42. {
  43. if (this.m_bDisposed)
  44. throw new ObjectDisposedException((string) null, Environment.GetResourceString("ObjectDisposed_Generic"));
  45. byte[] numArray1 = new byte[4096];
  46. int cbSize;
  47. do
  48. {
  49. cbSize = inputStream.Read(numArray1, 0, 4096);
  50. if (cbSize > 0)
  51. this.HashCore(numArray1, 0, cbSize);
  52. }
  53. while (cbSize > 0);
  54. this.HashValue = this.HashFinal();
  55. byte[] numArray2 = (byte[]) this.HashValue.Clone();
  56. this.Initialize();
  57. return numArray2;
  58. }
  59.  
  60. public byte[] ComputeHash(byte[] buffer)
  61. {
  62. if (this.m_bDisposed)
  63. throw new ObjectDisposedException((string) null, Environment.GetResourceString("ObjectDisposed_Generic"));
  64. if (buffer == null)
  65. throw new ArgumentNullException(nameof (buffer));
  66. this.HashCore(buffer, 0, buffer.Length);
  67. this.HashValue = this.HashFinal();
  68. byte[] numArray = (byte[]) this.HashValue.Clone();
  69. this.Initialize();
  70. return numArray;
  71. }
  72.  
  73. public byte[] ComputeHash(byte[] buffer, int offset, int count)
  74. {
  75. if (this.m_bDisposed)
  76. throw new ObjectDisposedException((string) null, Environment.GetResourceString("ObjectDisposed_Generic"));
  77. if (buffer == null)
  78. throw new ArgumentNullException(nameof (buffer));
  79. if (offset < 0)
  80. throw new ArgumentOutOfRangeException(nameof (offset), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  81. if (count < 0 || count > buffer.Length)
  82. throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
  83. if (buffer.Length - count < offset)
  84. throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
  85. this.HashCore(buffer, offset, count);
  86. this.HashValue = this.HashFinal();
  87. byte[] numArray = (byte[]) this.HashValue.Clone();
  88. this.Initialize();
  89. return numArray;
  90. }
  91.  
  92. public virtual int InputBlockSize
  93. {
  94. get
  95. {
  96. return 1;
  97. }
  98. }
  99.  
  100. public virtual int OutputBlockSize
  101. {
  102. get
  103. {
  104. return 1;
  105. }
  106. }
  107.  
  108. public virtual bool CanTransformMultipleBlocks
  109. {
  110. get
  111. {
  112. return true;
  113. }
  114. }
  115.  
  116. public virtual bool CanReuseTransform
  117. {
  118. get
  119. {
  120. return true;
  121. }
  122. }
  123.  
  124. public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  125. {
  126. if (this.m_bDisposed)
  127. throw new ObjectDisposedException((string) null, Environment.GetResourceString("ObjectDisposed_Generic"));
  128. if (inputBuffer == null)
  129. throw new ArgumentNullException(nameof (inputBuffer));
  130. if (inputOffset < 0)
  131. throw new ArgumentOutOfRangeException(nameof (inputOffset), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  132. if (inputCount < 0 || inputCount > inputBuffer.Length)
  133. throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
  134. if (inputBuffer.Length - inputCount < inputOffset)
  135. throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
  136. this.State = 1;
  137. this.HashCore(inputBuffer, inputOffset, inputCount);
  138. if (outputBuffer != null && (inputBuffer != outputBuffer || inputOffset != outputOffset))
  139. Buffer.BlockCopy((Array) inputBuffer, inputOffset, (Array) outputBuffer, outputOffset, inputCount);
  140. return inputCount;
  141. }
  142.  
  143. public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
  144. {
  145. if (this.m_bDisposed)
  146. throw new ObjectDisposedException((string) null, Environment.GetResourceString("ObjectDisposed_Generic"));
  147. if (inputBuffer == null)
  148. throw new ArgumentNullException(nameof (inputBuffer));
  149. if (inputOffset < 0)
  150. throw new ArgumentOutOfRangeException(nameof (inputOffset), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  151. if (inputCount < 0 || inputCount > inputBuffer.Length)
  152. throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
  153. if (inputBuffer.Length - inputCount < inputOffset)
  154. throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
  155. this.HashCore(inputBuffer, inputOffset, inputCount);
  156. this.HashValue = this.HashFinal();
  157. byte[] numArray = new byte[inputCount];
  158. if (inputCount != 0)
  159. Buffer.InternalBlockCopy((Array) inputBuffer, inputOffset, (Array) numArray, 0, inputCount);
  160. this.State = 0;
  161. return numArray;
  162. }
  163.  
  164. void IDisposable.Dispose()
  165. {
  166. this.Dispose(true);
  167. GC.SuppressFinalize((object) this);
  168. }
  169.  
  170. public void Clear()
  171. {
  172. ((IDisposable) this).Dispose();
  173. }
  174.  
  175. protected virtual void Dispose(bool disposing)
  176. {
  177. if (!disposing)
  178. return;
  179. if (this.HashValue != null)
  180. Array.Clear((Array) this.HashValue, 0, this.HashValue.Length);
  181. this.HashValue = (byte[]) null;
  182. this.m_bDisposed = true;
  183. }
  184.  
  185. public abstract void Initialize();
  186.  
  187. protected abstract void HashCore(byte[] array, int ibStart, int cbSize);
  188.  
  189. protected abstract byte[] HashFinal();
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement