DigitalSignClientSnippet.java
unknown
java
10 months ago
1.6 kB
4
Indexable
package org.arttha.fruit.common.crypto; import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.Signature; import java.security.SignatureException; import java.security.spec.PKCS8EncodedKeySpec; import java.util.Base64; public class DigitalSignClientSnippet{ private static final String SIGNATURE_ALGO = "SHA256withRSA"; public static void main(String[] args) throws Exception { String keystorePath = "arttha_sandbox_passport.jks"; String keystorePassword = "{kestore_password}"; String key=KeyStoreUtil.getKeyFromKeyStore(keystorePath, keystorePassword, "{alias}", "{key_password}"); PrivateKey privateKey = getPrivateKeyFromBytes(Base64.getDecoder().decode(key)); String requestBody = "{" + "\"userIdentification\": \"john waler\"," + "\"deviceid\":\"77881992\"" + "}"; String encDate = Base64.getEncoder().encodeToString(sign(requestBody, privateKey)); System.out.println(encDate); } public static byte[] sign(String requestData, PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException { Signature signature = Signature.getInstance(SIGNATURE_ALGO); signature.initSign(privateKey); signature.update(requestData.getBytes()); return signature.sign(); } public static PrivateKey getPrivateKeyFromBytes(byte[] privateKeyBytes) throws Exception { PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePrivate(keySpec); } }
Editor is loading...
Leave a Comment