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.ibm.mobilefirstplatform.clientsdk.android.security.mca.internal.certificate.DefaultJSONSigner.java

private byte[] signCsrData(String csrJSONData, PrivateKey privateKey)
        throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    Signature signature = Signature.getInstance("SHA256withRSA");
    signature.initSign(privateKey);
    signature.update(csrJSONData.getBytes());
    return signature.sign();
}

From source file:org.jgrades.security.utils.SignatureProvider.java

public byte[] sign(byte[] bytes) {
    try {/*w  w w  . j a v  a  2  s .  c  o  m*/
        if (!Optional.ofNullable(bytes).isPresent()) {
            throw new IllegalArgumentException();
        }
        Signature signature = Signature.getInstance(SIGNATURE_PROVIDER_INTERFACE);
        signature.initSign(extractor.getPrivateKeyForSigning());
        signature.update(bytes);
        return signature.sign();
    } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e) {
        throw new CryptographyException(e);
    }
}

From source file:cl.niclabs.tscrypto.common.encryption.KeyChain.java

public byte[] sign(String rsaKeyAlias, byte[] data) throws UnrecoverableEntryException,
        NoSuchAlgorithmException, KeyStoreException, InvalidKeyException, SignatureException {
    Signature sign = Signature.getInstance("SHA1WithRsa");
    sign.initSign(getPrivateKey(rsaKeyAlias));
    sign.update(data);/*from   w  w  w .  java2s.c  o m*/

    byte[] signature = sign.sign();
    return signature;
}

From source file:netinf.common.security.impl.SignatureAlgorithmImpl.java

/**
 * @see SignatureAlgorithm#sign(String, PrivateKey, String)
 *//*from ww w.  j a v a2  s .c  o  m*/
@Override
public String sign(String originalString, PrivateKey sk, String hashAndSignatureFunction)
        throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    Signature signature = Signature.getInstance(hashAndSignatureFunction);
    signature.initSign(sk);
    signature.update(originalString.getBytes());
    return Base64.encodeBase64String(signature.sign());
}

From source file:com.streamsets.lib.security.util.DataSignature.java

public Signer getSigner(final PrivateKey privateKey) {
    return new Signer() {
        @Override//from   w ww  .ja  v a2 s  . c  o m
        public byte[] sign(byte[] data) throws GeneralSecurityException {
            Signature signer = Signature.getInstance("SHA1withDSA");
            signer.initSign(privateKey);
            signer.update(data);
            return signer.sign();
        }
    };
}

From source file:org.apache.james.jmap.crypto.JamesSignatureHandler.java

