MD5Managed.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //Copyright (c) Microsoft Corporation. All rights reserved.
  2. using System;
  3. using System.Security.Cryptography;
  4. // **************************************************************
  5. // * Raw implementation of the MD5 hash algorithm
  6. // * from RFC 1321.
  7. // *
  8. // * Written By: Reid Borsuk and Jenny Zheng
  9. // * Copyright (c) Microsoft Corporation. All rights reserved.
  10. // **************************************************************
  11. #if SILVERLIGHT
  12. #else
  13. //public class MD5Managed : MD5
  14. #endif
  15. namespace SharpCifs.Util.Sharpen
  16. {
  17. public class Md5Managed : HashAlgorithm
  18. {
  19. public static Md5Managed Create()
  20. {
  21. return new Md5Managed();
  22. }
  23. private byte[] _data;
  24. private AbcdStruct _abcd;
  25. private Int64 _totalLength;
  26. private int _dataSize;
  27. public Md5Managed()
  28. {
  29. //field cannot access
  30. //HashSizeValue = 0x80;
  31. Initialize();
  32. }
  33. public override void Initialize()
  34. {
  35. _data = new byte[64];
  36. _dataSize = 0;
  37. _totalLength = 0;
  38. _abcd = new AbcdStruct();
  39. //Intitial values as defined in RFC 1321
  40. _abcd.A = 0x67452301;
  41. _abcd.B = 0xefcdab89;
  42. _abcd.C = 0x98badcfe;
  43. _abcd.D = 0x10325476;
  44. }
  45. protected override void HashCore(byte[] array, int ibStart, int cbSize)
  46. {
  47. int startIndex = ibStart;
  48. int totalArrayLength = _dataSize + cbSize;
  49. if (totalArrayLength >= 64)
  50. {
  51. Array.Copy(array, startIndex, _data, _dataSize, 64 - _dataSize);
  52. // Process message of 64 bytes (512 bits)
  53. Md5Core.GetHashBlock(_data, ref _abcd, 0);
  54. startIndex += 64 - _dataSize;
  55. totalArrayLength -= 64;
  56. while (totalArrayLength >= 64)
  57. {
  58. Array.Copy(array, startIndex, _data, 0, 64);
  59. Md5Core.GetHashBlock(array, ref _abcd, startIndex);
  60. totalArrayLength -= 64;
  61. startIndex += 64;
  62. }
  63. _dataSize = totalArrayLength;
  64. Array.Copy(array, startIndex, _data, 0, totalArrayLength);
  65. }
  66. else
  67. {
  68. Array.Copy(array, startIndex, _data, _dataSize, cbSize);
  69. _dataSize = totalArrayLength;
  70. }
  71. _totalLength += cbSize;
  72. }
  73. protected override byte[] HashFinal()
  74. {
  75. //field cannot access
  76. //HashValue = Md5Core.GetHashFinalBlock(_data, 0, _dataSize, _abcd, _totalLength * 8);
  77. //return HashValue;
  78. return Md5Core.GetHashFinalBlock(_data, 0, _dataSize, _abcd, _totalLength * 8);
  79. }
  80. }
  81. }