KeyedHashAlgorithm.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // KeyedHashAlgorithm.cs: Handles keyed hash and MAC classes.
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System;
  10. using System.Security.Cryptography;
  11. namespace System.Security.Cryptography {
  12. public abstract class KeyedHashAlgorithm : HashAlgorithm {
  13. protected byte[] KeyValue;
  14. protected KeyedHashAlgorithm () : base ()
  15. {
  16. // create a random 64 bits key
  17. }
  18. ~KeyedHashAlgorithm ()
  19. {
  20. Dispose (false);
  21. }
  22. public virtual byte[] Key {
  23. get {
  24. return (byte[]) KeyValue.Clone ();
  25. }
  26. set {
  27. // can't change the key during a hashing ops
  28. if (State != 0)
  29. throw new CryptographicException ();
  30. // zeroize current key material for security
  31. ZeroizeKey ();
  32. // copy new key
  33. KeyValue = (byte[]) value.Clone ();
  34. }
  35. }
  36. protected override void Dispose (bool disposing)
  37. {
  38. // zeroize key material for security
  39. ZeroizeKey();
  40. // dispose managed resources
  41. // none so far
  42. // dispose unmanaged resources
  43. // none so far
  44. // calling base class HashAlgorithm
  45. base.Dispose (disposing);
  46. }
  47. private void ZeroizeKey()
  48. {
  49. if (KeyValue != null)
  50. Array.Clear (KeyValue, 0, KeyValue.Length);
  51. }
  52. public static new KeyedHashAlgorithm Create ()
  53. {
  54. return Create ("System.Security.Cryptography.KeyedHashAlgorithm");
  55. }
  56. public static new KeyedHashAlgorithm Create (string algName)
  57. {
  58. return (KeyedHashAlgorithm) CryptoConfig.CreateFromName (algName);
  59. }
  60. }
  61. }