Search in sources :

Example 1 with RSAPrivateKey

use of io.jans.as.model.crypto.signature.RSAPrivateKey in project jans by JanssenProject.

the class SignatureTest method generateRS384Keys.

@Test
public void generateRS384Keys() throws Exception {
    showTitle("TEST: generateRS384Keys");
    KeyFactory<RSAPrivateKey, RSAPublicKey> keyFactory = new RSAKeyFactory(SignatureAlgorithm.RS384, "CN=Test CA Certificate");
    Key<RSAPrivateKey, RSAPublicKey> key = keyFactory.getKey();
    RSAPrivateKey privateKey = key.getPrivateKey();
    RSAPublicKey publicKey = key.getPublicKey();
    Certificate certificate = key.getCertificate();
    System.out.println(key);
    String signingInput = "Hello World!";
    RSASigner rsaSigner1 = new RSASigner(SignatureAlgorithm.RS384, privateKey);
    String signature = rsaSigner1.generateSignature(signingInput);
    RSASigner rsaSigner2 = new RSASigner(SignatureAlgorithm.RS384, publicKey);
    assertTrue(rsaSigner2.validateSignature(signingInput, signature));
    RSASigner rsaSigner3 = new RSASigner(SignatureAlgorithm.RS384, certificate);
    assertTrue(rsaSigner3.validateSignature(signingInput, signature));
}
Also used : RSAKeyFactory(io.jans.as.model.crypto.signature.RSAKeyFactory) RSAPublicKey(io.jans.as.model.crypto.signature.RSAPublicKey) RSASigner(io.jans.as.model.jws.RSASigner) RSAPrivateKey(io.jans.as.model.crypto.signature.RSAPrivateKey) Certificate(io.jans.as.model.crypto.Certificate) Test(org.testng.annotations.Test) BaseTest(io.jans.as.server.BaseTest)

Example 2 with RSAPrivateKey

use of io.jans.as.model.crypto.signature.RSAPrivateKey in project jans by JanssenProject.

the class SignatureTest method generateRS256Keys.

@Test
public void generateRS256Keys() throws Exception {
    showTitle("TEST: generateRS256Keys");
    KeyFactory<RSAPrivateKey, RSAPublicKey> keyFactory = new RSAKeyFactory(SignatureAlgorithm.RS256, "CN=Test CA Certificate");
    Key<RSAPrivateKey, RSAPublicKey> key = keyFactory.getKey();
    RSAPrivateKey privateKey = key.getPrivateKey();
    RSAPublicKey publicKey = key.getPublicKey();
    Certificate certificate = key.getCertificate();
    System.out.println(key);
    String signingInput = "Hello World!";
    RSASigner rsaSigner1 = new RSASigner(SignatureAlgorithm.RS256, privateKey);
    String signature = rsaSigner1.generateSignature(signingInput);
    RSASigner rsaSigner2 = new RSASigner(SignatureAlgorithm.RS256, publicKey);
    assertTrue(rsaSigner2.validateSignature(signingInput, signature));
    RSASigner rsaSigner3 = new RSASigner(SignatureAlgorithm.RS256, certificate);
    assertTrue(rsaSigner3.validateSignature(signingInput, signature));
}
Also used : RSAKeyFactory(io.jans.as.model.crypto.signature.RSAKeyFactory) RSAPublicKey(io.jans.as.model.crypto.signature.RSAPublicKey) RSASigner(io.jans.as.model.jws.RSASigner) RSAPrivateKey(io.jans.as.model.crypto.signature.RSAPrivateKey) Certificate(io.jans.as.model.crypto.Certificate) Test(org.testng.annotations.Test) BaseTest(io.jans.as.server.BaseTest)

Example 3 with RSAPrivateKey

use of io.jans.as.model.crypto.signature.RSAPrivateKey in project jans by JanssenProject.

the class RSASigner method generateSignature.

@Override
public String generateSignature(String signingInput) throws SignatureException {
    if (getSignatureAlgorithm() == null) {
        throw new SignatureException("The signature algorithm is null");
    }
    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(StandardCharsets.UTF_8));
        return Base64Util.base64urlencode(signature.sign());
    } catch (Exception e) {
        throw new SignatureException(e);
    }
}
Also used : RSAPrivateKey(io.jans.as.model.crypto.signature.RSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) Signature(java.security.Signature) SignatureException(java.security.SignatureException) KeyFactory(java.security.KeyFactory) SignatureException(java.security.SignatureException)

Example 4 with RSAPrivateKey

use of io.jans.as.model.crypto.signature.RSAPrivateKey in project jans by JanssenProject.

the class SignatureTest method generateRS512Keys.

@Test
public void generateRS512Keys() throws Exception {
    showTitle("TEST: generateRS512Keys");
    KeyFactory<RSAPrivateKey, RSAPublicKey> keyFactory = new RSAKeyFactory(SignatureAlgorithm.RS512, "CN=Test CA Certificate");
    Key<RSAPrivateKey, RSAPublicKey> key = keyFactory.getKey();
    RSAPrivateKey privateKey = key.getPrivateKey();
    RSAPublicKey publicKey = key.getPublicKey();
    Certificate certificate = key.getCertificate();
    System.out.println(key);
    String signingInput = "Hello World!";
    RSASigner rsaSigner1 = new RSASigner(SignatureAlgorithm.RS512, privateKey);
    String signature = rsaSigner1.generateSignature(signingInput);
    RSASigner rsaSigner2 = new RSASigner(SignatureAlgorithm.RS512, publicKey);
    assertTrue(rsaSigner2.validateSignature(signingInput, signature));
    RSASigner rsaSigner3 = new RSASigner(SignatureAlgorithm.RS512, certificate);
    assertTrue(rsaSigner3.validateSignature(signingInput, signature));
}
Also used : RSAKeyFactory(io.jans.as.model.crypto.signature.RSAKeyFactory) RSAPublicKey(io.jans.as.model.crypto.signature.RSAPublicKey) RSASigner(io.jans.as.model.jws.RSASigner) RSAPrivateKey(io.jans.as.model.crypto.signature.RSAPrivateKey) Certificate(io.jans.as.model.crypto.Certificate) Test(org.testng.annotations.Test) BaseTest(io.jans.as.server.BaseTest)

Aggregations

RSAPrivateKey (io.jans.as.model.crypto.signature.RSAPrivateKey)4 Certificate (io.jans.as.model.crypto.Certificate)3 RSAKeyFactory (io.jans.as.model.crypto.signature.RSAKeyFactory)3 RSAPublicKey (io.jans.as.model.crypto.signature.RSAPublicKey)3 RSASigner (io.jans.as.model.jws.RSASigner)3 BaseTest (io.jans.as.server.BaseTest)3 Test (org.testng.annotations.Test)3 KeyFactory (java.security.KeyFactory)1 PrivateKey (java.security.PrivateKey)1 Signature (java.security.Signature)1 SignatureException (java.security.SignatureException)1 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)1