Example usage for java.security Signature update

List of usage examples for java.security Signature update

Introduction

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

Prototype

public final void update(ByteBuffer data) throws SignatureException 

Source Link

Document

Updates the data to be signed or verified using the specified ByteBuffer.

Usage

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

/**
 * ./*from ww  w  . j  a  v  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:com.icanft.common.startup.ValidateLicense.java

/**
 * License/*  w w  w .  jav a  2  s.  c  om*/
 * 
 * @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 ww  w  .j  av  a 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/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/*  w  w w.ja v a2s. 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;
}

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

/**
* RSA ??/*www.  j  a  v  a2  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.linkage.crm.csb.sign.CtSignature.java

/**
 * @param originalText String //from w  ww.j  a va 2s. co  m
 * @param pwd String 
 * @param alias String 
 * @param priKeyFile 
 * @return String 
 */
public static String signature(String originalText, String pwd, String alias, String priKeyFile) {
    try {
        KeyStore ks = KeyStore.getInstance("JKS");
        FileInputStream ksfis = new FileInputStream(priKeyFile);
        BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
        char[] kpass = pwd.toCharArray();
        ks.load(ksbufin, kpass);
        PrivateKey priKey = (PrivateKey) ks.getKey(alias, kpass);
        Signature rsa = Signature.getInstance("SHA1withDSA");
        rsa.initSign(priKey);
        rsa.update(originalText.getBytes());
        byte[] signedText = rsa.sign();
        return HexUtils.toHexString(signedText);
    } catch (Exception ex) {
        logger.error("errors appeared while trying to signature", ex);
        return null;
    }
}

From source file:ee.sk.hwcrypto.demo.signature.TestSigningData.java

private static byte[] encrypt(final String javaSignatureAlgorithm, final PrivateKey privateKey,
        final byte[] bytes) {
    try {// w  w  w  .  ja v  a2  s.  co  m
        java.security.Signature signature = java.security.Signature.getInstance(javaSignatureAlgorithm);
        signature.initSign(privateKey);
        signature.update(bytes);
        final byte[] signatureValue = signature.sign();
        return signatureValue;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}

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

public static byte[] sign(PrivateKey privateKey, byte[] message) {

    try {//from   ww w  . j av a2  s  .co  m
        Signature signatureObject = Signature.getInstance("SHA256withECDSA", new BouncyCastleProvider());
        signatureObject.initSign(privateKey);
        signatureObject.update(message);
        return signatureObject.sign();
    } catch (InvalidKeyException | SignatureException | NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }

}

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

/**
 * Verify a signature using the given token and certificate.
 *
 * @param message//from w ww.  j  ava  2s  .c o  m
 *            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:hh.learnj.test.license.test.rsa.RSATest.java

/**
 * ???// w w w  .ja  va 2s  .  com
 * 
 * @param target
 * @return
 * @throws Exception
 */
static String signByPrivateKey(String target) throws Exception {
    PrivateKey privateKey = getPrivateKey();
    Signature signature = Signature.getInstance(ALGORITHM_SIGN);
    signature.initSign(privateKey);
    signature.update(target.getBytes("UTF-8"));
    String sign = encodeBase64(signature.sign());
    System.out.println("???\r\n" + sign);
    return sign;
}