2 using System.Collections.Generic;
6 using System.Reflection;
7 using System.Security.Cryptography;
9 namespace AppLimit.NetSparkle
11 public class NetSparkleDSAVerificator
13 private DSACryptoServiceProvider _provider;
15 public static Boolean ExistsPublicKey(String publicKey)
17 // 1. try to load this from resource
18 Stream data = TryGetResourceStream(publicKey);
20 data = TryGetFileResource(publicKey, data);
22 // 2. check the resource
29 public NetSparkleDSAVerificator(String publicKey)
31 // 1. try to load this from resource
32 Stream data = TryGetResourceStream(publicKey);
34 data = TryGetFileResource(publicKey, data);
36 // 2. check the resource
38 throw new Exception("Couldn't find public key for verification");
40 // 3. read out the key value
41 using (StreamReader reader = new StreamReader(data))
43 String key = reader.ReadToEnd();
44 _provider = new DSACryptoServiceProvider();
45 _provider.FromXmlString(key);
49 public Boolean VerifyDSASignature(String signature, String binaryPath)
51 if (_provider == null)
55 Byte[] bHash = Convert.FromBase64String(signature);
59 using (Stream inputStream = File.OpenRead(binaryPath))
61 bData = new Byte[inputStream.Length];
62 inputStream.Read(bData, 0, bData.Length);
66 return _provider.VerifyData(bData, bHash);
69 private static Stream TryGetFileResource(String publicKey, Stream data)
71 if (File.Exists(publicKey))
73 data = File.OpenRead(publicKey);
78 private static Stream TryGetResourceStream(String publicKey)
82 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
84 var resourceName = asm.GetManifestResourceNames().FirstOrDefault(s => s.IndexOf(publicKey, StringComparison.OrdinalIgnoreCase) > -1);
85 if (!string.IsNullOrEmpty(resourceName))
87 data = asm.GetManifestResourceStream(resourceName);