@Override
public String sign(String source) {
    Preconditions.checkNotNull(source);/*from w  ww . ja  v a 2s .c o m*/
    try {
        Signature javaSignature = Signature.getInstance(ALGORITHM);
        javaSignature.initSign(privateKey);
        javaSignature.update(source.getBytes());
        return new Base64().encodeAsString(javaSignature.sign());
    } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.springframework.security.oauth.common.signature.TestRSA_SHA1SignatureMethod.java

/**
 * tests signing and verifying.//  w  w  w . ja va  2s .  co m
 */
public void testSignAndVerify() throws Exception {
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
    generator.initialize(1024);
    KeyPair keyPair = generator.generateKeyPair();
    String baseString = "thisismysignaturebasestringthatshouldbemuchlongerthanthisbutitdoesnthavetobeandherearesomestrangecharacters!@#$%^&*)(*";

    byte[] signatureBytes;
    {
        Signature signer = Signature.getInstance("SHA1withRSA");
        signer.initSign(keyPair.getPrivate());
        signer.update(baseString.getBytes("UTF-8"));
        signatureBytes = signer.sign();
    }

    {
        Signature signer = Signature.getInstance("SHA1withRSA");
        signer.initVerify(keyPair.getPublic());
        signer.update(baseString.getBytes("UTF-8"));
        assertTrue(signer.verify(signatureBytes));
    }

    RSA_SHA1SignatureMethod signatureMethod = new RSA_SHA1SignatureMethod(keyPair.getPrivate(),
            keyPair.getPublic());
    String signature = signatureMethod.sign(baseString);
    signatureMethod.verify(baseString, signature);
}

From source file:test.integ.be.fedict.hsm.PKCS11Test.java

@Test
public void testEToken() throws Exception {
    File tmpConfigFile = File.createTempFile("pkcs11-", ".conf");
    tmpConfigFile.deleteOnExit();//from w w  w  .  j  a v  a2s.c  o  m
    PrintWriter configWriter = new PrintWriter(new FileOutputStream(tmpConfigFile));
    configWriter.println("name=test");
    configWriter.println("library=/usr/lib/libeTPkcs11.so");
    configWriter.println("slotListIndex=0");
    configWriter.close();
    SunPKCS11 sunPKCS11 = new SunPKCS11(tmpConfigFile.getAbsolutePath());
    Security.addProvider(sunPKCS11);

    Security.removeProvider(sunPKCS11.getName());
    sunPKCS11 = new SunPKCS11(tmpConfigFile.getAbsolutePath());
    Security.addProvider(sunPKCS11);

    Security.removeProvider(sunPKCS11.getName());
    sunPKCS11 = new SunPKCS11(tmpConfigFile.getAbsolutePath());
    Security.addProvider(sunPKCS11);

    Security.removeProvider(sunPKCS11.getName());
    sunPKCS11 = new SunPKCS11(tmpConfigFile.getAbsolutePath());
    Security.addProvider(sunPKCS11);

    Security.removeProvider(sunPKCS11.getName());
    sunPKCS11 = new SunPKCS11(tmpConfigFile.getAbsolutePath());
    Security.addProvider(sunPKCS11);

    KeyStore keyStore = KeyStore.getInstance("PKCS11", sunPKCS11);
    keyStore.load(null, "HSMProxy1234".toCharArray());
    Enumeration<String> aliasesEnum = keyStore.aliases();
    String alias = aliasesEnum.nextElement();

    PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, "HSMProxy1234".toCharArray());

    final int TEST_COUNT = 50;
    int count = TEST_COUNT;
    while (count > 0) {
        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initSign(privateKey);
        signature.update("to be signed".getBytes());
        signature.sign();
        count--;
    }
}

From source file:net.padlocksoftware.padlock.license.LicenseSigner.java

/**
 * Sign the supplied license using the PrivateKey given at the LicenseSigner instantation.
 * /*from   w ww.j  a v  a 2 s .c o  m*/
 * @param license The license to sign. Once signed, a license cannot be modified.
 * @throws IllegalStateException If the license has already been signed.
 */
public void sign(License license) {
    synchronized (license) {
        if (license.isSigned()) {
            throw new IllegalStateException("License is already signed");
        }

        String licenseString = ((LicenseImpl) license).concatenate();
        try {
            Signature signature = Signature.getInstance("SHA1withDSA");
            signature.initSign(privateKey);
            signature.update(licenseString.getBytes("UTF-8"));
            byte[] sig = signature.sign();
            String sigString = new String(Hex.encodeHex(sig));
            ((LicenseImpl) license).props.setProperty("signature", sigString);
        } catch (SignatureException ex) {
            logger.log(Level.SEVERE, "Signature failure.  Please verify a DSA security provider is available.",
                    ex);
        } catch (InvalidKeyException ex) {
            logger.log(Level.SEVERE, "Key failure.  Please verify a DSA security provider is available.", ex);
        } catch (NoSuchAlgorithmException ex) {
            logger.log(Level.SEVERE,
                    "Signature creation failure.  Please verify a DSA security provider is available.", ex);
        } catch (UnsupportedEncodingException ex) {
            logger.log(Level.SEVERE, "Encoding Exception.  Please report to support@padlocksoftware.net", ex);
        }
    }
}

From source file:org.codice.ddf.commands.util.DigitalSignature.java

public byte[] createDigitalSignature(InputStream data, String alias, String password) throws IOException {
    PrivateKey privateKey = getPrivateKey(alias, password);

    if (privateKey == null) {
        throw new CatalogCommandRuntimeException("Unable to retrieve private key");
    }//w w w  .  j  av a  2 s.co  m

    try {
        Signature rsa = Signature.getInstance("SHA256withRSA");

        rsa.initSign(privateKey);

        byte[] buffer = new byte[BUFFER_SIZE];
        int len;

        while ((len = data.read(buffer)) >= 0) {
            rsa.update(buffer, OFFSET, len);
        }

        return rsa.sign();
    } catch (SignatureException | InvalidKeyException | NoSuchAlgorithmException e) {
        String message = "An error occurred while signing file";
        LOGGER.debug(message, e);
        throw new CatalogCommandRuntimeException(message, e);
    }
}