Example usage for org.bouncycastle.crypto Signer verifySignature

List of usage examples for org.bouncycastle.crypto Signer verifySignature

Introduction

In this page you can find the example usage for org.bouncycastle.crypto Signer verifySignature.

Prototype

public boolean verifySignature(byte[] signature);

Source Link

Document

return true if the internal state represents the signature described in the passed in array.

Usage

From source file:com.codename1.payments.GooglePlayValidator.java

/**
 * Create JWT token.  See https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
 * @param payload/*from   w ww  .ja  v  a  2s  .  com*/
 * @return 
 */
private String createJWT(String payload) {
    try {
        Map header = new HashMap();
        header.put("alg", "RS256");
        header.put("typ", "JWT");

        Map claims = new HashMap();
        claims.put("iss", getGoogleClientId());
        claims.put("scope", "https://www.googleapis.com/auth/androidpublisher");
        claims.put("aud", "https://www.googleapis.com/oauth2/v4/token");
        claims.put("exp", String.valueOf(System.currentTimeMillis() / 1000l + 1800));
        claims.put("iat", String.valueOf(System.currentTimeMillis() / 1000l));

        String headerEnc = Base64.encodeNoNewline(Result.fromContent(header).toString().getBytes("UTF-8"))
                .replace('+', '-').replace('/', '_').replace("=", " ");
        String claimsEnc = Base64.encodeNoNewline(Result.fromContent(claims).toString().getBytes("UTF-8"))
                .replace('+', '-').replace('/', '_').replace("=", " ");
        ;
        String sigContent = headerEnc + "." + claimsEnc;

        Digest digest = new SHA256Digest();
        Signer signer = new RSADigestSigner(digest);

        String pkey = getGooglePrivateKey();
        RSAPrivateKey rpkey = getRSAPrivateKey(pkey);
        signer.init(true, new RSAKeyParameters(true, rpkey.getModulus(), rpkey.getPrivateExponent()));

        byte[] sigBytes = sigContent.getBytes("UTF-8");
        signer.update(sigBytes, 0, sigBytes.length);

        byte[] sig = signer.generateSignature();

        RSAKeyParameters kp = new RSAKeyParameters(false, rpkey.getModulus(), rpkey.getPublicExponent());
        signer.init(false, kp);
        signer.update(sigBytes, 0, sigBytes.length);
        boolean res = signer.verifySignature(sig);
        if (!res) {
            throw new RuntimeException("Failed to verify signature after creating it");
        }

        String jwt = headerEnc + "." + claimsEnc + "."
                + Base64.encodeNoNewline(sig).replace('+', '-').replace('/', '_').replace("=", " ");
        ;
        return jwt;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

}

From source file:org.opcfoundation.ua.transport.security.BcCryptoProvider.java

License:Open Source License

@Override
public boolean verifyAsymm(PublicKey signingCertificate, SecurityAlgorithm algorithm, byte[] dataToVerify,
        byte[] signature) throws ServiceResultException {
    if (algorithm == null)
        return true;
    if (signingCertificate == null || dataToVerify == null || signature == null)
        throw new IllegalArgumentException("null arg");

    java.security.interfaces.RSAPublicKey signingCertificateRSA = (java.security.interfaces.RSAPublicKey) signingCertificate;
    RSAPublicKey publicKey = new RSAPublicKey(signingCertificateRSA.getModulus(),
            signingCertificateRSA.getPublicExponent());
    Signer signer = getAsymmetricSigner(false, algorithm, publicKey);
    signer.update(dataToVerify, 0, dataToVerify.length);
    return signer.verifySignature(signature);

}