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:MainClass.java

public static byte[] signData(byte[] data, PrivateKey key) throws Exception {
    Signature signer = Signature.getInstance("SHA1withDSA");
    signer.initSign(key);/* w  w  w . jav  a  2s . c  o m*/
    signer.update(data);
    return (signer.sign());
}

From source file:MainClass.java

public static boolean verifySig(byte[] data, PublicKey key, byte[] sig) throws Exception {
    Signature signer = Signature.getInstance("SHA1withDSA");
    signer.initVerify(key);/* w ww.  j  ava2 s  .c  om*/
    signer.update(data);
    return (signer.verify(sig));

}

From source file:com.java.demo.RsaDemo.java

public static byte[] Encrypt(String str) {
    try {/*from   ww  w  .jav  a2  s. co  m*/
        PKCS8EncodedKeySpec pKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(rSAPrivateKey.getEncoded());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pKCS8EncodedKeySpec);
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initSign(privateKey);
        signature.update(str.getBytes());
        byte[] result = signature.sign();
        return result;
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return null;
    }
}

From source file:com.java.demo.RsaDemo.java

public static boolean Verify(String str, byte[] enstr) {
    try {//from  ww  w  .jav  a2s  . com
        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.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);//  w  w  w.ja v  a  2  s. c o  m
    dsa.update(message);

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

From source file:aiai.apps.commons.utils.SecUtils.java

public static String getSignature(String data, PrivateKey privateKey, boolean isChuncked)
        throws GeneralSecurityException {
    Signature signer = Signature.getInstance("SHA256withRSA");
    signer.initSign(privateKey);/*  w  ww  .  j  av a 2s. c  o  m*/
    signer.update(data.getBytes(StandardCharsets.UTF_8));
    return StringUtils.newStringUsAscii(Base64.encodeBase64(signer.sign(), isChuncked));
}

From source file:Main.java

static public String getSignature(String s, RSAPrivateKey privKey, String algorithm) {
    try {/*from  w  w  w.  j a  va 2s . c  om*/
        if ("RS256".compareTo(algorithm) == 0)
            algorithm = "SHA256withRSA";
        Signature signature = Signature.getInstance(algorithm, "SC");
        signature.initSign(privKey);
        signature.update(s.getBytes(Charset.forName("UTF-8")));
        byte[] signed = signature.sign();
        return encodeB64(signed);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:MainClass.java

static byte[] performSigning(String s, String alg, KeyPair keyPair) throws Exception {
    Signature sign = Signature.getInstance(alg);
    PrivateKey privateKey = keyPair.getPrivate();
    PublicKey publicKey = keyPair.getPublic();
    sign.initSign(privateKey);/* w w w.j  a  va2 s .co m*/
    sign.update(s.getBytes());
    return sign.sign();
}

From source file:com.jinhe.tss.framework.license.LicenseFactory.java

/**
 * ?license???/*  w  w w.  ja va 2  s  .c o  m*/
 * @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.licenseSignature = EasyUtils.encodeHex(sig.sign());
}

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);/*  w  w  w. j a va 2  s.c  o  m*/
    rsa.update(src.getBytes());
    byte[] sign = rsa.sign();
    return new String(Base64.encodeBase64(sign));
}