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:com.linkage.crm.csb.sign.CtSignature.java

/**
 * ./*from  w  w w .  j a  v a  2s  .  c o m*/
 * 
 * @param pubKeyFile String 
 * @return Signature 
 */
public static Signature createSignatureForVerify(String pubKeyFile) {
    try {
        CertificateFactory certificatefactory = CertificateFactory.getInstance("X.509");
        FileInputStream fin = new FileInputStream(pubKeyFile);
        X509Certificate certificate = (X509Certificate) certificatefactory.generateCertificate(fin);
        PublicKey pub = certificate.getPublicKey();
        Signature dsa = Signature.getInstance("SHA1withDSA");
        dsa.initVerify(pub);
        return dsa;
    } catch (Exception ex) {
        logger.error("errors appeared while trying to verify a signature", ex);
        return null;
    }
}

From source file:com.linkage.crm.csb.sign.CtSignature.java

/**
 * ./*from ww  w.  jav  a 2  s .c o  m*/
 * 
 * @param originalText String 
 * @param signedText String 
 * @param pubKeyFile String 
 * @return boolean 
 */
public static boolean verify(String originalText, String signedText, String pubKeyFile) {
    try {
        CertificateFactory certificatefactory = CertificateFactory.getInstance("X.509");
        FileInputStream fin = new FileInputStream(pubKeyFile);
        X509Certificate certificate = (X509Certificate) certificatefactory.generateCertificate(fin);
        PublicKey pub = certificate.getPublicKey();
        Signature dsa = Signature.getInstance("SHA1withDSA");
        dsa.initVerify(pub);
        dsa.update(originalText.getBytes());
        return dsa.verify(HexUtils.fromHexString(signedText));
    } catch (Exception ex) {
        logger.error("errors appeared while trying to verify a signature", ex);
        return false;
    }
}

From source file:org.syphr.utils.x509.X509Utils.java

/**
 * Verify a signature using the given token and certificate.
 *
 * @param message/* w  ww  . j a v  a2s .  c  om*/
 *            the message to which the signature belongs
 * @param signature
 *            the signature to verify
 * @param sigAlg
 *            the algorithm used to create the signature
 * @param certificate
 *            the certificate to use to verify the signature
 * @return <code>true</code> if the signature is valid; <code>false</code>
 *         otherwise
 * @throws CertificateException
 *             if there is an error reading the certificate
 * @throws InvalidKeyException
 *             if the given certificate does not have a valid public key
 * @throws SignatureException
 *             if the signature is not valid
 */
public static boolean verify(String message, byte[] signature, SignatureAlgorithm sigAlg,
        InputStream certificate) throws CertificateException, InvalidKeyException, SignatureException {
    Certificate cert = getCertFactory().generateCertificate(certificate);

    try {
        Signature sig = Signature.getInstance(sigAlg.getAlgorithm());
        sig.initVerify(cert);
        sig.update(message.getBytes());
        return sig.verify(signature);
    } catch (NoSuchAlgorithmException e) {
        /*
         * This is protected against by enforcing specific algorithm
         * choices.
         */
        throw new IllegalArgumentException("Unknown algorithm", e);
    }
}

From source file:com.dev.cty.utils.googleplay.Security.java

/**
 * Verifies that the signature from the server matches the computed
 * signature on the data.  Returns true if the data is correctly signed.
 *
 * @param publicKey public key associated with the developer account
 * @param signedData signed data from server
 * @param signature server signature//w  w  w  .  j a  v a  2s .c o m
 * @return true if the data and signature match
 */
public static boolean verify(PublicKey publicKey, String signedData, String signature) {
    Signature sig;
    try {
        sig = Signature.getInstance(SIGNATURE_ALGORITHM);
        sig.initVerify(publicKey);
        sig.update(signedData.getBytes());
        if (!sig.verify(Base64.decode(signature))) {
            logger.info("Signature verification failed.");
            return false;
        }
        return true;
    } catch (NoSuchAlgorithmException e) {
        logger.info("NoSuchAlgorithmException.");
    } catch (InvalidKeyException e) {
        logger.info("Invalid key specification.");
    } catch (SignatureException e) {
        logger.info("Signature exception.");
    } catch (Base64DecoderException e) {
        logger.info("Base64 decoding failed.");
    }
    return false;
}

From source file:hh.learnj.test.license.test.rsa.RSATest.java

/**
 * ???/*from   w w w.  j  ava  2s . com*/
 * 
 * @return
 * @throws Exception
 */
static void verifyByPublicKey(String target, String sign) throws Exception {
    PublicKey publicKey = getPublicKey();
    Signature signature = Signature.getInstance(ALGORITHM_SIGN);
    signature.initVerify(publicKey);
    signature.update(target.getBytes("UTF-8"));
    if (signature.verify(decodeBase64(sign))) {
        System.out.println("???");
    } else {
        System.out.println("???");
    }
}

From source file:acceptable_risk.nik.uniobuda.hu.andrawid.util.Security.java

