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:be.fedict.eidviewer.lib.X509Utilities.java

public static boolean isValidSignature(X509Certificate certificate, byte[] data, byte[] data2,
        byte[] signature) {
    try {/*  w  ww  .  j av  a2  s.  com*/
        Signature verifier = Signature.getInstance("SHA1withRSA");
        verifier.initVerify(certificate);
        verifier.update(data);
        if (data2 != null)
            verifier.update(data2);
        return verifier.verify(signature);
    } catch (Exception e) {
        return false;
    }
}

From source file:com.sharky.Security.java

public static boolean verify(PublicKey publicKey, String signedData, String signature) {
    Signature sig;
    try {/*from  w  w  w .ja  va2s.co  m*/
        sig = Signature.getInstance(SIGNATURE_ALGORITHM);
        sig.initVerify(publicKey);
        sig.update(signedData.getBytes());
        if (!sig.verify(Base64.decode(signature))) {
            return false;
        }
        return true;
    } catch (NoSuchAlgorithmException e) {
        //code here
    } catch (InvalidKeyException e) {
        // code here
    } catch (SignatureException e) {
        // code here
    } catch (Base64DecoderException e) {
        // code here
    }
    return false;
}

From source file:com.alliander.osgp.oslp.OslpUtils.java

/**
 * Validate the signature against the message.
 *
 * @param message/*from w  ww  .ja v a2 s.c o m*/
 *            message to validate
 * @param securityKey
 *            signature to validate
 * @param publicKey
 *            public key to use for decryption of signature
 * @param signature
 *            signature algorithm to use
 * @param provider
 *            provider which supplies algorithm
 * @return true when signature is correct, false when it's not
 * @throws GeneralSecurityException
 *             when configuration is incorrect.
 */
public static boolean validateSignature(final byte[] message, final byte[] securityKey,
        final PublicKey publicKey, final String signature, final String provider)
        throws GeneralSecurityException {

    // Use fallback to plain SHA512 hash, which is encrypted with RSA
    // instead of real RSA signature
    if (signature.equalsIgnoreCase(FALLBACK_SIGNATURE)) {
        return validateEncryptedHash(message, securityKey, publicKey);
    }

    final Signature signatureBuilder = Signature.getInstance(signature, provider);
    signatureBuilder.initVerify(publicKey);
    signatureBuilder.update(message);

    return signatureBuilder.verify(securityKey);
}

From source file:com.znsx.util.licence.LicenceUtil.java

/**
 * ???/*from  w  w w  . j a  v  a2s .  c  o m*/
 * 
 * @param data
 *            ??
 * @param publicKey
 *            2
 * @param signature
 *            base64????
 * @return
 * @throws Exception
 * @author huangbuji
 *         <p />
 *         Create at 2014-2-12 ?5:37:18
 */
public static boolean verifyBinKey(String data, byte[] publicKey, String signature) throws Exception {
    Base64 base64 = new Base64();
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
    PublicKey pub = KeyFactory.getInstance("DSA").generatePublic(keySpec);
    // ?
    Signature sign = Signature.getInstance("DSA");
    sign.initVerify(pub);
    sign.update(data.getBytes("utf8"));
    // return sign.verify(decoder.decodeBuffer(signature));
    return sign.verify(base64.decode(signature.getBytes("utf8")));
}

From source file:com.znsx.util.licence.LicenceUtil.java

/**
 * ???/*from  ww  w .j a va  2  s. c  om*/
 * 
 * @param data
 *            ??
 * @param publicKeyString
 *            ??base64?
 * @param signature
 *            base64????
 * @return
 * @throws Exception
 */
public static boolean verify(String data, String publicKeyString, String signature) throws Exception {
    // ???
    // BASE64Decoder decoder = new BASE64Decoder();
    // byte[] bytes = decoder.decodeBuffer(publicKeyString);
    Base64 base64 = new Base64();
    byte[] bytes = base64.decode(publicKeyString.getBytes("utf8"));
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
    PublicKey publicKey = KeyFactory.getInstance("DSA").generatePublic(keySpec);
    // ?
    Signature sign = Signature.getInstance("DSA");
    sign.initVerify(publicKey);
    sign.update(data.getBytes("utf8"));
    // return sign.verify(decoder.decodeBuffer(signature));
    return sign.verify(base64.decode(signature.getBytes("utf8")));
}

From source file:com.sixsq.slipstream.cookie.CryptoUtils.java

/**
 * Determine if the given signature matches the given data.
 *
 * @param signed/*from w w  w. jav  a2 s.  c  o  m*/
 *            String representation of signature
 * @param data
 *            information to check
 *
 * @return true if the signature matches the given data, false otherwise
 */
