Search in sources :

Example 86 with Signature

use of java.security.Signature in project spring-boot by spring-projects.

the class TokenValidator method hasValidSignature.

private boolean hasValidSignature(Token token, String key) {
    try {
        PublicKey publicKey = getPublicKey(key);
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initVerify(publicKey);
        signature.update(token.getContent());
        return signature.verify(token.getSignature());
    } catch (GeneralSecurityException ex) {
        return false;
    }
}
Also used : PublicKey(java.security.PublicKey) Signature(java.security.Signature) GeneralSecurityException(java.security.GeneralSecurityException)

Example 87 with Signature

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

the class OpenSSLX509Certificate method verifyInternal.

private void verifyInternal(PublicKey key, String sigProvider) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
    String sigAlg = getSigAlgName();
    if (sigAlg == null) {
        sigAlg = getSigAlgOID();
    }
    final Signature sig;
    if (sigProvider == null) {
        sig = Signature.getInstance(sigAlg);
    } else {
        sig = Signature.getInstance(sigAlg, sigProvider);
    }
    sig.initVerify(key);
    sig.update(getTBSCertificate());
    if (!sig.verify(getSignature())) {
        throw new SignatureException("signature did not verify");
    }
}
Also used : Signature(java.security.Signature) SignatureException(java.security.SignatureException)

Example 88 with Signature

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

the class JcaContentVerifierProviderBuilder method createSignatureStream.

private SignatureOutputStream createSignatureStream(AlgorithmIdentifier algorithm, PublicKey publicKey) throws OperatorCreationException {
    try {
        Signature sig = helper.createSignature(algorithm);
        sig.initVerify(publicKey);
        return new SignatureOutputStream(sig);
    } catch (GeneralSecurityException e) {
        throw new OperatorCreationException("exception on setup: " + e, e);
    }
}
Also used : Signature(java.security.Signature) GeneralSecurityException(java.security.GeneralSecurityException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException)

Example 89 with Signature

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

the class OperatorHelper method createRawSignature.

public Signature createRawSignature(AlgorithmIdentifier algorithm) {
    Signature sig;
    try {
        String algName = getSignatureName(algorithm);
        algName = "NONE" + algName.substring(algName.indexOf("WITH"));
        sig = helper.createSignature(algName);
        // the AlgorithmIdentifier parameters field MUST contain RSASSA-PSS-params.
        if (algorithm.getAlgorithm().equals(PKCSObjectIdentifiers.id_RSASSA_PSS)) {
            AlgorithmParameters params = helper.createAlgorithmParameters(algName);
            params.init(algorithm.getParameters().toASN1Primitive().getEncoded(), "ASN.1");
            PSSParameterSpec spec = (PSSParameterSpec) params.getParameterSpec(PSSParameterSpec.class);
            sig.setParameter(spec);
        }
    } catch (Exception e) {
        return null;
    }
    return sig;
}
Also used : PSSParameterSpec(java.security.spec.PSSParameterSpec) Signature(java.security.Signature) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 90 with Signature

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

the class SignatureTest method testVerify_SHA1withDSA_Key_Success.

public void testVerify_SHA1withDSA_Key_Success() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("DSA");
    DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(DSA_pub, DSA_P, DSA_Q, DSA_G);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    Signature sig = Signature.getInstance("SHA1withDSA");
    sig.initVerify(pubKey);
    sig.update(Vector2Data);
    assertTrue("Signature must verify correctly", sig.verify(SHA1withDSA_Vector2Signature));
}
Also used : PublicKey(java.security.PublicKey) Signature(java.security.Signature) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

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