/**
 * Verifies that the signature from the server matches the computed
 * signature on the data.  Returns true if the data is correctly signed.
 *
 * @param publicKey public key associated with the developer account
 * @param signedData signed data from server
 * @param signature server signature/*w ww.j  a va 2s . c  om*/
 * @return true if the data and signature match
 */
public static boolean verify(PublicKey publicKey, String signedData, String signature) {
    Signature sig;
    try {
        sig = Signature.getInstance(SIGNATURE_ALGORITHM);
        sig.initVerify(publicKey);
        sig.update(signedData.getBytes());
        if (!sig.verify(Base64.decode(signature))) {
            Log.e(TAG, "Signature verification failed.");
            return false;
        }
        return true;
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "NoSuchAlgorithmException.");
    } catch (InvalidKeyException e) {
        Log.e(TAG, "Invalid key specification.");
    } catch (SignatureException e) {
        Log.e(TAG, "Signature exception.");
    } catch (Base64DecoderException e) {
        Log.e(TAG, "Base64 decoding failed.");
    }
    return false;
}

From source file:me.disconnect.mobile.billing.Security.java

/**
 * Verifies that the signature from the server matches the computed
 * signature on the data.  Returns true if the data is correctly signed.
 *
 * @param publicKey public key associated with the developer account
 * @param signedData signed data from server
 * @param signature server signature//from   ww w .java  2s . c o m
 * @return true if the data and signature match
 */
public static boolean verify(PublicKey publicKey, String signedData, String signature) {
    Signature sig;
    try {
        sig = Signature.getInstance(SIGNATURE_ALGORITHM);
        sig.initVerify(publicKey);
        sig.update(signedData.getBytes());
        if (!sig.verify(Base64.decode(signature))) {
            Log.e(TAG, "Signature verification failed.");
            return false;
        }
        return true;
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "NoSuchAlgorithmException.");
    } catch (InvalidKeyException e) {
        Log.e(TAG, "Invalid key specification.");
    } catch (SignatureException e) {
        Log.e(TAG, "Signature exception.");
    } catch (Base64DecoderException e) {
        Log.e(TAG, "Base64 decoding failed.");
    } catch (RuntimeException e) {
        Log.e(TAG, "RuntimeException in Security.verify():");
        e.printStackTrace();
    }
    return false;
}

From source file:com.easarrive.aws.plugins.common.util.SNSUtil.java

public static boolean isMessageSignatureValid(SNSMessage msg) {
    try {// w  w  w .  j  a  v a  2  s  . c o  m
        URL url = new URL(msg.getSigningCertURL());
        InputStream inStream = url.openStream();
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);
        inStream.close();

        Signature sig = Signature.getInstance("SHA1withRSA");
        sig.initVerify(cert.getPublicKey());
        sig.update(getMessageBytesToSign(msg));
        return sig.verify(Base64.decodeBase64(msg.getSignature()));
    } catch (Exception e) {
        throw new SecurityException("Verify method failed.", e);
    }
}

From source file:nl.arietimmerman.u2f.server.CryptoHelper.java

/**
 * Verifies the signature, as set on the registration response and authentication response messages
 * @param publicKey The public key//from w  w w  . j a  v a2s  . com
 * @param bytesSigned The bytes to be signed
 * @param signature See "FIDO U2F Raw Message Formats" for requirements regarding signatures
 * @return
 */
public static boolean verifySignature(PublicKey publicKey, byte[] bytesSigned, byte[] signature) {
    Log.info("start verifySignature");
    try {
        Signature signatureObject = Signature.getInstance("SHA256withECDSA", new BouncyCastleProvider());
        Log.info("Initialized SHA256withECDSA");
        signatureObject.initVerify(publicKey);
        Log.info("executed initVerify");
        signatureObject.update(bytesSigned);
        Log.info("start verifying");
        return signatureObject.verify(signature);
    } catch (InvalidKeyException | SignatureException | NoSuchAlgorithmException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:org.carewebframework.api.security.CipherUtil.java

/**
 * Verifies a digitally signed payload./*from   www.java 2 s  .  c  o  m*/
 * 
 * @param key Public key to verify digital signature.
 * @param base64Signature Digital signature of content.
 * @param content The content that was signed.
 * @param timestamp Optional timestamp for time-sensitive payloads.
 * @param duration Optional validity duration in minutes for time-sensitive payloads.
 * @return True if signature is valid.
 * @throws Exception Unspecified exception.
 */
public static boolean verify(PublicKey key, String base64Signature, String content, String timestamp,
        int duration) throws Exception {
    if (key == null || base64Signature == null || content == null || timestamp == null) {
        return false;
    }

    try {
        if (timestamp != null && duration > 0) {
            validateTime(timestamp, duration);
        }

        Signature signature = Signature.getInstance(SIGN_ALGORITHM);
        signature.initVerify(key);
        signature.update(content.getBytes());
        byte[] signatureBytes = Base64.decodeBase64(base64Signature);
        return signature.verify(signatureBytes);
    } catch (Exception e) {
        log.error("Authentication Exception:verifySignature", e);
        throw e;
    }
}