HMACT64.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. using SharpCifs.Util.Sharpen;
  19. namespace SharpCifs.Util
  20. {
  21. /// <summary>This is an implementation of the HMACT64 keyed hashing algorithm.</summary>
  22. /// <remarks>
  23. /// This is an implementation of the HMACT64 keyed hashing algorithm.
  24. /// HMACT64 is defined by Luke Leighton as a modified HMAC-MD5 (RFC 2104)
  25. /// in which the key is truncated at 64 bytes (rather than being hashed
  26. /// via MD5).
  27. /// </remarks>
  28. public class Hmact64 : MessageDigest
  29. {
  30. private const int BlockLength = 64;
  31. private const byte Ipad = unchecked(unchecked(0x36));
  32. private const byte Opad = unchecked(unchecked(0x5c));
  33. private MessageDigest _md5;
  34. private byte[] _ipad = new byte[BlockLength];
  35. private byte[] _opad = new byte[BlockLength];
  36. /// <summary>Creates an HMACT64 instance which uses the given secret key material.</summary>
  37. /// <remarks>Creates an HMACT64 instance which uses the given secret key material.</remarks>
  38. /// <param name="key">The key material to use in hashing.</param>
  39. public Hmact64(byte[] key)
  40. {
  41. int length = Math.Min(key.Length, BlockLength);
  42. for (int i = 0; i < length; i++)
  43. {
  44. _ipad[i] = unchecked((byte)(key[i] ^ Ipad));
  45. _opad[i] = unchecked((byte)(key[i] ^ Opad));
  46. }
  47. for (int i1 = length; i1 < BlockLength; i1++)
  48. {
  49. _ipad[i1] = Ipad;
  50. _opad[i1] = Opad;
  51. }
  52. try
  53. {
  54. _md5 = GetInstance("MD5");
  55. }
  56. catch (Exception ex)
  57. {
  58. throw new InvalidOperationException(ex.Message);
  59. }
  60. EngineReset();
  61. }
  62. protected byte[] EngineDigest()
  63. {
  64. byte[] digest = _md5.Digest();
  65. _md5.Update(_opad);
  66. return _md5.Digest(digest);
  67. }
  68. protected int EngineDigest(byte[] buf, int offset, int len)
  69. {
  70. byte[] digest = _md5.Digest();
  71. _md5.Update(_opad);
  72. _md5.Update(digest);
  73. try
  74. {
  75. _md5.Digest(buf, offset, len);
  76. return len;
  77. }
  78. catch (Exception)
  79. {
  80. throw new InvalidOperationException();
  81. }
  82. }
  83. protected int EngineGetDigestLength()
  84. {
  85. return _md5.GetDigestLength();
  86. }
  87. protected void EngineReset()
  88. {
  89. _md5.Reset();
  90. _md5.Update(_ipad);
  91. }
  92. protected void EngineUpdate(byte b)
  93. {
  94. _md5.Update(b);
  95. }
  96. protected void EngineUpdate(byte[] input, int offset, int len)
  97. {
  98. _md5.Update(input, offset, len);
  99. }
  100. public override byte[] Digest()
  101. {
  102. return EngineDigest();
  103. }
  104. public override int GetDigestLength()
  105. {
  106. return EngineGetDigestLength();
  107. }
  108. public override void Reset()
  109. {
  110. EngineReset();
  111. }
  112. public override void Update(byte[] b)
  113. {
  114. EngineUpdate(b, 0, b.Length);
  115. }
  116. public override void Update(byte b)
  117. {
  118. EngineUpdate(b);
  119. }
  120. public override void Update(byte[] b, int offset, int len)
  121. {
  122. EngineUpdate(b, offset, len);
  123. }
  124. }
  125. }