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