public static boolean verify(String signed, String data) {

    boolean valid = false;

    try {

        Signature signature = Signature.getInstance(signatureAlgorithm);
        signature.initVerify(publicKey);

        signature.update(data.getBytes());

        byte[] signBytes = (new BigInteger(signed, radix)).toByteArray();
        valid = signature.verify(signBytes);

    } catch (NoSuchAlgorithmException e) {
        log.warning("Algorithm not recognized: " + signatureAlgorithm + " with details: " + e.getMessage());
    } catch (InvalidKeyException e) {
        log.warning(e.toString());
    } catch (SignatureException e) {
        log.warning(e.toString());
    }

    return valid;
}

From source file:com.shenit.commons.codec.RsaUtils.java

/**
 * RSA??//from   w ww . ja  va 2s  . co m
 * 
 * @param content
 *            ???
 * @param sign
 *            ??
 * @param publicKey
 *            ?
 * @param inputCharset
 *            ??
 * @return 
 */
public static boolean verify(String content, String sign, String publicKey, String algorithm,
        String inputCharset) {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(CODEC_RSA);
        byte[] encodedKey = Base64.decodeBase64(publicKey);
        PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));

        java.security.Signature signature = java.security.Signature.getInstance(algorithm);

        signature.initVerify(pubKey);
        signature.update(content.getBytes(inputCharset));

        boolean bverify = signature.verify(Base64.decodeBase64(sign));
        return bverify;

    } catch (Exception e) {
        if (LOG.isWarnEnabled())
            LOG.warn("[verify] verify with exception", e);
    }

    return false;
}

From source file:ee.ria.xroad.common.request.ManagementRequestHandler.java

private static boolean verifySignature(X509Certificate cert, byte[] signatureData, String signatureAlgorithmId,
        byte[] dataToVerify) throws Exception {
    try {/*from w w  w  . j  a v a  2s .c  o  m*/
        Signature signature = Signature.getInstance(signatureAlgorithmId, "BC");
        signature.initVerify(cert.getPublicKey());
        signature.update(dataToVerify);

        return signature.verify(signatureData);
    } catch (Exception e) {
        log.error("Failed to verify signature", e);

        throw translateException(e);
    }
}

From source file:org.javaweb.utils.RSAUtils.java

/**
 * RSA???/*  w  w  w  . j a  v a2s  . c om*/
 *
 * @param data ?
 * @param key  
 * @param sign ??Base64
 * @return
 * @throws Exception
 */
public static boolean verify(byte[] data, Key key, String sign) throws Exception {
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key.getEncoded());
    KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm());
    PublicKey publicK = keyFactory.generatePublic(keySpec);
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    signature.initVerify(publicK);
    signature.update(data);

    return signature.verify(Base64.decodeBase64(sign));
}

From source file:com.amazon.speech.speechlet.authentication.SpeechletRequestSignatureVerifier.java

/**
 * Verifies the certificate authenticity using the configured TrustStore and the signature of
 * the speechlet request.//w  w w  .j  av a2 s.c  o m
 *
 * @param serializedSpeechletRequest
 *            speechlet request serialized as a string of JSON
 * @param baseEncoded64Signature
 *            the signature for provided in the request header
 * @param signingCertificateChainUrl
 *            the certificate chain URL provided in the request header
 */
public static void checkRequestSignature(final byte[] serializedSpeechletRequest,
        final String baseEncoded64Signature, final String signingCertificateChainUrl) {
    if ((baseEncoded64Signature == null) || (signingCertificateChainUrl == null)) {
        throw new SecurityException("Missing signature/certificate for the provided speechlet request");
    }

    try {
        X509Certificate signingCertificate;
        if (CERTIFICATE_CACHE.containsKey(signingCertificateChainUrl)) {
            signingCertificate = CERTIFICATE_CACHE.get(signingCertificateChainUrl);
            /*
             * check the before/after dates on the certificate are still valid for the present
             * time
             */
            signingCertificate.checkValidity();
        } else {
            signingCertificate = retrieveAndVerifyCertificateChain(signingCertificateChainUrl);

            // if certificate is valid, then add it to the cache
            CERTIFICATE_CACHE.put(signingCertificateChainUrl, signingCertificate);
        }

        // verify that the request was signed by the provided certificate
        Signature signature = Signature.getInstance(Sdk.SIGNATURE_ALGORITHM);
        signature.initVerify(signingCertificate.getPublicKey());
        signature.update(serializedSpeechletRequest);
        if (!signature.verify(Base64.decodeBase64(baseEncoded64Signature.getBytes(Sdk.CHARACTER_ENCODING)))) {
            throw new SecurityException(
                    "Failed to verify the signature/certificate for the provided speechlet request");
        }
    } catch (CertificateException | SignatureException | NoSuchAlgorithmException | InvalidKeyException
            | IOException ex) {
        throw new SecurityException(
                "Failed to verify the signature/certificate for the provided speechlet request", ex);
    }
}