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.github.ibole.infrastructure.security.ECDSA.java

public static void jdkECDSA() {
    try {/*from   ww  w.jav a 2  s  .  co  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:Main.java

static public boolean verifyJWS(String s, String algorithm, PublicKey pubKey, PrivateKey privKey) {
    boolean bverify = false;

    String parts[] = s.split("\\.");
    if (parts == null || parts.length != 3)
        return bverify;

    try {//from  ww  w.j  ava 2s.co m
        if ("RS256".compareTo(algorithm) == 0)
            algorithm = "SHA256withRSA";
        Signature signature = Signature.getInstance(algorithm, "SC");
        signature.initVerify(pubKey);
        signature.update((parts[0] + "." + parts[1]).getBytes());
        bverify = signature.verify(decodeB64(parts[2]));

        Log.d("verifyJWS", "payload: " + new String(decodeB64(parts[1])));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return bverify;
}

From source file:Main.java

static byte[] sign(PrivateKey privateKey, byte[] input) throws Exception {
    Signature signer = Signature.getInstance(getSignatureAlgorithm(privateKey));
    signer.initSign(privateKey);/*from   w ww.jav a 2s  .c o m*/
    signer.update(input);
    return signer.sign();
}

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

/**
 * ?license???/*from  w  w  w .  jav a 2s  . com*/
 * @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: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);/*from   w  w w.  j  a  va 2 s  .c  om*/
    sig.update(text);
    return sig.verify(sigBytes);
}

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

public static byte[] sign(byte[] text, byte[] prvKeyBytes) throws GeneralSecurityException {
    PKCS8EncodedKeySpec prvSpec = new PKCS8EncodedKeySpec(prvKeyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    PrivateKey prvKey = keyFactory.generatePrivate(prvSpec);
    Signature sig = Signature.getInstance("SHA1withDSA");
    sig.initSign(prvKey);/*from w w  w.  j  ava 2 s . c  om*/
    sig.update(text);
    return sig.sign();
}

From source file:fi.vm.kapa.identification.shibboleth.extauthn.util.CertificateUtil.java

public static boolean checkSignature(String data, String signature, X509Certificate cert) {
    boolean result = false;
    try {/*from  w  ww .ja v  a 2  s  . c  o  m*/
        logger.debug("checkSignature: data={}, signature={}, cert={}", data, signature, cert.toString());
        byte[] sigToVerify = Base64.getDecoder().decode(signature);
        Signature sig = Signature.getInstance("SHA256withRSA");
        sig.initVerify(cert);
        sig.update(Base64.getDecoder().decode(data));
        result = sig.verify(sigToVerify);
    } catch (Exception e) {
        logger.warn("checkSignature: Got exception " + e.getClass(), e);
    }
    return result;
}

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

public static boolean verifySign(String src, String sign, PublicKey publicKey) {
    try {// w  w  w . j ava  2s.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:com.thoughtworks.go.server.util.EncryptionHelper.java

public static boolean verifyRSASignature(String subordinatePublicKeyContent, String signatureContent,
        String masterPublicKeyContent) throws NoSuchProviderException, NoSuchAlgorithmException, IOException,
        InvalidKeySpecException, InvalidKeyException, SignatureException {
    PublicKey masterPublicKey = getRSAPublicKeyFrom(masterPublicKeyContent);
    signatureContent = signatureContent.replace("\n", "");
    Signature signature = Signature.getInstance("SHA512withRSA");
    signature.initVerify(masterPublicKey);
    signature.update(subordinatePublicKeyContent.getBytes());
    return signature.verify(Base64.getDecoder().decode(signatureContent.getBytes()));
}

From source file:aiai.ai.utils.checksum.ChecksumWithSignatureService.java

public static boolean isValid(byte[] data, String signatureAsBase64, PublicKey publicKey) {
    try {/*from   ww  w .ja  va 2s . c o m*/
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initVerify(publicKey);
        signature.update(data);
        //noinspection UnnecessaryLocalVariable
        final byte[] bytes = Base64.decodeBase64(signatureAsBase64);
        boolean status = signature.verify(bytes);
        return status;
    } catch (GeneralSecurityException e) {
        log.error("Error checking signature", e);
        throw new RuntimeException("Error", e);
    }
}