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.dianxin.imessage.common.util.SignUtil.java

public static boolean verifySign(String src, String sign, PublicKey publicKey) {
    try {//  w w  w  .  ja v a  2  s  .  c o 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:org.jgrades.security.utils.SignatureProvider.java

public boolean signatureValidated(File encryptedLicenceFile, File signatureFile) {
    try {/*from   w w w.  ja  va 2s  .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:com.premiumminds.billy.portugal.services.certification.CertificationManager.java

public CertificationManager() {
    this.privateKey = null;
    this.publicKey = null;
    this.autoVerifyHash = false;

    try {//  www  .  j a  v a  2  s .c o m
        this.signature = Signature.getInstance("SHA1withRSA");
    } catch (NoSuchAlgorithmException e) {
        CertificationManager.log.error(e.getMessage(), 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);//www.ja v  a  2 s  .  c  o  m
    sig.update(material.getBytes(CHARSET));
    return sig.verify(Base64.decodeBase64(signature));
}

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

/**
 * RSA??/*from ww  w  . j av  a2  s.  c om*/
 * 
 * @param content
 *            ???
 * @param privateKey
 *            ?
 * @param input_charset
 *            ??
 * @return ??
 */
public static String sign(String content, String privateKey, String algorithm, String input_charset) {
    try {
        PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
        KeyFactory keyf = KeyFactory.getInstance(CODEC_RSA);
        PrivateKey priKey = keyf.generatePrivate(priPKCS8);

        Signature signature = Signature.getInstance(algorithm);
        signature.initSign(priKey);
        signature.update(content.getBytes(input_charset));
        byte[] signed = signature.sign();
        return Base64Utils.base64EncodeHex(signed);
    } catch (Exception e) {
        if (LOG.isWarnEnabled())
            LOG.warn("[sign] could not sign with exception", e);
    }

    return null;
}

From source file:org.wso2.carbon.connector.CreateJWT.java

/**
 * The method to sign the byte array of data using the private key.
 *
 * @param data the byte array of data to generate the signature
 * @param privateKey the private key to sign the byte array
 * @return the signed signature//from  w w  w .j a va2 s . co  m
 * @throws InvalidKeyException
 * @throws SignatureException
 * @throws NoSuchAlgorithmException
 */
public static byte[] signData(byte[] data, PrivateKey privateKey)
        throws InvalidKeyException, SignatureException, NoSuchAlgorithmException {

    Signature signature = Signature.getInstance(JWTConstant.SIGNATURE);
    signature.initSign(privateKey);
    signature.update(data);
    return signature.sign();
}

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

public void validate(License license) throws LicenseException {
    try {/*from  w ww  .  j  av  a2  s.  c  om*/
        //   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.syphr.utils.x509.X509Utils.java

/**
 * Create a signature using the given token and private key.
 *
 * @param message/*from  w w w. j a v a2  s.c o  m*/
 *            the message to sign
 * @param key
 *            the private key to use to create the signature (this must be
 *            PKCS8 encoded)
 * @param keyAlg
 *            the algorithm used to create the private key
 * @param sigAlg
 *            the algorithm to use to create the signature
 * @return the signature
 * @throws IOException
 *             if there is an error reading the key
 * @throws InvalidKeySpecException
 *             if the key algorithm is not appropriate for the given private
 *             key
 * @throws InvalidKeyException
 *             if the given private key is not valid
 * @throws SignatureException
 *             if there is an error while generating the signature
 */
public static byte[] sign(String message, InputStream key, KeyAlgorithm keyAlg, SignatureAlgorithm sigAlg)
        throws IOException, InvalidKeySpecException, InvalidKeyException, SignatureException {
    KeySpec privateKeySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(key));

    try {
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlg.getAlgorithm());
        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

        Signature sig = Signature.getInstance(sigAlg.getAlgorithm());
        sig.initSign(privateKey);
        sig.update(message.getBytes());
        return sig.sign();
    } catch (NoSuchAlgorithmException e) {
        /*
         * This is protected against by enforcing specific algorithm
         * choices.
         */
        throw new IllegalArgumentException("Unknown algorithm", e);
    }
}

From source file:com.threerings.getdown.tools.Digester.java

/**
 * Creates a digest file in the specified application directory.
 *//*w w w  . j  a  va  2 s.com*/
public static void signDigest(File appdir, File storePath, String storePass, String storeAlias)
        throws IOException, GeneralSecurityException {
    File inputFile = new File(appdir, Digest.DIGEST_FILE);
    File signatureFile = new File(appdir, Digest.DIGEST_FILE + Application.SIGNATURE_SUFFIX);

    FileInputStream storeInput = null, dataInput = null;
    FileOutputStream signatureOutput = null;
    try {
        // initialize the keystore
        KeyStore store = KeyStore.getInstance("JKS");
        storeInput = new FileInputStream(storePath);
        store.load(storeInput, storePass.toCharArray());
        PrivateKey key = (PrivateKey) store.getKey(storeAlias, storePass.toCharArray());

        // sign the digest file
        Signature sig = Signature.getInstance("SHA1withRSA");
        dataInput = new FileInputStream(inputFile);
        byte[] buffer = new byte[8192];
        int length;

        sig.initSign(key);
        while ((length = dataInput.read(buffer)) != -1) {
            sig.update(buffer, 0, length);
        }

        // Write out the signature
        signatureOutput = new FileOutputStream(signatureFile);
        String signed = new String(Base64.encodeBase64(sig.sign()));
        signatureOutput.write(signed.getBytes("utf8"));

    } finally {
        StreamUtil.close(signatureOutput);
        StreamUtil.close(dataInput);
        StreamUtil.close(storeInput);
    }
}

From source file:RGSDigestTools.SignatureTool.java

public String sign(String dataToSign)
        throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, UnsupportedEncodingException {
    Signature signer = Signature.getInstance(signAlg);
    signer.initSign(signKey);//from   www .j  a v a2  s.  co  m
    signer.update(dataToSign.getBytes("Windows-1251"));
    return bytesToHex(signer.sign());//Base64.encodeBase64String(signer.sign());//bytesToHex(signer.sign());

}