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:Main.java

static public boolean verifyJWS(String s, String algorithm, PublicKey pubKey, PrivateKey privKey) {
    boolean bverify = false;

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

    try {/* w w  w.  j a v  a  2s  .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])));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return bverify;
}

From source file:net.sf.zekr.common.util.CryptoUtils.java

public static boolean verify(byte[] text, PublicKey pubKey, byte[] sigBytes)
        throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    Signature sig = Signature.getInstance("SHA1withDSA");
    sig.initVerify(pubKey);
    sig.update(text);/*w  ww.j a v a  2s  .  c  o m*/
    return sig.verify(sigBytes);
}

From source file:fi.vm.kapa.identification.shibboleth.extauthn.util.CertificateUtil.java

public static boolean checkSignature(String data, String signature, X509Certificate cert) {
    boolean result = false;
    try {/*from   www . ja  va  2 s .  c o  m*/
        logger.debug("checkSignature: data={}, signature={}, cert={}", data, signature, cert.toString());
        byte[] sigToVerify = Base64.getDecoder().decode(signature);
        Signature sig = Signature.getInstance("SHA256withRSA");
        sig.initVerify(cert);
        sig.update(Base64.getDecoder().decode(data));
        result = sig.verify(sigToVerify);
    } catch (Exception e) {
        logger.warn("checkSignature: Got exception " + e.getClass(), e);
    }
    return result;
}

From source file:com.dianxin.imessage.common.util.SignUtil.java

public static boolean verifySign(String src, String sign, PublicKey publicKey) {
    try {/*w  ww  . j  a  va  2  s. co  m*/
        Signature rsa = Signature.getInstance("SHA1WithRSA");

        rsa.initVerify(publicKey);
        rsa.update(src.getBytes());
        return rsa.verify(Base64.decodeBase64(sign.getBytes()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

public static boolean verifyRSASignature(String subordinatePublicKeyContent, String signatureContent,
        String masterPublicKeyContent) throws NoSuchProviderException, NoSuchAlgorithmException, IOException,
        InvalidKeySpecException, InvalidKeyException, SignatureException {
    PublicKey masterPublicKey = getRSAPublicKeyFrom(masterPublicKeyContent);
    signatureContent = signatureContent.replace("\n", "");
    Signature signature = Signature.getInstance("SHA512withRSA");
    signature.initVerify(masterPublicKey);
    signature.update(subordinatePublicKeyContent.getBytes());
    return signature.verify(Base64.getDecoder().decode(signatureContent.getBytes()));
}

From source file:aiai.ai.utils.checksum.ChecksumWithSignatureService.java

public static boolean isValid(byte[] data, String signatureAsBase64, PublicKey publicKey) {
    try {/*w  w  w . jav  a  2s  .c  o m*/
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initVerify(publicKey);
        signature.update(data);
        //noinspection UnnecessaryLocalVariable
        final byte[] bytes = Base64.decodeBase64(signatureAsBase64);
        boolean status = signature.verify(bytes);
        return status;
    } catch (GeneralSecurityException e) {
        log.error("Error checking signature", e);
        throw new RuntimeException("Error", e);
    }
}

From source file:zerogame.info.javapay.web.RSASignature.java

/**
* RSA ??//  w  ww  . j a va 2 s  . c o  m
*/
public static boolean verify(String content, String sign) throws Exception {
    PublicKey pubKey = getPublicKey();

    try {
        java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);

        signature.initVerify(pubKey);
        signature.update(content.getBytes("utf-8"));

        boolean result = signature.verify(Base64.decodeBase64(sign));

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

    return false;
}

From source file:com.icanft.common.startup.ValidateLicense.java

/**
 * License//  w  w  w . ja v  a 2s. c o  m
 * 
 * @param licenseColl LicenseColl
 * @param path String
 * @return boolean
 */
public static boolean validateLicense(LicenseColl licenseColl, String path) {
    // ??
    boolean payedUser = true;
    List<String> macAddressList = CollectMacAddress.getMacAddress();
    for (LicenseInfo licenseInfo : licenseColl.getLicenseInfoList()) {
        String productName = licenseInfo.getProductName();
        String expirationDate = licenseInfo.getExpiration();
        String signature = licenseInfo.getSignature();
        boolean flag = false;
        String data = new StringBuffer().append(productName).append(expirationDate).toString();
        java.security.cert.CertificateFactory cf;
        try {
            cf = java.security.cert.CertificateFactory.getInstance("X.509");

            java.security.cert.Certificate cert = cf
                    .generateCertificate(new FileInputStream(path + "/WEB-INF/fbrp.cer"));

            // 
            PublicKey pubKey = cert.getPublicKey();
            // ??Signature
            Signature sig = Signature.getInstance("SHA1withDSA");
            sig.initVerify(pubKey);
            sig.update(InternationalizationUtil.getBytes(data));
            // ?License
            boolean verifies = true;
            //                  sig.verify(Base64.decodeBase64(InternationalizationUtil
            //                  .getBytes(signature)));
            if (verifies) {
                Date evalDate = DateTimeUtil.parseDate(expirationDate);
                if (evalDate.before(new Date())) {
                    // ?
                    payedUser = false;
                } else {
                    flag = true;
                    payedUser = true;
                    System.out.println(
                            "" + productName + " License" + expirationDate + "?");
                }
            }
        } catch (Exception e) {
            log.error("", e);
        }

        if (!flag) {
            payedUser = false;
            System.out.println(
                    "" + productName + " License??License?");
        }
    }
    return payedUser;
}

From source file:com.mqm.frame.infrastructure.util.ValidateLicense.java

/**
 * License//from   w  ww.  jav a  2s.  co  m
 * 
 * @param licenseColl LicenseColl
 * @param path String
 * @return boolean
 */
public static boolean validateLicense(LicenseColl licenseColl, String path) {
    // ??
    boolean payedUser = true;
    List<String> macAddressList = CollectMacAddress.getMacAddress();
    for (LicenseInfo licenseInfo : licenseColl.getLicenseInfoList()) {
        String productName = licenseInfo.getProductName();
        String expirationDate = licenseInfo.getExpiration();
        String signature = licenseInfo.getSignature();
        boolean flag = false;
        String data = new StringBuffer().append(productName).append(expirationDate).toString();
        java.security.cert.CertificateFactory cf;
        try {
            cf = java.security.cert.CertificateFactory.getInstance("X.509");

            java.security.cert.Certificate cert = cf
                    .generateCertificate(new FileInputStream(path + "/WEB-INF/fbrp.cer"));

            // 
            PublicKey pubKey = cert.getPublicKey();
            // ??Signature
            Signature sig = Signature.getInstance("SHA1withDSA");
            sig.initVerify(pubKey);
            sig.update(InternationalizationUtil.getBytes(data));
            // ?License
            boolean verifies = sig.verify(Base64.decode((InternationalizationUtil.getBytes(signature))));
            if (verifies) {
                Date evalDate = DateTimeUtil.parseDate(expirationDate);
                if (evalDate.before(new Date())) {
                    // ?
                    payedUser = false;
                } else {
                    flag = true;
                    payedUser = true;
                    System.out.println(
                            "" + productName + " License" + expirationDate + "?");
                }
            }
        } catch (Exception e) {
            log.error("", e);
        }

        if (!flag) {
            payedUser = false;
            System.out.println(
                    "" + productName + " License??License?");
        }
    }
    return payedUser;
}

From source file:com.qm.frame.infrastructure.util.ValidateLicense.java

/**
 * License//from w  w w.ja  va  2 s  .c o m
 * 
 * @param licenseColl LicenseColl
 * @param path String
 * @return boolean
 */
public static boolean validateLicense(LicenseColl licenseColl, String path) {
    // ??
    boolean payedUser = true;
    List<String> macAddressList = CollectMacAddress.getMacAddress();
    for (LicenseInfo licenseInfo : licenseColl.getLicenseInfoList()) {
        String productName = licenseInfo.getProductName();
        String expirationDate = licenseInfo.getExpiration();
        String signature = licenseInfo.getSignature();
        boolean flag = false;
        String data = new StringBuffer().append(productName).append(expirationDate).toString();
        java.security.cert.CertificateFactory cf;
        try {
            cf = java.security.cert.CertificateFactory.getInstance("X.509");

            java.security.cert.Certificate cert = cf
                    .generateCertificate(new FileInputStream(path + "/WEB-INF/qm.cer"));

            // 
            PublicKey pubKey = cert.getPublicKey();
            // ??Signature
            Signature sig = Signature.getInstance("SHA1withDSA");
            sig.initVerify(pubKey);
            sig.update(InternationalizationUtil.getBytes(data));
            // ?License
            boolean verifies = sig.verify(Base64.decode((InternationalizationUtil.getBytes(signature))));
            if (verifies) {
                Date evalDate = DateTimeUtil.parseDate(expirationDate);
                if (evalDate.before(new Date())) {
                    // ?
                    payedUser = false;
                } else {
                    flag = true;
                    payedUser = true;
                    System.out.println(
                            "" + productName + " License" + expirationDate + "?");
                }
            }
        } catch (Exception e) {
            log.error("", e);
        }

        if (!flag) {
            payedUser = false;
            System.out.println(
                    "" + productName + " License??License?");
        }
    }
    return payedUser;
}