MessageDigest.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Security.Cryptography;
  5. namespace SharpCifs.Util.Sharpen
  6. {
  7. public abstract class MessageDigest
  8. {
  9. public void Digest(byte[] buffer, int o, int len)
  10. {
  11. byte[] d = Digest();
  12. d.CopyTo(buffer, o);
  13. }
  14. public byte[] Digest(byte[] buffer)
  15. {
  16. Update(buffer);
  17. return Digest();
  18. }
  19. public abstract byte[] Digest();
  20. public abstract int GetDigestLength();
  21. public static MessageDigest GetInstance(string algorithm)
  22. {
  23. switch (algorithm.ToLower())
  24. {
  25. case "sha-1":
  26. //System.Security.CryptographySHA1Managed not found
  27. //return new MessageDigest<SHA1Managed> ();
  28. return new MessageDigest<System.Security.Cryptography.SHA1>();
  29. case "md5":
  30. return new MessageDigest<Md5Managed>();
  31. }
  32. throw new NotSupportedException(
  33. string.Format("The requested algorithm \"{0}\" is not supported.", algorithm));
  34. }
  35. public abstract void Reset();
  36. public abstract void Update(byte[] b);
  37. public abstract void Update(byte b);
  38. public abstract void Update(byte[] b, int offset, int len);
  39. }
  40. public class MessageDigest<TAlgorithm>
  41. : MessageDigest where TAlgorithm : HashAlgorithm //, new() //use static `Create` method
  42. {
  43. private TAlgorithm _hash;
  44. //private CryptoStream _stream; //don't work .NET Core
  45. private MemoryStream _stream;
  46. public MessageDigest()
  47. {
  48. Init();
  49. }
  50. public override byte[] Digest()
  51. {
  52. //CryptoStream -> MemoryStream, needless method
  53. //_stream.FlushFinalBlock ();
  54. //HashAlgorithm.`Hash` property deleted
  55. //byte[] hash = _hash.Hash;
  56. byte[] hash = _hash.ComputeHash(_stream.ToArray());
  57. Reset();
  58. return hash;
  59. }
  60. public void Dispose()
  61. {
  62. if (_stream != null)
  63. {
  64. _stream.Dispose();
  65. }
  66. _stream = null;
  67. }
  68. public override int GetDigestLength()
  69. {
  70. return (_hash.HashSize / 8);
  71. }
  72. private void Init()
  73. {
  74. //use static `Create` method
  75. //_hash = Activator.CreateInstance<TAlgorithm> ();
  76. var createMethod = typeof(TAlgorithm).GetRuntimeMethod("Create", new Type[0]);
  77. _hash = (TAlgorithm)createMethod.Invoke(null, new object[] { });
  78. //HashAlgorithm cannot cast `ICryptoTransform` on .NET Core, gave up using CryptoStream.
  79. //_stream = new CryptoStream(Stream.Null, _hash, CryptoStreamMode.Write);
  80. //_stream = new CryptoStream(_tmpStream, (ICryptoTransform)_hash, CryptoStreamMode.Write);
  81. _stream = new MemoryStream();
  82. }
  83. public override void Reset()
  84. {
  85. Dispose();
  86. Init();
  87. }
  88. public override void Update(byte[] input)
  89. {
  90. _stream.Write(input, 0, input.Length);
  91. }
  92. public override void Update(byte input)
  93. {
  94. _stream.WriteByte(input);
  95. }
  96. public override void Update(byte[] input, int index, int count)
  97. {
  98. if (count < 0)
  99. Console.WriteLine("Argh!");
  100. _stream.Write(input, index, count);
  101. }
  102. }
  103. }