Skip to content

Instantly share code, notes, and snippets.

@anoopsankar
Last active December 11, 2015 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anoopsankar/4594587 to your computer and use it in GitHub Desktop.
Save anoopsankar/4594587 to your computer and use it in GitHub Desktop.
PKCS#1 digital signature
private byte[] sign(byte[] data, PrivateKey privateKey) throws GeneralSecurityException {
Security.addProvider(new BouncyCastleProvider());
Signature signer = Signature.getInstance("SHA1withRSA", "BC");
signer.initSign(privateKey);
signer.update(data);
return signer.sign();
}
public byte[] signWithExternDigest(byte[] data, PrivateKey privateKey) throws GeneralSecurityException, IOException {
AlgorithmIdentifier sha1AlgoId = new AlgorithmIdentifier(new ASN1ObjectIdentifier("1.3.14.3.2.26"));
DigestInfo digestInfo = new DigestInfo(sha1AlgoId, data);
byte[] paddedDigest = digestInfo.getEncoded("DER");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(paddedDigest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment