Added check and failover of hash algorithms: OpenSSL > Cng > Default
[pithos-ms-client] / trunk / Pithos.Network / SHA1OpenSSL.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Linq;\r
4 using System.Security.Cryptography;\r
5 using System.Text;\r
6 using OpenSSL.Crypto;\r
7 \r
8 namespace Pithos.Network\r
9 {\r
10     public class SHA1OpenSSL : SHA1\r
11     {\r
12 \r
13         private MessageDigestContext _context;\r
14 \r
15         public SHA1OpenSSL()\r
16         {\r
17             _context = new MessageDigestContext(MessageDigest.CreateByName("SHA1"));\r
18             _context.Init();\r
19         }\r
20 \r
21         public override void Initialize()\r
22         {\r
23             _context.Init();\r
24         }\r
25 \r
26         protected override void HashCore(byte[] array, int ibStart, int cbSize)\r
27         {\r
28             if (array.Length == cbSize)\r
29                 _context.Update(array);\r
30             else\r
31             {\r
32                 var block = new byte[cbSize];\r
33                 Buffer.BlockCopy(array, ibStart, block, 0, cbSize);\r
34                 _context.Update(block);\r
35             }\r
36         }\r
37 \r
38         protected override byte[] HashFinal()\r
39         {\r
40             return _context.DigestFinal();\r
41         }\r
42 \r
43         protected override void Dispose(bool disposing)\r
44         {\r
45             base.Dispose(disposing);\r
46             if (disposing)\r
47             {\r
48                 if (_context != null)\r
49                     _context.Dispose();\r
50                 _context = null;\r
51             }\r
52         }\r
53     }\r
54     \r
55     public class SHA256OpenSSL : SHA256\r
56     {\r
57 \r
58         private MessageDigestContext _context;\r
59 \r
60         public SHA256OpenSSL()\r
61         {\r
62             _context = new MessageDigestContext(MessageDigest.CreateByName("SHA256"));\r
63             _context.Init();\r
64         }\r
65 \r
66         public override void Initialize()\r
67         {\r
68             _context.Init();\r
69         }\r
70 \r
71         protected override void HashCore(byte[] array, int ibStart, int cbSize)\r
72         {\r
73             if (array.Length == cbSize)\r
74                 _context.Update(array);\r
75             else\r
76             {\r
77                 var block = new byte[cbSize];\r
78                 Buffer.BlockCopy(array, ibStart, block, 0, cbSize);\r
79                 _context.Update(block);\r
80             }\r
81         }\r
82 \r
83         protected override byte[] HashFinal()\r
84         {\r
85             return _context.DigestFinal();\r
86         }\r
87 \r
88         protected override void Dispose(bool disposing)\r
89         {\r
90             base.Dispose(disposing);\r
91             if (disposing)\r
92             {\r
93                 if (_context != null)\r
94                     _context.Dispose();\r
95                 _context = null;\r
96             }\r
97         }\r
98     }\r
99 }\r