Example usage for java.security Signature initSign

List of usage examples for java.security Signature initSign

Introduction

In this page you can find the example usage for java.security Signature initSign.

Prototype

public final void initSign(PrivateKey privateKey) throws InvalidKeyException 

Source Link

Document

Initialize this object for signing.

Usage

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

public static byte[] Encrypt(String str) {
    try {//from   ww  w .ja  va  2s. c o  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.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);
    dsa.update(message);//  w  w w.  j ava 2 s.com

    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);
    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 ww .  j  a  v  a 2s  .  c  o  m*/
        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:com.github.ibole.infrastructure.security.ECDSA.java

public static void jdkECDSA() {
    try {/*from   w  ww. j  a  v  a2 s . com*/
        // 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: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);
    sign.update(s.getBytes());/* www .  j  a  va  2s.co  m*/
    return sign.sign();
}

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

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

From source file:Main.java

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

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

public static byte[] sign(String datafile, PrivateKey prvKey, String sigAlg) throws Exception {
    Signature sig = Signature.getInstance(sigAlg);
    sig.initSign(prvKey);
    FileInputStream fis = new FileInputStream(datafile);
    byte[] dataBytes = new byte[1024];
    int nread = fis.read(dataBytes);
    while (nread > 0) {
        sig.update(dataBytes, 0, nread);
        nread = fis.read(dataBytes);//from  w ww.j a  v  a 2 s .co m
    }
    return sig.sign();
}