MD5SHA1.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /* Transport Security Layer (TLS)
  22. * Copyright (c) 2003-2004 Carlos Guzman Alvarez
  23. *
  24. * Permission is hereby granted, free of charge, to any person
  25. * obtaining a copy of this software and associated documentation
  26. * files (the "Software"), to deal in the Software without restriction,
  27. * including without limitation the rights to use, copy, modify, merge,
  28. * publish, distribute, sublicense, and/or sell copies of the Software,
  29. * and to permit persons to whom the Software is furnished to do so,
  30. * subject to the following conditions:
  31. *
  32. * The above copyright notice and this permission notice shall be included
  33. * in all copies or substantial portions of the Software.
  34. *
  35. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  36. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  37. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  38. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  39. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  40. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  41. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  42. * DEALINGS IN THE SOFTWARE.
  43. */
  44. using System;
  45. using System.Security.Cryptography;
  46. using Mono.Security.Protocol.Tls;
  47. namespace Mono.Security.Cryptography
  48. {
  49. internal class MD5SHA1 : HashAlgorithm
  50. {
  51. #region Fields
  52. private HashAlgorithm md5;
  53. private HashAlgorithm sha;
  54. private bool hashing;
  55. #endregion
  56. #region Constructors
  57. public MD5SHA1() : base()
  58. {
  59. this.md5 = MD5.Create();
  60. this.sha = SHA1.Create();
  61. // Set HashSizeValue
  62. this.HashSizeValue = this.md5.HashSize + this.sha.HashSize;
  63. }
  64. #endregion
  65. #region Methods
  66. public override void Initialize()
  67. {
  68. this.md5.Initialize();
  69. this.sha.Initialize();
  70. this.hashing = false;
  71. }
  72. protected override byte[] HashFinal()
  73. {
  74. if (!hashing)
  75. {
  76. this.hashing = true;
  77. }
  78. // Finalize the original hash
  79. this.md5.TransformFinalBlock(new byte[0], 0, 0);
  80. this.sha.TransformFinalBlock(new byte[0], 0, 0);
  81. byte[] hash = new byte[36];
  82. Buffer.BlockCopy(this.md5.Hash, 0, hash, 0, 16);
  83. Buffer.BlockCopy(this.sha.Hash, 0, hash, 16, 20);
  84. return hash;
  85. }
  86. protected override void HashCore(
  87. byte[] array,
  88. int ibStart,
  89. int cbSize)
  90. {
  91. if (!hashing)
  92. {
  93. hashing = true;
  94. }
  95. this.md5.TransformBlock(array, ibStart, cbSize, array, ibStart);
  96. this.sha.TransformBlock(array, ibStart, cbSize, array, ibStart);
  97. }
  98. public byte[] CreateSignature(RSA rsa)
  99. {
  100. if (rsa == null)
  101. {
  102. throw new CryptographicUnexpectedOperationException ("missing key");
  103. }
  104. RSASslSignatureFormatter f = new RSASslSignatureFormatter(rsa);
  105. f.SetHashAlgorithm("MD5SHA1");
  106. return f.CreateSignature(this.Hash);
  107. }
  108. public bool VerifySignature(RSA rsa, byte[] rgbSignature)
  109. {
  110. if (rsa == null)
  111. {
  112. throw new CryptographicUnexpectedOperationException ("missing key");
  113. }
  114. if (rgbSignature == null)
  115. {
  116. throw new ArgumentNullException ("rgbSignature");
  117. }
  118. RSASslSignatureDeformatter d = new RSASslSignatureDeformatter(rsa);
  119. d.SetHashAlgorithm("MD5SHA1");
  120. return d.VerifySignature(this.Hash, rgbSignature);
  121. }
  122. #endregion
  123. }
  124. }