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.LicenseManager.java

/**
 * <pre>//  w w w  .j a v  a 2 s  .  co m
 * ?license????????
 * </pre>
 * @param license
 * @return
 * @throws Exception
 */
boolean validate(License license) throws Exception {
    File keyFile = new File(LicenseFactory.PUBLIC_KEY_FILE);
    String publicKey = FileHelper.readFile(keyFile).trim();

    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(EasyUtils.decodeHex(publicKey));
    KeyFactory keyFactory = KeyFactory.getInstance(LicenseFactory.KEY_ALGORITHM);
    java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);

    Signature sig = Signature.getInstance(LicenseFactory.KEY_ALGORITHM);
    sig.initVerify(pubKey);
    sig.update(license.getFingerprint());
    return sig.verify(EasyUtils.decodeHex(license.signature));
}

From source file:com.vimukti.accounter.license.LicenseManager.java

private byte[] checkAndGetLicenseText(String licenseContent) {
    byte[] licenseText;
    try {/*from w ww. java 2  s .  co  m*/
        byte[] decodedBytes = Base64.decodeBase64(licenseContent.getBytes());
        ByteArrayInputStream in = new ByteArrayInputStream(decodedBytes);
        DataInputStream dIn = new DataInputStream(in);
        int textLength = dIn.readInt();
        licenseText = new byte[textLength];
        dIn.read(licenseText);
        byte[] hash = new byte[dIn.available()];
        dIn.read(hash);
        try {
            Signature signature = Signature.getInstance("SHA1withDSA");
            signature.initVerify(PUBLIC_KEY);
            signature.update(licenseText);
            if (!signature.verify(hash)) {
                throw new LicenseException("Failed to verify the license.");
            }

        } catch (InvalidKeyException e) {
            throw new LicenseException(e);
        } catch (SignatureException e) {
            throw new LicenseException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new LicenseException(e);
        }

    } catch (IOException e) {
        throw new LicenseException(e);
    }

    return licenseText;
}

From source file:mx.bigdata.sat.cfdi.TFDv11c33.java

public int verificar() throws Exception {
    if (tfd == null) {
        return 601; //No contiene timbrado
    }/*from  w w  w  .  j av a 2 s  .  c  o  m*/
    Base64 b64 = new Base64();
    String sigStr = tfd.getSelloSAT();
    byte[] signature = b64.decode(sigStr);
    byte[] bytes = getOriginalBytes();
    Signature sig = Signature.getInstance("SHA256withRSA");
    sig.initVerify(cert);
    sig.update(bytes);
    boolean verified = sig.verify(signature);
    return verified ? 600 : 602; //Sello del timbrado no valido
}

From source file:org.apache.xml.security.algorithms.implementations.SignatureBaseRSA.java

/** @inheritDoc */
protected void engineInitVerify(Key publicKey) throws XMLSignatureException {
    if (!(publicKey instanceof PublicKey)) {
        String supplied = publicKey.getClass().getName();
        String needed = PublicKey.class.getName();
        Object exArgs[] = { supplied, needed };

        throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", exArgs);
    }//ww  w.  j a  v  a  2s  . c om

    try {
        this.signatureAlgorithm.initVerify((PublicKey) publicKey);
    } catch (InvalidKeyException ex) {
        // reinstantiate Signature object to work around bug in JDK
        // see: http://bugs.sun.com/view_bug.do?bug_id=4953555
        Signature sig = this.signatureAlgorithm;
        try {
            this.signatureAlgorithm = Signature.getInstance(signatureAlgorithm.getAlgorithm());
        } catch (Exception e) {
            // this shouldn't occur, but if it does, restore previous 
            // Signature
            if (log.isDebugEnabled()) {
                log.debug("Exception when reinstantiating Signature:" + e);
            }
            this.signatureAlgorithm = sig;
        }
        throw new XMLSignatureException("empty", ex);
    }
}

From source file:com.znsx.util.licence.LicenceUtil.java

/**
 * ?????/*w w  w.  j av a2s .  com*/
 * 
 * @param data
 *            ??
 * @param privateKey
 *            ???base64?
 * @return base64????
 * @throws Exception
 */
public static String sign(String data, String privateKeyString) throws Exception {
    Base64 base64 = new Base64();
    // ????
    // BASE64Decoder decoder = new BASE64Decoder();
    // byte[] bytes = decoder.decodeBuffer(privateKeyString);
    byte[] bytes = base64.decode(privateKeyString.getBytes("utf8"));
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
    PrivateKey privateKey = KeyFactory.getInstance("DSA").generatePrivate(keySpec);
    // ???
    Signature signature = Signature.getInstance("DSA");
    signature.initSign(privateKey);
    signature.update(data.getBytes("utf8"));
    // return new BASE64Encoder().encode(signature.sign());
    return new String(base64.encode(signature.sign()), "utf8");
}

From source file:com.sharky.Security.java

public static boolean verify(PublicKey publicKey, String signedData, String signature) {
    Signature sig;/*from   w  ww .  j  av a2 s .co m*/
    try {
        sig = Signature.getInstance(SIGNATURE_ALGORITHM);
        sig.initVerify(publicKey);
        sig.update(signedData.getBytes());
        if (!sig.verify(Base64.decode(signature))) {
            return false;
        }
        return true;
    } catch (NoSuchAlgorithmException e) {
        //code here
    } catch (InvalidKeyException e) {
        // code here
    } catch (SignatureException e) {
        // code here
    } catch (Base64DecoderException e) {
        // code here
    }
    return false;
}

From source file:gemlite.core.util.RSAUtils.java

/**
 * <p>//ww w .j a v a 2 s. c  om
 * ??
 * </p>
 * 
 * @param data
 *          ?
 * @param publicKey
 *          (BASE64?)
 * @param sign
 *          ??
 * 
 * @return
 * @throws Exception
 * 
 */
public static boolean verify(byte[] data, String publicKey, String sign) throws Exception {
    byte[] keyBytes = Base64Utils.decode(publicKey);
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    PublicKey publicK = keyFactory.generatePublic(keySpec);
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    signature.initVerify(publicK);
    signature.update(data);
    return signature.verify(Base64Utils.decode(sign));
}

From source file:pxb.android.tinysign.TinySign.java

private static Signature instanceSignature() throws Exception {
    byte[] data = dBase64(Constants.privateKey);
    KeyFactory rSAKeyFactory = KeyFactory.getInstance("RSA");
    PrivateKey privateKey = rSAKeyFactory.generatePrivate(new PKCS8EncodedKeySpec(data));
    Signature signature = Signature.getInstance("SHA1withRSA");
    signature.initSign(privateKey);/*  www  .j  av  a  2s. co m*/
    return signature;
}

From source file:com.vmware.identity.samlservice.SamlServiceTest.java

@Test
public void testVerifySignature() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    // pick a sample message
    String message = "This is a sample message to be encoded";

    // sign using our algorithm
    SignatureAlgorithm algo = SignatureAlgorithm.getSignatureAlgorithmForURI(TestConstants.SIGNATURE_ALGORITHM);
    Signature sig = Signature.getInstance(algo.getAlgorithmName());
    sig.initSign(privateKey);/*w w  w . java2s  . c  om*/

    byte[] messageBytes = message.getBytes();
    sig.update(messageBytes);

    byte[] sigBytes = sig.sign();
    String signature = Shared.encodeBytes(sigBytes);

    // verify signature here
    sig.initVerify(x509Certificate.getPublicKey());
    sig.update(messageBytes);
    boolean verifies = sig.verify(sigBytes);
    log.debug("signature verifies in test: " + verifies);

    // just call verifySignature method and expect to not throw
    service.verifySignature(message, signature);
}

From source file:Decoder.java

private byte[] checkAndGetLicenseText(String licenseContent) throws Exception {
    byte[] licenseText;
    try {//from   w  ww .  j a v a 2s  .  co  m
        byte[] decodedBytes = Base64.decodeBase64(licenseContent.getBytes());
        ByteArrayInputStream in = new ByteArrayInputStream(decodedBytes);
        DataInputStream dIn = new DataInputStream(in);
        int textLength = dIn.readInt();
        licenseText = new byte[textLength];
        dIn.read(licenseText);
        byte[] hash = new byte[dIn.available()];
        dIn.read(hash);
        try {
            Signature signature = Signature.getInstance("SHA1withDSA");
            signature.initVerify(PUBLIC_KEY);
            signature.update(licenseText);
            if (!signature.verify(hash)) {
                throw new Exception("Failed to verify the license.");
            }

        } catch (InvalidKeyException e) {
            throw new Exception(e);
        } catch (SignatureException e) {
            throw new Exception(e);
        } catch (NoSuchAlgorithmException e) {
            throw new Exception(e);
        }

    } catch (IOException e) {
        throw new Exception(e);
    }

    return licenseText;
}