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:org.jgrades.lic.api.crypto.decrypt.SignatureValidator.java

public boolean signatureValidated(File encryptedLicenceFile, File signatureFile)
        throws LicenceCryptographyException {
    try {/*from   w w  w .j  av a  2 s  . co  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.java.demo.RsaDemo.java

public static boolean Verify(String str, byte[] enstr) {
    try {/*from  ww  w  .  j a  v  a  2 s . c o  m*/
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rSAPublicKey.getEncoded());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initVerify(publicKey);
        signature.update(str.getBytes());
        boolean result = signature.verify(enstr);
        return result;
    } catch (Exception e) {
        System.out.println("com.Java.Demo.RsaDemo.Verify()" + e.getMessage());
        return false;
    }
}

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

public static String sign(String src, PrivateKey privateKey) throws Exception {
    Signature rsa = Signature.getInstance("SHA1WithRSA");
    rsa.initSign(privateKey);// ww w  .  jav  a 2s  .  c om
    rsa.update(src.getBytes());
    byte[] sign = rsa.sign();
    return new String(Base64.encodeBase64(sign));
}

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

public String sign(String material) throws GeneralSecurityException {
    Signature sig = Signature.getInstance("SHA256withRSA");
    sig.initSign(privateKey, secureRandom);
    sig.update(material.getBytes(CHARSET));
    return Base64.encodeBase64String(sig.sign());
}

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

/**
 * .//from  ww w  .  j  a v a 2 s .  c o  m
 * 
 * @param pubKeyFile String 
 * @return Signature 
 */
public static Signature createSignatureForVerify(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);
        return dsa;
    } catch (Exception ex) {
        logger.error("errors appeared while trying to verify a signature", ex);
        return null;
    }
}

From source file:gui.configurar.GerarAssinatura.java

String assinar() {
    String senha = tSenha.getText();
    String c = tContribuinte.getText() + tDev.getText();
    if (certificado == null) {
        Msg.show("Escolha o certificado");
        return "";
    }// w  ww  . j  a  va 2 s . co  m
    try {
        KeyStore keystore = KeyStore.getInstance("PKCS12");
        keystore.load(new FileInputStream(certificado), senha.toCharArray());
        ArrayList<String> apelidos = new ArrayList<String>();
        Enumeration<String> aliases = keystore.aliases();
        while (aliases.hasMoreElements()) {
            apelidos.add(aliases.nextElement());
        }
        PrivateKey key = (PrivateKey) keystore.getKey(apelidos.get(0), senha.toCharArray());
        Signature assinatura = Signature.getInstance("SHA256withRSA");
        assinatura.initSign(key);
        byte[] bytes = c.getBytes();
        assinatura.update(bytes);
        byte[] assinado = assinatura.sign();
        String strAssinado = Base64.encodeBase64String(assinado);
        return strAssinado;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:ru.codeinside.gws.crypto.cryptopro.Pkcs7Test.java

@Test
public void testEncoding() throws Exception {
    CryptoProvider.loadCertificate();//  w ww. j  a v a  2s.  c  o  m
    byte[] content = IOUtils
            .toByteArray(R.getRequiredResourceStream("rr/req_cb8cff92-c788-46cf-a97f-f5c6f0170be5.xml"));
    final Signature signature = Signature.getInstance("GOST3411withGOST3410EL");
    signature.initSign(CryptoProvider.privateKey);
    signature.update(content);
    byte[] sign = signature.sign();
    byte[] pkcs7 = SunPkcs7
            .toPkcs7(new ru.codeinside.gws.api.Signature(CryptoProvider.cert, null, sign, false));
    ru.codeinside.gws.api.Signature sig2 = SunPkcs7.fromPkcs7(pkcs7);
    assertEquals(CryptoProvider.cert, sig2.certificate);
    assertArrayEquals(sign, sig2.sign);

    assertTrue(R.provider.validate(sig2, null, content));

}

From source file:org.springframework.security.oauth.common.signature.TestRSA_SHA1SignatureMethod.java

/**
 * tests signing and verifying./*from   w w  w.jav  a  2s  .  c  om*/
 */
public void testSignAndVerify() throws Exception {
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
    generator.initialize(1024);
    KeyPair keyPair = generator.generateKeyPair();
    String baseString = "thisismysignaturebasestringthatshouldbemuchlongerthanthisbutitdoesnthavetobeandherearesomestrangecharacters!@#$%^&*)(*";

    byte[] signatureBytes;
    {
        Signature signer = Signature.getInstance("SHA1withRSA");
        signer.initSign(keyPair.getPrivate());
        signer.update(baseString.getBytes("UTF-8"));
        signatureBytes = signer.sign();
    }

    {
        Signature signer = Signature.getInstance("SHA1withRSA");
        signer.initVerify(keyPair.getPublic());
        signer.update(baseString.getBytes("UTF-8"));
        assertTrue(signer.verify(signatureBytes));
    }

    RSA_SHA1SignatureMethod signatureMethod = new RSA_SHA1SignatureMethod(keyPair.getPrivate(),
            keyPair.getPublic());
    String signature = signatureMethod.sign(baseString);
    signatureMethod.verify(baseString, signature);
}

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);/*w  w  w .j  a v  a2s.  c om*/
    sig.update(text);
    return sig.verify(sigBytes);
}

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

/**
 * Verifies the signature for the given bytes using the public key.
 * @param signature Signature/*from   w  w w  .  jav a 2  s  .  c o  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());
    }
}