Merge pull request #924 from marcusva/master
[mono.git] / mcs / class / Mono.Security / Mono.Security.Cryptography / MD5SHA1.cs
1
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.
21 //
22 /* Transport Security Layer (TLS)
23  * Copyright (c) 2003-2004 Carlos Guzman Alvarez
24  * 
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:
32  * 
33  * The above copyright notice and this permission notice shall be included 
34  * in all copies or substantial portions of the Software.
35  * 
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.
44  */
45
46 using System;
47 using System.Security.Cryptography;
48
49 using Mono.Security.Protocol.Tls;
50
51 namespace Mono.Security.Cryptography
52 {
53         internal class MD5SHA1 : HashAlgorithm
54         {
55                 #region Fields
56
57                 private HashAlgorithm   md5;
58                 private HashAlgorithm   sha;
59                 private bool                    hashing;
60
61                 #endregion
62
63                 #region Constructors
64
65                 public MD5SHA1() : base()
66                 {
67                         this.md5 = MD5.Create();
68                         this.sha = SHA1.Create();
69
70                         // Set HashSizeValue
71                         this.HashSizeValue = this.md5.HashSize + this.sha.HashSize;
72                 }
73
74                 #endregion
75
76                 #region Methods
77
78                 public override void Initialize()
79                 {
80                         this.md5.Initialize();
81                         this.sha.Initialize();
82                         this.hashing = false;
83                 }
84
85                 protected override byte[] HashFinal()
86                 {
87                         if (!hashing)
88                         {
89                                 this.hashing = true;
90                         }
91                         // Finalize the original hash
92                         this.md5.TransformFinalBlock(new byte[0], 0, 0);
93                         this.sha.TransformFinalBlock(new byte[0], 0, 0);
94
95                         byte[] hash = new byte[36];
96
97                         Buffer.BlockCopy(this.md5.Hash, 0, hash, 0, 16);
98                         Buffer.BlockCopy(this.sha.Hash, 0, hash, 16, 20);
99
100                         return hash;
101                 }
102
103                 protected override void HashCore(
104                         byte[] array,
105                         int ibStart,
106                         int cbSize)
107                 {
108                         if (!hashing)
109                         {
110                                 hashing = true;
111                         }
112                         this.md5.TransformBlock(array, ibStart, cbSize, array, ibStart);
113                         this.sha.TransformBlock(array, ibStart, cbSize, array, ibStart);
114                 }
115
116                 public byte[] CreateSignature(RSA rsa) 
117                 {
118                         if (rsa == null)
119                         {
120                                 throw new CryptographicUnexpectedOperationException ("missing key");
121                         }
122
123                         RSASslSignatureFormatter f = new RSASslSignatureFormatter(rsa);
124                         f.SetHashAlgorithm("MD5SHA1");
125
126                         return f.CreateSignature(this.Hash);
127                 }
128
129                 public bool VerifySignature(RSA rsa, byte[] rgbSignature) 
130                 {
131                         if (rsa == null)
132                         {
133                                 throw new CryptographicUnexpectedOperationException ("missing key");
134                         }
135                         if (rgbSignature == null)
136                         {
137                                 throw new ArgumentNullException ("rgbSignature");
138                         }
139
140                         RSASslSignatureDeformatter d = new RSASslSignatureDeformatter(rsa);
141                         d.SetHashAlgorithm("MD5SHA1");
142
143                         return d.VerifySignature(this.Hash, rgbSignature);
144                 }
145
146                 #endregion
147         }
148 }