Example usage for java.security Signature initVerify

List of usage examples for java.security Signature initVerify

Introduction

In this page you can find the example usage for java.security Signature initVerify.

Prototype

public final void initVerify(Certificate certificate) throws InvalidKeyException 

Source Link

Document

Initializes this object for verification, using the public key from the given certificate.

Usage

From source file:org.mitre.openid.connect.client.AbstractOIDCAuthenticationFilter.java

/**
 * Verifies the signature text against the data
 * /*from  w w w . j  av  a 2  s .c  o  m*/
 * @param data
 *            The data
 * @param sigText
 *            The signature text
 * @return True if valid, false if not
 */
public static boolean verify(Signature signer, PublicKey publicKey, String data, String sigText) {

    try {
        signer.initVerify(publicKey);
        signer.update(data.getBytes("UTF-8"));

        byte[] sigBytes = Base64.decodeBase64(sigText);

        return signer.verify(sigBytes);

    } catch (GeneralSecurityException generalSecurityException) {

        // generalSecurityException.printStackTrace();

        throw new IllegalStateException(generalSecurityException);

    } catch (UnsupportedEncodingException unsupportedEncodingException) {

        // unsupportedEncodingException.printStackTrace();

        throw new IllegalStateException(unsupportedEncodingException);

    }
}

From source file:com.orange.oidc.tim.service.KryptoUtils.java

static public boolean verifyJWS(String s, String algorithm, PublicKey pubKey, PrivateKey privKey) {
    // algorithm = "SHA256withRSA";
    // algorithm = "SHA1withRSA";

    boolean bverify = false;

    String parts[] = s.split("\\.");
    if (parts == null || parts.length != 3)
        return bverify;

    try {//from  w  ww  . j a  va2  s . c  o m
        if ("RS256".compareTo(algorithm) == 0)
            algorithm = "SHA256withRSA";
        Signature signature = Signature.getInstance(algorithm, "SC");
        signature.initVerify(pubKey);
        signature.update((parts[0] + "." + parts[1]).getBytes());
        bverify = signature.verify(decodeB64(parts[2]));

        Log.d("verifyJWS", "payload: " + new String(decodeB64(parts[1])));
        /*
        // verify signature
        signature.initSign(privKey);
        signature.update((parts[0]+"."+parts[1]).getBytes());
        byte sig[] = signature.sign();
        String sig64 = encodeB64(sig);
        Log.d("verifyJWS","compute: "+sig64);
        Log.d("verifyJWS","SIM    : "+parts[2]);
        */

    } catch (Exception e) {
        e.printStackTrace();
    }

    return bverify;
}

From source file:cn.usually.common.pay.union.sdk.SecureUtil.java

/**
 * ???// w w w  .  jav  a  2s.  c  o m
 * 
 * @param publicKey
 *            
 * @param signData
 *            ???
 * @param srcData
 *            ?
 * @param validateMethod
 *            ??.
 * @return
 * @throws Exception
 */
public static boolean validateSignBySoft(PublicKey publicKey, byte[] signData, byte[] srcData)
        throws Exception {
    Signature st = Signature.getInstance(BC_PROV_ALGORITHM_SHA1RSA);
    st.initVerify(publicKey);
    st.update(srcData);
    return st.verify(signData);
}

From source file:acp.sdk.SecureUtil.java

/**
 * ???/*from ww w .  j  a v  a  2  s .  c  o  m*/
 * 
 * @param publicKey
 *            
 * @param signData
 *            ???
 * @param srcData
 *            ?
 * @param validateMethod
 *            ??.
 * @return
 * @throws Exception
 */
public static boolean validateSignBySoft(PublicKey publicKey, byte[] signData, byte[] srcData)
        throws Exception {
    Signature st = Signature.getInstance(BC_PROV_ALGORITHM_SHA1RSA, "BC");
    st.initVerify(publicKey);
    st.update(srcData);
    return st.verify(signData);
}

From source file:nl.knmi.adaguc.services.oauth2.OAuth2Handler.java

