Last active
December 2, 2022 07:49
-
-
Save jamesidw/4c5c872d8a7c250df897fa8b1940e36e to your computer and use it in GitHub Desktop.
Sign a message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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