Skip to content

Instantly share code, notes, and snippets.

@jamesidw
Last active December 2, 2022 07:49
Show Gist options
  • Save jamesidw/4c5c872d8a7c250df897fa8b1940e36e to your computer and use it in GitHub Desktop.
Save jamesidw/4c5c872d8a7c250df897fa8b1940e36e to your computer and use it in GitHub Desktop.
Sign a message
String getSignature(String jsonBody) throws IOException {
try (FileInputStream ksFile = new FileInputStream(keyStorePath)) {
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(ksFile, keyPassword.toCharArray());
// there may be more than one key in the keystore...here we are picking the 'dev' key
var pk = ks.getKey("dev", keyPassword.toCharArray());
Signature ps = Signature.getInstance("SHA256withRSA");
ps.initSign((PrivateKey) pk);
ps.update(jsonBody.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(ps.sign());
} catch (KeyStoreException | SignatureException | NoSuchAlgorithmException |
CertificateException | InvalidKeyException | UnrecoverableKeyException e) {
Log.error("signature generation failed", e);
throw new RuntimeException("signature failure");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment