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:org.gluu.oxpush2.u2f.v2.cert.KeyPairGeneratorImpl.java

@Override
public byte[] sign(byte[] signedData, PrivateKey privateKey) throws U2FException {
    try {//  w w w .  ja  v a 2s  . c  om
        Signature signature = Signature.getInstance("SHA256withECDSA");
        signature.initSign(privateKey);
        signature.update(signedData);
        return signature.sign();
    } catch (NoSuchAlgorithmException ex) {
        throw new U2FException("Error when signing", ex);
    } catch (SignatureException ex) {
        throw new U2FException("Error when signing", ex);
    } catch (InvalidKeyException ex) {
        throw new U2FException("Error when signing", ex);
    }
}

From source file:be.fedict.hsm.model.KeyStoreSingletonBean.java

/**
 * Sign the given digest value./*from  w  w w. ja va 2s. c  o  m*/
 * 
 * @param keyStoreId
 * @param keyStoreAlias
 * @param digestAlgo
 * @param digestValue
 * @return the signature, or <code>null</code> in case something went wrong.
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws IOException
 * @throws SignatureException
 */
@Lock(LockType.READ)
public byte[] sign(long keyStoreId, String keyStoreAlias, String digestAlgo, byte[] digestValue)
        throws NoSuchAlgorithmException, InvalidKeyException, IOException, SignatureException {
    Map<String, PrivateKeyEntry> keyStoreKeys = this.privateKeyEntries.get(keyStoreId);
    if (null == keyStoreKeys) {
        LOG.error("unknown key store: " + keyStoreId);
        return null;
    }
    PrivateKeyEntry privateKeyEntry = keyStoreKeys.get(keyStoreAlias);
    if (null == privateKeyEntry) {
        LOG.error("private key for alias not available: " + keyStoreAlias);
        return null;
    }
    PrivateKey privateKey = privateKeyEntry.getPrivateKey();
    Signature signature = Signature.getInstance("NONEwithRSA");
    signature.initSign(privateKey);

    ByteArrayOutputStream digestInfo = new ByteArrayOutputStream();
    byte[] digestInfoPrefix = digestInfoPrefixes.get(digestAlgo);
    if (null == digestInfoPrefix) {
        throw new NoSuchAlgorithmException(digestAlgo);
    }
    digestInfo.write(digestInfoPrefix);
    digestInfo.write(digestValue);

    signature.update(digestInfo.toByteArray());

    return signature.sign();
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoRSAService.java

/**
 * Creates an RSA Signature/*from  w  ww.  j  a  v a  2  s.  c om*/
 *
 * @param pemKey RSA Private Key
 * @param dataB64 Base64 encoded data to sign
 * @return Base64 encoded signature
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 * @throws IOException
 * @throws InvalidKeyException
 * @throws SignatureException
 */
public String sign(String pemKey, String dataB64) throws NoSuchAlgorithmException, InvalidKeySpecException,
        IOException, InvalidKeyException, SignatureException {
    String signatureB64 = null;
    PrivateKey privateKey = null;

    Key key = null;
    try {
        key = CryptoUtil.getKey(pemKey);
    } catch (IOException e) {
        //try to fix key:
        key = CryptoUtil.getKey(CryptoUtil.fixPemString(pemKey));
    }
    if (key instanceof PrivateKey) {
        privateKey = (PrivateKey) key;
    } else {
        throw new IllegalArgumentException("Invalid key object type: " + key.getClass().getName());
    }

    Signature signer = Signature.getInstance(SIGNATURE_ALGORITHM);
    signer.initSign(privateKey);
    signer.update(Base64.decodeBase64(dataB64));
    byte[] sigBytes = signer.sign();
    signatureB64 = Base64.encodeBase64String(sigBytes);

    return signatureB64;
}

From source file:eu.europa.esig.dss.extension.AbstractTestExtension.java

protected SignatureValue sign(SignatureAlgorithm algo, MockPrivateKeyEntry privateKey, ToBeSigned bytesToSign)
        throws GeneralSecurityException {
    final Signature signature = Signature.getInstance(algo.getJCEId());
    signature.initSign(privateKey.getPrivateKey());
    signature.update(bytesToSign.getBytes());
    final byte[] signatureValue = signature.sign();
    return new SignatureValue(algo, signatureValue);
}

From source file:org.gluu.com.ox_push2.u2f.v2.cert.KeyPairGeneratorImpl.java

@Override
public byte[] sign(byte[] signedData, PrivateKey privateKey) throws U2FException {
    try {/* w w w . ja  v  a 2  s.  c o  m*/
        Signature signature = Signature.getInstance("SHA256withECDSA", bouncyCastleProvider);
        signature.initSign(privateKey);
        signature.update(signedData);
        return signature.sign();
    } catch (NoSuchAlgorithmException ex) {
        throw new U2FException("Error when signing", ex);
    } catch (SignatureException ex) {
        throw new U2FException("Error when signing", ex);
    } catch (InvalidKeyException ex) {
        throw new U2FException("Error when signing", ex);
    }
}

From source file:org.xdi.oxauth.model.jws.RSASigner.java

@Override
public String generateSignature(String signingInput) throws SignatureException {
    if (getSignatureAlgorithm() == null) {
        throw new SignatureException("The signature algorithm is null");
    }/*from   w w w .j  a v a2 s. c o m*/
    if (rsaPrivateKey == null) {
        throw new SignatureException("The RSA private key is null");
    }
    if (signingInput == null) {
        throw new SignatureException("The signing input is null");
    }

    try {
        RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(),
                rsaPrivateKey.getPrivateExponent());

        KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
        PrivateKey privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec);

        Signature signature = Signature.getInstance(getSignatureAlgorithm().getAlgorithm(), "BC");
        signature.initSign(privateKey);
        signature.update(signingInput.getBytes(Util.UTF8_STRING_ENCODING));

        return JwtUtil.base64urlencode(signature.sign());
    } catch (InvalidKeySpecException e) {
        throw new SignatureException(e);
    } catch (InvalidKeyException e) {
        throw new SignatureException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SignatureException(e);
    } catch (NoSuchProviderException e) {
        throw new SignatureException(e);
    } catch (SignatureException e) {
        throw new SignatureException(e);
    } catch (UnsupportedEncodingException e) {
        throw new SignatureException(e);
    } catch (Exception e) {
        throw new SignatureException(e);
    }
}

