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.xdi.oxauth.model.util.JwtUtil.java

public static boolean verifySignatureES512(byte[] signingInput, byte[] sigBytes, ECDSAPublicKey ecdsaPublicKey)
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException,
        NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
        IOException, SignatureException {
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-521");
    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("SHA512WITHECDSA", "BC");
    signature.initVerify(publicKey);
    signature.update(signingInput);/*from ww  w . j a v  a  2s . co m*/
    return signature.verify(sigBytes);
}

From source file:jef.tools.security.EncrypterUtil.java

/**
 * ????/* www.j  a  v  a 2  s  .c o  m*/
 * 
 * @param in
 * @param key
 * @param sign
 * @return
 */
public static boolean verifyDSASign(InputStream in, byte[] sign, PublicKey key) {
    Signature signetcheck;
    try {
        signetcheck = java.security.Signature.getInstance("DSA");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    try {
        signetcheck.initVerify(key);
        byte[] b = new byte[1024];
        int len;
        while ((len = in.read(b)) != -1) {
            signetcheck.update(b, 0, len);
        }
        return signetcheck.verify(sign);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (SignatureException e) {
        return false;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:architecture.common.license.validator.CheckSignatureValidator.java

public void validate(License license) throws LicenseException {
    try {/*from   w  ww  .  jav a2  s .  co m*/
        //   DSA ? .
        String publicKey = "308201b83082012c06072a8648ce3804013082011f02818100fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c70215009760508f15230bccb292b982a2eb840bf0581cf502818100f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a0381850002818100faf2d25b2866aa68501094d1097bebc95c6bcf1c58766f18b35fbf5e9d761cc5bf913447e374c21d279777859f9f043d1dc0d58b93a2081b56b4f5269a81b076907a3b11b01ec5cfde5dae4dfd7d26346e53e611235e714e69ec1bc141c77a8a28c4c799df570a4c3240e7f2fee19d6ed4caaa1b15b5da4a967ee82e3eb4d4ca";

        byte pub[] = Hex.decodeHex(publicKey.toCharArray());
        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pub);

        KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
        Signature sig = Signature.getInstance("DSA");

        sig.initVerify(pubKey);
        // license.getSignature().getBytes("UTF-8");
        byte decoded[] = Hex.decodeHex(license.getSignature().toCharArray());

        log.debug("decoded sig: " + Hex.encodeHexString(decoded));

        log.info((new StringBuilder()).append("Validating license. License fingerprint: ")
                .append(license.getSignature()).toString());

        sig.update(license.getFingerprint());

        boolean verified = sig.verify(decoded);

        if (!verified)
            throw new LicenseException("License signature is invalid.");
    } catch (Exception e) {
        log.fatal(e.getMessage(), e);
        throw new LicenseException(e);
    }
}

From source file:org.chaston.oakfunds.xsrf.XsrfSigner.java

public boolean verify(String material, String signature) throws GeneralSecurityException {
    Signature sig = Signature.getInstance("SHA256withRSA");
    sig.initVerify(certificate);
    sig.update(material.getBytes(CHARSET));
    return sig.verify(Base64.decodeBase64(signature));
}

From source file:org.jgrades.lic.api.crypto.decrypt.SignatureValidator.java

public boolean signatureValidated(File encryptedLicenceFile, File signatureFile)
        throws LicenceCryptographyException {
    try {/*from w  w  w.  j  a va2 s  .c  o m*/
        X509Certificate certificate = keyExtractor.getCertificateForVerification();
        PublicKey publicKey = certificate.getPublicKey();

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

        signature.update(FileUtils.readFileToByteArray(encryptedLicenceFile));

        return signature.verify(FileUtils.readFileToByteArray(signatureFile));
    } catch (SignatureException e) {
        LOGGER.error("Signature {} validation failed", signatureFile.getAbsolutePath(), e);
        return false;
    } catch (NoSuchAlgorithmException | InvalidKeyException | IOException e) {
        throw new LicenceCryptographyException(e);
    }
}

From source file:com.xinferin.licensing.LicenceActivator.java

/**
 * Verifies the signature for the given bytes using the public key.
 * @param signature Signature/*from ww  w.j  ava  2s  .co  m*/
 * @param data Data that was signed
 * @return boolean True if valid signature else false
 * @throws Exception 
*/
public boolean verifySignature(byte[] signature, byte[] data) throws Exception {

    try {
        initialiseKeys();
        Signature signatureInstance = Signature.getInstance("SHA1withRSA");
        signatureInstance.initVerify(publicKey);
        signatureInstance.update(data);

        return signatureInstance.verify(signature);
    } catch (NoSuchAlgorithmException e) {
        throw new Exception("There is no such algorithm. Please check the JDK ver." + e.getCause());
    } catch (SignatureException e) {
        throw new Exception("There is a problem with the signature provided " + e.getCause());
    }
}

From source file:org.jgrades.security.utils.SignatureProvider.java

public boolean signatureValidated(File encryptedLicenceFile, File signatureFile) {
    try {/*from  ww  w.j a va2  s  . c o m*/
        X509Certificate certificate = extractor.getCertificateForVerification();
        PublicKey publicKey = certificate.getPublicKey();

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

        signature.update(FileUtils.readFileToByteArray(encryptedLicenceFile));

        return signature.verify(FileUtils.readFileToByteArray(signatureFile));
    } catch (SignatureException e) {
        LOGGER.debug("Signature verification failed", e);
        return false;
    } catch (NoSuchAlgorithmException | InvalidKeyException | IOException e) {
        throw new CryptographyException(e);
    }
}

From source file:netinf.common.security.impl.SignatureAlgorithmImpl.java

/**
 * @see SignatureAlgorithm#verifySignature(String, String, PublicKey, String)
 *//*w  w  w. j a  v a2 s.  co m*/
@Override
public boolean verifySignature(String originalString, String signatureString, PublicKey pk,
        String hashAndSignatureFunction)
        throws SignatureException, NoSuchAlgorithmException, InvalidKeyException {
    Signature signature = Signature.getInstance(hashAndSignatureFunction);
    signature.initVerify(pk);
    signature.update(originalString.getBytes());
    return signature.verify(Base64.decodeBase64(signatureString));
}

From source file:com.streamsets.lib.security.util.DataSignature.java

public Verifier getVerifier(final PublicKey publicKey) {
    return new Verifier() {
        @Override/* www.  j  a  v a  2s  . c om*/
        public boolean verify(byte[] data, byte[] signature) throws GeneralSecurityException {
            Signature signer = Signature.getInstance("SHA1withDSA");
            signer.initVerify(publicKey);
            signer.update(data);
            return signer.verify(signature);
        }
    };
}

From source file:org.apache.james.jmap.crypto.JamesSignatureHandler.java

@Override
public boolean verify(String source, String signature) {
    Preconditions.checkNotNull(source);//  w  ww  .j  av  a 2  s  .  c  o  m
    Preconditions.checkNotNull(signature);
    try {
        Signature javaSignature = Signature.getInstance(ALGORITHM);
        javaSignature.initVerify(publicKey);
        javaSignature.update(source.getBytes());
        return javaSignature.verify(new Base64().decode(signature));
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw Throwables.propagate(e);
    } catch (SignatureException e) {
        LOGGER.warn("Attempt to use a malformed signature '" + signature + "' for source '" + source + "'", e);
        return false;
    }
}