/**
 * RSASSA-PKCS1-V1_5-VERIFY ((n, e), M, S) using SHA-256
 * //from  w w  w  . jav a 2s  . com
 * @param modulus_n
 * @param exponent_e
 * @param signinInput_M
 * @param signature_S
 * @return
 * @throws SignatureException
 * @throws InvalidKeyException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
static boolean RSASSA_PKCS1_V1_5_VERIFY(String modulus_n, String exponent_e, String signinInput_M,
        String signature_S)
        throws SignatureException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
    Debug.println("Starting verification");
    /* RSA SHA-256 RSASSA-PKCS1-V1_5-VERIFY */
    // Modulus (n from https://www.googleapis.com/oauth2/v2/certs)
    String n = modulus_n;
    // Exponent (e from https://www.googleapis.com/oauth2/v2/certs)
    String e = exponent_e;
    // The JWT Signing Input (JWT Header and JWT Payload concatenated with
    // ".")
    byte[] M = signinInput_M.getBytes();
    // Signature (JWT Crypto)
    byte[] S = Base64.decodeBase64(signature_S);

    byte[] modulusBytes = Base64.decodeBase64(n);
    byte[] exponentBytes = Base64.decodeBase64(e);
    BigInteger modulusInteger = new BigInteger(1, modulusBytes);
    BigInteger exponentInteger = new BigInteger(1, exponentBytes);

    RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulusInteger, exponentInteger);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    PublicKey pubKey = fact.generatePublic(rsaPubKey);
    Signature signature = Signature.getInstance("SHA256withRSA");
    signature.initVerify(pubKey);
    signature.update(M);
    boolean isVerified = signature.verify(S);
    Debug.println("Verify result [" + isVerified + "]");
    return isVerified;
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static boolean verifySignatureES256(byte[] signingInput, byte[] sigBytes, X509Certificate cert)
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    PublicKey publicKey = cert.getPublicKey();

    Signature signature = Signature.getInstance("SHA256WITHECDSA", "BC");
    signature.initVerify(publicKey);
    signature.update(signingInput);/*from w  w w . j  a  v a  2s  .com*/
    return signature.verify(sigBytes);
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static boolean verifySignatureES384(byte[] signingInput, byte[] sigBytes, X509Certificate cert)
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    PublicKey publicKey = cert.getPublicKey();

    Signature signature = Signature.getInstance("SHA384WITHECDSA", "BC");
    signature.initVerify(publicKey);
    signature.update(signingInput);// www.ja  v a2  s . c om
    return signature.verify(sigBytes);
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static boolean verifySignatureES512(byte[] signingInput, byte[] sigBytes, X509Certificate cert)
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    PublicKey publicKey = cert.getPublicKey();

    Signature signature = Signature.getInstance("SHA512WITHECDSA", "BC");
    signature.initVerify(publicKey);
    signature.update(signingInput);//from w  w  w.  j a  v  a2s  .c om
    return signature.verify(sigBytes);
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static boolean verifySignatureES256(byte[] signingInput, byte[] sigBytes, ECDSAPublicKey ecdsaPublicKey)
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException,
        NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
        IOException, SignatureException {
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-256");
    BigInteger q = ((ECCurve.Fp) ecSpec.getCurve()).getQ();
    ECFieldElement xFieldElement = new ECFieldElement.Fp(q, ecdsaPublicKey.getX());
    ECFieldElement yFieldElement = new ECFieldElement.Fp(q, ecdsaPublicKey.getY());
    ECPoint pointQ = new ECPoint.Fp(ecSpec.getCurve(), xFieldElement, yFieldElement);
    ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(pointQ, ecSpec);

    KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC");
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

    Signature signature = Signature.getInstance("SHA256WITHECDSA", "BC");
    signature.initVerify(publicKey);
    signature.update(signingInput);/*  w  w  w. j  av a 2s. c  o m*/
    return signature.verify(sigBytes);
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static boolean verifySignatureES384(byte[] signingInput, byte[] sigBytes, ECDSAPublicKey ecdsaPublicKey)
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException,
        NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
        IOException, SignatureException {
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-384");
    BigInteger q = ((ECCurve.Fp) ecSpec.getCurve()).getQ();
    ECFieldElement xFieldElement = new ECFieldElement.Fp(q, ecdsaPublicKey.getX());
    ECFieldElement yFieldElement = new ECFieldElement.Fp(q, ecdsaPublicKey.getY());
    ECPoint pointQ = new ECPoint.Fp(ecSpec.getCurve(), xFieldElement, yFieldElement);
    ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(pointQ, ecSpec);

    KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC");
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

    Signature signature = Signature.getInstance("SHA384WITHECDSA", "BC");
    signature.initVerify(publicKey);
    signature.update(signingInput);//from w  ww .  j a v a2s.  c  o m
    return signature.verify(sigBytes);
}