Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tobiasviehweger/3f9f6fbe6c169be09a3d0b0879878c3f to your computer and use it in GitHub Desktop.
Save tobiasviehweger/3f9f6fbe6c169be09a3d0b0879878c3f to your computer and use it in GitHub Desktop.
Creating file hash + signature with OpenSSL:
openssl dgst -sha256 -binary <infile> > <infile>.hash
openssl rsautl -sign -inkey somekey.pfx -keyform pkcs12 -passin pass:<password> -in <infile>.hash > <infile>.sig
rm <infile>.hash
Validating the signature from C#, using both <infile> and <infile>.sig
var cert = new X509Certificate2( .. somesource .. );
var csp = (RSACryptoServiceProvider) cert.PublicKey.Key;
var sha = new SHA256Managed();
var hash = sha.ComputeHash(File.Open(<infile>, FileMode.Open));
var signature = File.ReadAllBytes(<infile>.sig);
bool isValid = csp.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA256"), signature);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment