Converted NetSparkle project to .NET 4 Client Profile
[pithos-ms-client] / trunk / NetSparkle / NetSparkleDSAVerificator.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using System.Reflection;
7 using System.Security.Cryptography;
8
9 namespace AppLimit.NetSparkle
10 {
11     public class NetSparkleDSAVerificator
12     {
13         private DSACryptoServiceProvider _provider;
14
15         public static Boolean ExistsPublicKey(String publicKey)
16         {
17                 // 1. try to load this from resource
18             Stream data = TryGetResourceStream(publicKey);
19             if (data == null )
20                 data = TryGetFileResource(publicKey, data);
21
22             // 2. check the resource
23             if (data == null)
24                 return false;
25             else
26                 return true;
27         }
28
29         public NetSparkleDSAVerificator(String publicKey)
30         {
31             // 1. try to load this from resource
32             Stream data = TryGetResourceStream(publicKey);
33             if (data == null )
34                 data = TryGetFileResource(publicKey, data);
35
36             // 2. check the resource
37             if ( data == null )
38                 throw new Exception("Couldn't find public key for verification");
39
40             // 3. read out the key value
41             using (StreamReader reader = new StreamReader(data))
42             {
43                     String key = reader.ReadToEnd();
44                     _provider = new DSACryptoServiceProvider();
45                     _provider.FromXmlString(key);
46             }            
47         }
48
49         public Boolean VerifyDSASignature(String signature, String binaryPath)
50         {
51             if (_provider == null)
52                 return false;
53
54             // convert signature
55             Byte[] bHash = Convert.FromBase64String(signature);
56
57             // read the data
58             byte[] bData = null;
59             using (Stream inputStream = File.OpenRead(binaryPath))
60             {
61                 bData = new Byte[inputStream.Length];
62                 inputStream.Read(bData, 0, bData.Length);
63             }
64             
65             // verify
66             return _provider.VerifyData(bData, bHash);            
67         }
68
69         private static Stream TryGetFileResource(String publicKey, Stream data)
70         {
71             if (File.Exists(publicKey))
72             {
73                 data = File.OpenRead(publicKey);
74             }
75             return data;
76         }
77
78         private static Stream TryGetResourceStream(String publicKey)
79         {
80             Stream data = null;
81
82             foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
83             {
84                 var resourceName = asm.GetManifestResourceNames().FirstOrDefault(s => s.IndexOf(publicKey, StringComparison.OrdinalIgnoreCase) > -1);
85                 if (!string.IsNullOrEmpty(resourceName))
86                 {
87                   data = asm.GetManifestResourceStream(resourceName);
88                   if (data != null)
89                     break;
90                 }
91             }
92             return data;
93         }
94     }
95 }