Search in sources :

Example 1 with Base64

use of com.bluenimble.platform.crypto.Base64 in project serverless by bluenimble.

the class SimpleSigner method signWithKey.

private void signWithKey(SecureDocument doc, SecretKey key) throws SignerException {
    try {
        byte[] signature = null;
        if (key.getAlgorithm().equals(Algorithm.HmacSHA1.getId())) {
            Mac mac = Mac.getInstance(key.getAlgorithm());
            mac.init(key);
            signature = doc.getBytes();
            if (SignatureAware.class.isAssignableFrom(doc.getClass())) {
                ((SignatureAware) doc).setSignature(new Base64().encode(mac.doFinal(signature)));
            } else {
                doc.setBytes(mac.doFinal(signature));
            }
        } else {
            MessageDigest md = MessageDigest.getInstance(key.getAlgorithm());
            signature = md.digest(doc.getBytes());
            if (SignatureAware.class.isAssignableFrom(doc.getClass())) {
                ((SignatureAware) doc).setSignature(hexEncode(signature));
            } else {
                doc.setBytes(hexEncode(signature));
            }
        }
    } catch (Throwable th) {
        throw new SignerException(th, th.getMessage());
    }
}
Also used : Base64(com.bluenimble.platform.crypto.Base64) SignatureAware(com.bluenimble.platform.crypto.SignatureAware) MessageDigest(java.security.MessageDigest) SignerException(com.bluenimble.platform.crypto.signer.SignerException) Mac(javax.crypto.Mac)

Aggregations

Base64 (com.bluenimble.platform.crypto.Base64)1 SignatureAware (com.bluenimble.platform.crypto.SignatureAware)1 SignerException (com.bluenimble.platform.crypto.signer.SignerException)1 MessageDigest (java.security.MessageDigest)1 Mac (javax.crypto.Mac)1