Example usage for java.security Signature getInstance

List of usage examples for java.security Signature getInstance

Introduction

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

Prototype

public static Signature getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a Signature object that implements the specified signature algorithm.

Usage

From source file:com.boubei.tss.modules.license.LicenseFactory.java

/**
 * ?license???// www. j av a  2s.  c  om
 * @param license
 * @throws Exception
 */
public static synchronized void sign(License license) throws Exception {
    String privateKey = FileHelper.readFile(new File(PRIVATE_KEY_FILE));
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(EasyUtils.decodeHex(privateKey.trim()));
    PrivateKey privKey = keyFactory.generatePrivate(privKeySpec);

    Signature sig = Signature.getInstance(SIGN_ALGORITHM);
    sig.initSign(privKey);
    sig.update(license.getFingerprint());

    license.signature = EasyUtils.encodeHex(sig.sign());
}

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

/**
 * License/*from w ww.  j  ava2s  .  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:org.jgrades.security.utils.SignatureProvider.java

public byte[] sign(byte[] bytes) {
    try {/*ww  w .j  a  va 2 s .c  o  m*/
        if (!Optional.ofNullable(bytes).isPresent()) {
            throw new IllegalArgumentException();
        }
        Signature signature = Signature.getInstance(SIGNATURE_PROVIDER_INTERFACE);
        signature.initSign(extractor.getPrivateKeyForSigning());
        signature.update(bytes);
        return signature.sign();
    } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e) {
        throw new CryptographyException(e);
    }
}

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

public static byte[] sign(String datafile, PrivateKey prvKey, String sigAlg) throws Exception {
    Signature sig = Signature.getInstance(sigAlg);
    sig.initSign(prvKey);// w w  w  . ja v a2s .c  o m
    FileInputStream fis = new FileInputStream(datafile);
    byte[] dataBytes = new byte[1024];
    int nread = fis.read(dataBytes);
    while (nread > 0) {
        sig.update(dataBytes, 0, nread);
        nread = fis.read(dataBytes);
    }
    return sig.sign();
}

From source file:com.floreantpos.license.FiveStarPOSLicenseGenerator.java

private static String sign(byte[] message, PrivateKey privateKey)
        throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {

    Signature dsa = Signature.getInstance("SHA/DSA");
    dsa.initSign(privateKey);//from w ww .jav  a  2s . c o  m
    dsa.update(message);

    byte[] signature = dsa.sign();
    return Base64.getEncoder().encodeToString(signature);
}

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

/**
 * License/*from   w  w  w.j  a va2s.  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:com.mqm.frame.infrastructure.util.ValidateLicense.java

/**
 * License//ww w . j a va2s . com
 * 
 * @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.vmware.identity.rest.core.util.RequestSigner.java

/**
 * Signs a string using the private key and SHA 256 with RSA signing algorithm, and
 * returns it as a hex-encoded string.//from  ww w .j  a v  a2s.c  o  m
 *
 * @param signingString the string to sign.
 * @param privateKey the private key to sign the string with.
 * @return the signed string in a hex-encoded format.
 * @throws InvalidKeyException if the key is invalid.
 * @throws SignatureException if the signature algorithm is unable to process the input
 * data provided.
 */
public static String sign(String signingString, PrivateKey privateKey)
        throws InvalidKeyException, SignatureException {
    byte[] bytes = signingString.getBytes(StandardCharsets.UTF_8);

    Signature sig;

    try {
        sig = Signature.getInstance(SHA256_WITH_RSA);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("An error occurred while getting the signature algorithm", e);
    }

    sig.initSign(privateKey);
    sig.update(bytes);

    return Hex.encodeHexString(sig.sign());
}

From source file:com.github.ibole.infrastructure.security.ECDSA.java

public static void jdkECDSA() {
    try {//from w ww  .ja  v  a 2  s .c  o m
        // 1.?
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
        keyPairGenerator.initialize(256);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic();
        ECPrivateKey ecPrivateKey = (ECPrivateKey) keyPair.getPrivate();

        // 2.??
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());

        KeyFactory keyFactory = KeyFactory.getInstance("EC");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Signature signature = Signature.getInstance("SHA256withECDSA");
        signature.initSign(privateKey);
        signature.update(src.getBytes());
        byte[] res = signature.sign();
        System.out.println("??" + Base64.encodeBase64String(res));

        // 3.???
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded());
        keyFactory = KeyFactory.getInstance("EC");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        signature = Signature.getInstance("SHA256withECDSA");
        signature.initVerify(publicKey);
        signature.update(src.getBytes());
        boolean bool = signature.verify(res);
        System.out.println("?" + bool);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ai.susi.tools.JsonSignature.java

public static void addSignature(Map<String, byte[]> obj, PrivateKey key)
        throws InvalidKeyException, SignatureException {

    removeSignature(obj);/*from ww w  . j  ava2  s. c o  m*/

    Signature signature;
    try {
        signature = Signature.getInstance("SHA256withRSA");
    } catch (NoSuchAlgorithmException e) {
        return; //does not happen
    }

    signature.initSign(key);
    signature.update(obj.toString().getBytes(StandardCharsets.UTF_8));

    byte[] sigBytes = signature.sign();

    obj.put(signatureString, Base64.getEncoder().encode(sigBytes));
}