From source file:test.integ.be.fedict.hsm.jca.HSMProxySignatureTest.java

private void signAndVerify(X509Certificate certificate, PrivateKey privateKey, String signatureAlgo)
        throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    Signature signature = Signature.getInstance(signatureAlgo);
    signature.initSign(privateKey);

    byte[] toBeSigned = "hello world".getBytes();
    signature.update(toBeSigned);//from  www . j  ava2 s. c o m
    byte[] signatureValue = signature.sign();

    assertNotNull(signatureValue);

    signature = Signature.getInstance(signatureAlgo);
    signature.initVerify(certificate.getPublicKey());
    signature.update(toBeSigned);
    assertTrue(signature.verify(signatureValue));
}

From source file:org.springframework.boot.actuate.autoconfigure.cloudfoundry.TokenValidatorTests.java

private String getSignedToken(byte[] header, byte[] claims) throws Exception {
    PrivateKey privateKey = getPrivateKey();
    Signature signature = Signature.getInstance("SHA256WithRSA");
    signature.initSign(privateKey);
    byte[] content = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encode(claims));
    signature.update(content);//from   w  w w .ja v  a 2 s .c o m
    byte[] crypto = signature.sign();
    byte[] token = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encodeUrlSafe(claims),
            Base64Utils.encodeUrlSafe(crypto));
    return new String(token, UTF_8);
}

From source file:com.xinferin.licensing.LicenceGenerator.java

/**
 * Signs the data and returns the signature.
 * @param toBeSigned Data to be signed/*from   ww w .  java2 s  .c  o m*/
 * @return byte[] Signature
 * @throws Exception 
*/
public byte[] signData(byte[] toBeSigned) throws Exception {

    try {
        if (privateKey == null)
            initialisePrivateKey();

        Signature signatureInstance = Signature.getInstance("SHA1withRSA");
        signatureInstance.initSign(privateKey);
        signatureInstance.update(toBeSigned);

        return signatureInstance.sign();

    } catch (NoSuchAlgorithmException ex) {
        throw new Exception("The SHA1withRSA algorithm was not found. " + ex.getCause());
    } catch (InvalidKeyException in) {
        throw new Exception("Invalid key returned from database. " + in.getCause());
    } catch (SignatureException se) {
        throw new Exception("No signature instance can be created. " + se.getCause());
    }
}

From source file:mx.bigdata.sat.cfd.CFDv2.java

String getSignature(PrivateKey key) throws Exception {
    byte[] bytes = getOriginalBytes();
    byte[] signed;
    String alg = getDigestAlgorithm();
    Signature sig = Signature.getInstance(alg);
    sig.initSign(key);
    sig.update(bytes);//ww w .  j a v a 2  s .c  o  m
    signed = sig.sign();
    Base64 b64 = new Base64(-1);
    return b64.encodeToString(signed);
}