Search in sources :

Example 56 with Signature

use of java.security.Signature in project android_frameworks_base by ResurrectionRemix.

the class CertPinInstallReceiverTest method createSignature.

private String createSignature(String content, String version, String requiredHash) throws Exception {
    Signature signer = Signature.getInstance("SHA512withRSA");
    signer.initSign(createKey());
    signer.update(content.trim().getBytes());
    signer.update(version.trim().getBytes());
    signer.update(requiredHash.getBytes());
    String sig = new String(Base64.encode(signer.sign(), Base64.DEFAULT));
    assertEquals(true, verifySignature(content, version, requiredHash, sig, createCertificate()));
    return sig;
}
Also used : Signature(java.security.Signature)

Example 57 with Signature

use of java.security.Signature in project android_frameworks_base by ResurrectionRemix.

the class CertPinInstallReceiverTest method verifySignature.

public boolean verifySignature(String content, String version, String requiredPrevious, String signature, X509Certificate cert) throws Exception {
    Signature signer = Signature.getInstance("SHA512withRSA");
    signer.initVerify(cert);
    signer.update(content.trim().getBytes());
    signer.update(version.trim().getBytes());
    signer.update(requiredPrevious.trim().getBytes());
    return signer.verify(Base64.decode(signature.getBytes(), Base64.DEFAULT));
}
Also used : Signature(java.security.Signature)

Example 58 with Signature

use of java.security.Signature in project robovm by robovm.

the class X509Util method calculateSignature.

static byte[] calculateSignature(DERObjectIdentifier sigOid, String sigName, String provider, PrivateKey key, SecureRandom random, ASN1Encodable object) throws IOException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    Signature sig;
    if (sigOid == null) {
        throw new IllegalStateException("no signature algorithm specified");
    }
    sig = X509Util.getSignatureInstance(sigName, provider);
    if (random != null) {
        sig.initSign(key, random);
    } else {
        sig.initSign(key);
    }
    sig.update(object.toASN1Primitive().getEncoded(ASN1Encoding.DER));
    return sig.sign();
}
Also used : Signature(java.security.Signature)

Example 59 with Signature

use of java.security.Signature in project GNS by MobilityFirst.

the class CryptoUtils method signDigestOfMessage.

/**
     * Signs a digest of a message using private key of the given guid.
     *
     * @param guidEntry
     * @param message
     * @return a signed digest of the message string encoded as a hex string
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws SignatureException
     * @throws java.io.UnsupportedEncodingException
     *
     * arun: This method need to be synchronized over the signature
     * instance, otherwise it will result in corrupted signatures.
     */
public static String signDigestOfMessage(GuidEntry guidEntry, String message) throws ClientException {
    try {
        Signature signatureInstance = getSignatureInstance();
        synchronized (signatureInstance) {
            signatureInstance.initSign(guidEntry.getPrivateKey());
            // iOS client uses UTF-8 - should switch to ISO-8859-1 to be consistent with
            // secret key version
            signatureInstance.update(message.getBytes("UTF-8"));
            byte[] signedString = signatureInstance.sign();
            // We used to encode this as a hex so we could send it with the html without
            // encoding. Not really necessary anymore for the socket based client,
            // but the iOS client does as well so we need to keep it like this.
            // Also note that the secret based method doesn't do this - it just returns a string
            // using the ISO-8859-1 charset.
            String result = DatatypeConverter.printHexBinary(signedString);
            //String result = ByteUtils.toHex(signedString);
            return result;
        }
    } catch (InvalidKeyException | UnsupportedEncodingException | SignatureException e) {
        throw new ClientException("Error encoding message", e);
    }
}
Also used : Signature(java.security.Signature) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SignatureException(java.security.SignatureException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) InvalidKeyException(java.security.InvalidKeyException)

Example 60 with Signature

use of java.security.Signature in project nhin-d by DirectProject.

the class PKCS11OperationTests method testSignDataOnToken.

@Test
public void testSignDataOnToken() throws Exception {
    final String pkcs11ProvName = TestUtils.setupSafeNetToken();
    if (!StringUtils.isEmpty(pkcs11ProvName)) {
        final KeyStore ks = KeyStore.getInstance("PKCS11");
        ks.load(null, "1Kingpuff".toCharArray());
        final Enumeration<String> aliases = ks.aliases();
        while (aliases.hasMoreElements()) {
            final String alias = aliases.nextElement();
            System.out.println("\r\nAlias Name: " + alias);
            final KeyStore.Entry entry = ks.getEntry(alias, null);
            System.out.println("Key Type: " + entry.getClass());
            if (entry instanceof KeyStore.PrivateKeyEntry) {
                final KeyStore.PrivateKeyEntry pEntry = (KeyStore.PrivateKeyEntry) entry;
                final Signature sig = Signature.getInstance("SHA256withRSA", pkcs11ProvName);
                sig.initSign(pEntry.getPrivateKey());
                // sign the data
                String starttext = "Some Text to Encrypt and Sign as an Example";
                final byte[] bytes = starttext.getBytes();
                sig.update(bytes);
                final byte[] theSignature = sig.sign();
                assertNotNull(theSignature);
                System.out.println("Data Signed");
                break;
            }
        }
    }
}
Also used : Signature(java.security.Signature) KeyStore(java.security.KeyStore) Test(org.junit.Test)

Aggregations

Signature (java.security.Signature)261 SignatureException (java.security.SignatureException)84 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)70 InvalidKeyException (java.security.InvalidKeyException)61 PublicKey (java.security.PublicKey)61 PrivateKey (java.security.PrivateKey)43 IOException (java.io.IOException)42 KeyFactory (java.security.KeyFactory)41 X509Certificate (java.security.cert.X509Certificate)26 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)23 KeyPair (java.security.KeyPair)19 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)19 GeneralSecurityException (java.security.GeneralSecurityException)16 KeyPairGenerator (java.security.KeyPairGenerator)16 MySignature1 (org.apache.harmony.security.tests.support.MySignature1)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 BigInteger (java.math.BigInteger)14 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)14 CertificateException (java.security.cert.CertificateException)14 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)14