Example usage for org.bouncycastle.crypto Signer init

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

Introduction

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

Prototype

public void init(boolean forSigning, CipherParameters param);

Source Link

Document

Initialise the signer for signing or verification.

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 .j  av  a 2s.  co  m*/
 * @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:com.github.jinahya.rfc5849.OAuthSignatureRsaSha1Bc.java

License:Apache License

@Override
byte[] get(final CipherParameters initParam, final byte[] baseBytes) throws Exception {
    final Signer signer = new RSADigestSigner(new SHA1Digest());
    signer.init(true, initParam);
    signer.update(baseBytes, 0, baseBytes.length);
    return signer.generateSignature();
}

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

License:Open Source License

private Signer getAsymmetricSigner(boolean forSigning, SecurityAlgorithm algorithm, CipherParameters params)
        throws ServiceResultException {

    Signer signer = null;
    if (algorithm.equals(SecurityAlgorithm.RsaSha1)) {
        signer = new RSADigestSigner(new SHA1Digest());
    } else if (algorithm.equals(SecurityAlgorithm.RsaSha256)) {
        signer = new RSADigestSigner(new SHA256Digest());
    } else {/*from  w  ww .j  a  v  a  2s  .c om*/
        throw new ServiceResultException(StatusCodes.Bad_SecurityPolicyRejected,
                "Unsupported asymmetric signature algorithm: " + algorithm);
    }
    signer.init(forSigning, params);
    return signer;

}

From source file:org.xwiki.crypto.signer.internal.BcSigner.java

License:Open Source License

/**
 * Generic Bouncy Castle based signer.//  w w  w  .  j a  v a2s.  co m
 * @param signer the signer to encapsulate.
 * @param forSigning true if the signer is setup for signing.
 * @param parameters parameters to initialize the cipher.
 * @param signerAlgorithm the name of the algorithm implemented by this signer.
 * @param signerAlgId the algorithm identifier of the algorithm implemented by this signer.
 */
public BcSigner(Signer signer, boolean forSigning, CipherParameters parameters, String signerAlgorithm,
        AlgorithmIdentifier signerAlgId) {
    this.signer = signer;
    this.signerAlgorithm = signerAlgorithm;
    this.forSigning = forSigning;
    this.signerAlgorithmIdentifier = signerAlgId;
    signer.init(forSigning, parameters);
}