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: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);//from   w w  w. ja  va2  s .c  o  m
    sign.update(s.getBytes());
    return sign.sign();
}

From source file:MainClass.java

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

From source file:MainClass.java

static void performVerification(String s, String alg, byte[] signature, PublicKey publicKey) throws Exception {
    Signature sign = Signature.getInstance(alg);
    sign.initVerify(publicKey);//from  w w  w  .j  ava2s . c o  m
    sign.update(s.getBytes());
    System.out.println(sign.verify(signature));
}

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  w w. j av a2  s .  co  m*/
    signer.update(input);
    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);/*from  w w  w .  j  av  a  2s  .  c  o m*/
    signer.update(data);
    return (signer.verify(sig));

}

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);//from  w  w w  .  j  a va2  s.  c  o  m
    signer.update(data.getBytes(StandardCharsets.UTF_8));
    return StringUtils.newStringUsAscii(Base64.encodeBase64(signer.sign(), isChuncked));
}

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

/**
 * ?license???/*from   ww  w.  j a  va2s  . 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.linkage.crm.csb.sign.CtSignature.java

/**
 * .//w  w w . ja  v  a 2s. com
 * 
 * @param pwd String 
 * @param alias String 
 * @param priKeyFile 
 * @return Signature 
 */
public static Signature createSignatureForSign(String pwd, String alias, String priKeyFile) {
    try {
        logger.debug("keypath=============" + priKeyFile);
        KeyStore ks = KeyStore.getInstance("JKS");
        FileInputStream ksfis = new FileInputStream(priKeyFile);
        BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
        char[] kpass = pwd.toCharArray();
        ks.load(ksbufin, kpass);
        PrivateKey priKey = (PrivateKey) ks.getKey(alias, kpass);
        Signature rsa = Signature.getInstance("SHA1withDSA");
        rsa.initSign(priKey);
        return rsa;
    } catch (Exception ex) {
        logger.error("errors appeared while trying to signature", ex);
        return null;
    }
}

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

public static byte[] Encrypt(String str) {
    try {/*  w w w.j  a  v  a  2  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:ai.susi.tools.JsonSignature.java

/**
 * Create and add a signature to a JSONObject
 * @param obj the JSONObject/*from   w  w w  .  j a va  2 s .  c  o m*/
 * @param key the private key to use
 * @throws InvalidKeyException if the key is not valid (for example not RSA)
 * @throws SignatureException if something with the JSONObject is bogus
 */
public static void addSignature(JSONObject obj, PrivateKey key) throws InvalidKeyException, SignatureException {

    removeSignature(obj);

    Signature signature;
    try {
        signature = Signature.getInstance("SHA256withRSA");
    } catch (NoSuchAlgorithmException e) {
        return; //does not happen
    }

    signature.initSign(key);
    signature.update(obj.toString().getBytes(StandardCharsets.UTF_8));

    byte[] sigBytes = signature.sign();

    obj.put(signatureString, new String(Base64.getEncoder().encode(sigBytes)));
}