using System;
using System.Configuration.Assemblies;
+using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
RSAParameters p = rsa.ExportParameters (true);
return ((p.D != null) && (p.P != null) && (p.Q != null));
}
- catch {
+ catch (CryptographicException) {
return false;
}
}
// Ok from here - Same structure as keypair - expect for public key
publicKey [12] = 0x06; // PUBLICKEYBLOB
// we can copy this part
- Array.Copy (keyPair, 1, publicKey, 13, publicKey.Length - 13);
+ Buffer.BlockCopy (keyPair, 1, publicKey, 13, publicKey.Length - 13);
// and make a small adjustment
publicKey [23] = 0x31; // (RSA1 not RSA2)
}
- return publicKey;
+ return (byte[]) publicKey.Clone ();
}
}
public byte[] PublicKeyToken {
get {
- if (keyToken != null)
- return keyToken;
- byte[] publicKey = PublicKey;
- if (publicKey == null)
- return null;
- HashAlgorithm ha = SHA1.Create (TokenAlgorithm);
- byte[] hash = ha.ComputeHash (publicKey);
- // we need the last 8 bytes in reverse order
- keyToken = new byte [8];
- Array.Copy (hash, (hash.Length - 8), keyToken, 0, 8);
- Array.Reverse (keyToken, 0, 8);
- return keyToken;
+ if (keyToken == null) {
+ byte[] publicKey = PublicKey;
+ if (publicKey == null)
+ return null;
+ HashAlgorithm ha = SHA1.Create (TokenAlgorithm);
+ byte[] hash = ha.ComputeHash (publicKey);
+ // we need the last 8 bytes in reverse order
+ keyToken = new byte [8];
+ Buffer.BlockCopy (hash, (hash.Length - 8), keyToken, 0, 8);
+ Array.Reverse (keyToken, 0, 8);
+ }
+ return (byte[]) keyToken.Clone ();
}
}
return tokenAlgorithm;
}
set {
- string algo = value.ToUpper ();
+ string algo = value.ToUpper (CultureInfo.InvariantCulture);
if ((algo == "SHA1") || (algo == "MD5")) {
tokenAlgorithm = value;
InvalidateCache ();
{
bool result = false;
StrongNameSignature sn;
- FileStream fs = File.OpenRead (fileName);
- try {
+ using (FileStream fs = File.OpenRead (fileName)) {
sn = StrongHash (fs, StrongNameOptions.Signature);
- if (sn.Hash == null)
- return false;
- }
- finally {
fs.Close ();
}
+ if (sn.Hash == null)
+ return false;
byte[] signature = null;
try {
signature = sign.CreateSignature (sn.Hash);
Array.Reverse (signature);
}
- catch {
+ catch (CryptographicException) {
return false;
}
- try {
- fs = File.OpenWrite (fileName);
+ using (FileStream fs = File.OpenWrite (fileName)) {
fs.Position = sn.SignaturePosition;
fs.Write (signature, 0, signature.Length);
- }
- catch {
- }
- finally {
fs.Close ();
result = true;
}
public bool Verify (string fileName)
{
StrongNameSignature sn;
- FileStream fs = File.OpenRead (fileName);
- try {
+ using (FileStream fs = File.OpenRead (fileName)) {
sn = StrongHash (fs, StrongNameOptions.Signature);
- if (sn.Hash == null)
- return false;
- }
- finally {
fs.Close ();
}
+ if (sn.Hash == null) {
+ Console.WriteLine ("hash is null");
+ return false;
+ }
try {
AssemblyHashAlgorithm algorithm = AssemblyHashAlgorithm.SHA1;
if (tokenAlgorithm == "MD5")
algorithm = AssemblyHashAlgorithm.MD5;
- return Verify (rsa, algorithm, sn.Hash, sn.Signature);
+ bool v = Verify (rsa, algorithm, sn.Hash, sn.Signature);
+ Console.WriteLine ("returning v: " + v);
+ return v;
}
- catch {
+ catch (CryptographicException e) {
// no exception allowed
+ Console.WriteLine ("exception: " + e);
return false;
}
}