Package com.nimbusds.jose.util

Examples of com.nimbusds.jose.util.Base64URL


  public void testPublicKeyExportAndImport()
    throws Exception {


    RSAKey key = new RSAKey(new Base64URL(n), new Base64URL(e),
                      null, null, null, null,
                      null, null, null);

    // Public key export
    RSAPublicKey pubKey = key.toRSAPublicKey();
    assertEquals(new Base64URL(n).decodeToBigInteger(), pubKey.getModulus());
    assertEquals(new Base64URL(e).decodeToBigInteger(), pubKey.getPublicExponent());
    assertEquals("RSA", pubKey.getAlgorithm());


    // Public key import
    key = new RSAKey(pubKey, null, null, null, null, null, null, null);
    assertEquals(new Base64URL(n), key.getModulus());
    assertEquals(new Base64URL(e), key.getPublicExponent());
  }
View Full Code Here



  public void testPrivateKeyExportAndImport()
    throws Exception {

    RSAKey key = new RSAKey(new Base64URL(n), new Base64URL(e), new Base64URL(d),
                      new Base64URL(p), new Base64URL(q),
                      new Base64URL(dp), new Base64URL(dq), new Base64URL(qi),
                      null,
                      KeyUse.SIGNATURE, null, JWSAlgorithm.RS256, "1",
                      null, null, null);

    // Private key export with CRT (2nd form)
    RSAPrivateKey privKey = key.toRSAPrivateKey();
    assertEquals(new Base64URL(n).decodeToBigInteger(), privKey.getModulus());
    assertEquals(new Base64URL(d).decodeToBigInteger(), privKey.getPrivateExponent());

    assertTrue(privKey instanceof RSAPrivateCrtKey);
    RSAPrivateCrtKey privCrtKey = (RSAPrivateCrtKey)privKey;
    assertEquals(new Base64URL(e).decodeToBigInteger(), privCrtKey.getPublicExponent());
    assertEquals(new Base64URL(p).decodeToBigInteger(), privCrtKey.getPrimeP());
    assertEquals(new Base64URL(q).decodeToBigInteger(), privCrtKey.getPrimeQ());
    assertEquals(new Base64URL(dp).decodeToBigInteger(), privCrtKey.getPrimeExponentP());
    assertEquals(new Base64URL(dq).decodeToBigInteger(), privCrtKey.getPrimeExponentQ());
    assertEquals(new Base64URL(qi).decodeToBigInteger(), privCrtKey.getCrtCoefficient());


    // Key pair export
    KeyPair pair = key.toKeyPair();

    RSAPublicKey pubKey = (RSAPublicKey)pair.getPublic();
    assertEquals(new Base64URL(n).decodeToBigInteger(), pubKey.getModulus());
    assertEquals(new Base64URL(e).decodeToBigInteger(), pubKey.getPublicExponent());
    assertEquals("RSA", pubKey.getAlgorithm());

    privKey = (RSAPrivateKey)pair.getPrivate();
    assertEquals(new Base64URL(n).decodeToBigInteger(), privKey.getModulus());
    assertEquals(new Base64URL(d).decodeToBigInteger(), privKey.getPrivateExponent());

    assertTrue(privKey instanceof RSAPrivateCrtKey);
    privCrtKey = (RSAPrivateCrtKey)privKey;
    assertEquals(new Base64URL(e).decodeToBigInteger(), privCrtKey.getPublicExponent());
    assertEquals(new Base64URL(p).decodeToBigInteger(), privCrtKey.getPrimeP());
    assertEquals(new Base64URL(q).decodeToBigInteger(), privCrtKey.getPrimeQ());
    assertEquals(new Base64URL(dp).decodeToBigInteger(), privCrtKey.getPrimeExponentP());
    assertEquals(new Base64URL(dq).decodeToBigInteger(), privCrtKey.getPrimeExponentQ());
    assertEquals(new Base64URL(qi).decodeToBigInteger(), privCrtKey.getCrtCoefficient());


    // Key pair import, 1st private form
    key = new RSAKey(pubKey, privKey, KeyUse.SIGNATURE, null, JWSAlgorithm.RS256, "1", null, null, null);
    assertEquals(KeyUse.SIGNATURE, key.getKeyUse());
    assertEquals(JWSAlgorithm.RS256, key.getAlgorithm());
    assertEquals("1", key.getKeyID());

    assertEquals(new Base64URL(n), key.getModulus());
    assertEquals(new Base64URL(e), key.getPublicExponent());

    assertEquals(new Base64URL(d), key.getPrivateExponent());

    assertNull(key.getFirstPrimeFactor());
    assertNull(key.getSecondPrimeFactor());

    assertNull(key.getFirstFactorCRTExponent());
    assertNull(key.getSecondFactorCRTExponent());

    assertNull(key.getFirstCRTCoefficient());

    assertTrue(key.getOtherPrimes().isEmpty());

    assertTrue(key.isPrivate());


    // Key pair import, 2nd private form
    key = new RSAKey(pubKey, privCrtKey, KeyUse.SIGNATURE, null, JWSAlgorithm.RS256, "1", null, null, null);
    assertEquals(KeyUse.SIGNATURE, key.getKeyUse());
    assertEquals(JWSAlgorithm.RS256, key.getAlgorithm());
    assertEquals("1", key.getKeyID());

    assertEquals(new Base64URL(n), key.getModulus());
    assertEquals(new Base64URL(e), key.getPublicExponent());

    assertEquals(new Base64URL(d), key.getPrivateExponent());

    assertEquals(new Base64URL(p), key.getFirstPrimeFactor());
    assertEquals(new Base64URL(q), key.getSecondPrimeFactor());

    assertEquals(new Base64URL(dp), key.getFirstFactorCRTExponent());
    assertEquals(new Base64URL(dq), key.getSecondFactorCRTExponent());

    assertEquals(new Base64URL(qi), key.getFirstCRTCoefficient());

    assertTrue(key.getOtherPrimes().isEmpty());

    assertTrue(key.isPrivate());
  }
View Full Code Here

    KeyUse use = KeyUse.SIGNATURE;

    Set<KeyOperation> ops = new HashSet<KeyOperation>(Arrays.asList(KeyOperation.SIGN, KeyOperation.VERIFY));

    try {
      new RSAKey(new Base64URL(n), new Base64URL(e), use, ops, null, null, null, null, null);

      fail();
    } catch (IllegalArgumentException e) {
      // ok
    }

    try {
      new RSAKey.Builder(new Base64URL(n), new Base64URL(e)).
        keyUse(use).keyOperations(ops).build();
      fail();
    } catch (IllegalStateException e) {
      // ok
    }
View Full Code Here


  public void testConstructorAndSerialization()
    throws Exception {

    Base64URL k = new Base64URL("GawgguFyGrWKav7AX4VKUg");
    URL x5u = new URL("http://example.com/jwk.json");
    Base64URL x5t = new Base64URL("abc");
    List<Base64> x5c = new LinkedList<Base64>();
    x5c.add(new Base64("def"));

    Set<KeyOperation> ops = new LinkedHashSet<KeyOperation>(Arrays.asList(KeyOperation.SIGN, KeyOperation.VERIFY));

    OctetSequenceKey key = new OctetSequenceKey(k, null, ops, JWSAlgorithm.HS256, "1", x5u, x5t, x5c);

    assertEquals(KeyType.OCT, key.getKeyType());
    assertNull(key.getKeyUse());
    assertTrue(key.getKeyOperations().contains(KeyOperation.SIGN));
    assertTrue(key.getKeyOperations().contains(KeyOperation.VERIFY));
    assertEquals(2, key.getKeyOperations().size());
    assertEquals(JWSAlgorithm.HS256, key.getAlgorithm());
    assertEquals("1", key.getKeyID());
    assertEquals(x5u.toString(), key.getX509CertURL().toString());
    assertEquals(x5t.toString(), key.getX509CertThumbprint().toString());
    assertEquals(x5c.size(), key.getX509CertChain().size());

    assertEquals(k, key.getKeyValue());

    byte[] keyBytes = k.decode();

    for (int i=0; i < keyBytes.length; i++) {
      assertEquals(keyBytes[i], key.toByteArray()[i]);
    }

    assertNull(key.toPublicJWK());

    assertTrue(key.isPrivate());

    String jwkString = key.toJSONObject().toString();

    key = OctetSequenceKey.parse(jwkString);

    assertEquals(KeyType.OCT, key.getKeyType());
    assertNull(key.getKeyUse());
    assertTrue(key.getKeyOperations().contains(KeyOperation.SIGN));
    assertTrue(key.getKeyOperations().contains(KeyOperation.VERIFY));
    assertEquals(2, key.getKeyOperations().size());
    assertEquals(JWSAlgorithm.HS256, key.getAlgorithm());
    assertEquals("1", key.getKeyID());
    assertEquals(x5u.toString(), key.getX509CertURL().toString());
    assertEquals(x5t.toString(), key.getX509CertThumbprint().toString());
    assertEquals(x5c.size(), key.getX509CertChain().size());

    assertEquals(k, key.getKeyValue());

    keyBytes = k.decode();
View Full Code Here


  public void testAltConstructorAndSerialization()
    throws Exception {

    Base64URL k = new Base64URL("GawgguFyGrWKav7AX4VKUg");
    URL x5u = new URL("http://example.com/jwk.json");
    Base64URL x5t = new Base64URL("abc");
    List<Base64> x5c = new LinkedList<Base64>();
    x5c.add(new Base64("def"));

    OctetSequenceKey key = new OctetSequenceKey(k, KeyUse.SIGNATURE, null, JWSAlgorithm.HS256, "1", x5u, x5t, x5c);

    assertEquals(KeyType.OCT, key.getKeyType());
    assertEquals(KeyUse.SIGNATURE, key.getKeyUse());
    assertNull(key.getKeyOperations());
    assertEquals(JWSAlgorithm.HS256, key.getAlgorithm());
    assertEquals("1", key.getKeyID());
    assertEquals(x5u.toString(), key.getX509CertURL().toString());
    assertEquals(x5t.toString(), key.getX509CertThumbprint().toString());
    assertEquals(x5c.size(), key.getX509CertChain().size());

    assertEquals(k, key.getKeyValue());

    byte[] keyBytes = k.decode();

    for (int i=0; i < keyBytes.length; i++) {
      assertEquals(keyBytes[i], key.toByteArray()[i]);
    }

    assertNull(key.toPublicJWK());

    assertTrue(key.isPrivate());

    String jwkString = key.toJSONObject().toString();

    key = OctetSequenceKey.parse(jwkString);

    assertEquals(KeyType.OCT, key.getKeyType());
    assertEquals(KeyUse.SIGNATURE, key.getKeyUse());
    assertNull(key.getKeyOperations());
    assertEquals(JWSAlgorithm.HS256, key.getAlgorithm());
    assertEquals("1", key.getKeyID());
    assertEquals(x5u.toString(), key.getX509CertURL().toString());
    assertEquals(x5t.toString(), key.getX509CertThumbprint().toString());
    assertEquals(x5c.size(), key.getX509CertChain().size());

    assertEquals(k, key.getKeyValue());

    keyBytes = k.decode();
View Full Code Here

  public void testRejectUseAndOpsTogether() {

    Set<KeyOperation> ops = new LinkedHashSet<KeyOperation>(Arrays.asList(KeyOperation.SIGN, KeyOperation.VERIFY));

    try {
      new OctetSequenceKey(new Base64URL("GawgguFyGrWKav7AX4VKUg"), KeyUse.SIGNATURE, ops, null, null, null, null, null);
      fail();
    } catch (IllegalArgumentException e) {
      // ok
    }
  }
View Full Code Here


  public void testBuilder()
    throws Exception {

    Base64URL k = new Base64URL("GawgguFyGrWKav7AX4VKUg");
    URL x5u = new URL("http://example.com/jwk.json");
    Base64URL x5t = new Base64URL("abc");
    List<Base64> x5c = new LinkedList<Base64>();
    x5c.add(new Base64("def"));

    Set<KeyOperation> ops = new LinkedHashSet<KeyOperation>(Arrays.asList(KeyOperation.SIGN, KeyOperation.VERIFY));

    OctetSequenceKey key = new OctetSequenceKey.Builder(k).
      keyOperations(ops).
      algorithm(JWSAlgorithm.HS256).
      keyID("1").
      x509CertURL(x5u).
      x509CertThumbprint(x5t).
      x509CertChain(x5c).
      build();

    assertEquals(KeyType.OCT, key.getKeyType());
    assertNull(key.getKeyUse());
    assertTrue(key.getKeyOperations().contains(KeyOperation.SIGN));
    assertTrue(key.getKeyOperations().contains(KeyOperation.VERIFY));
    assertEquals(2, key.getKeyOperations().size());
    assertEquals(JWSAlgorithm.HS256, key.getAlgorithm());
    assertEquals("1", key.getKeyID());
    assertEquals(x5u.toString(), key.getX509CertURL().toString());
    assertEquals(x5t.toString(), key.getX509CertThumbprint().toString());
    assertEquals(x5c.size(), key.getX509CertChain().size());

    assertEquals(k, key.getKeyValue());

    byte[] keyBytes = k.decode();

    for (int i=0; i < keyBytes.length; i++) {
      assertEquals(keyBytes[i], key.toByteArray()[i]);
    }

    assertNull(key.toPublicJWK());

    assertTrue(key.isPrivate());


    String jwkString = key.toJSONObject().toString();

    key = OctetSequenceKey.parse(jwkString);


    assertEquals(KeyType.OCT, key.getKeyType());
    assertNull(key.getKeyUse());
    assertTrue(key.getKeyOperations().contains(KeyOperation.SIGN));
    assertTrue(key.getKeyOperations().contains(KeyOperation.VERIFY));
    assertEquals(2, key.getKeyOperations().size());
    assertEquals(JWSAlgorithm.HS256, key.getAlgorithm());
    assertEquals("1", key.getKeyID());
    assertEquals(x5u.toString(), key.getX509CertURL().toString());
    assertEquals(x5t.toString(), key.getX509CertThumbprint().toString());
    assertEquals(x5c.size(), key.getX509CertChain().size());

    assertEquals(k, key.getKeyValue());

    keyBytes = k.decode();
View Full Code Here

    JWSHeader header = JWSHeader.parse(B64_HEADER);

    JWSSigner signer = new RSASSASigner(PRIVATE_KEY);

    Base64URL b64sigComputed = signer.sign(header, SIGNABLE);

    assertEquals("Signature check", B64_SIG, b64sigComputed);
  }
View Full Code Here

    // Create signer from raw Java RSA key
    JWSObject jwsObject1 = new JWSObject(new JWSHeader(JWSAlgorithm.RS256), new Payload("test123"));
    JWSSigner signer = new RSASSASigner(rsaPrivateKey);
    jwsObject1.sign(signer);
    Base64URL sig1 = jwsObject1.getSignature();

    // Create signer from JWK representation
    RSAKey rsaJWK = new RSAKey.Builder(rsaPublicKey).privateKey(rsaPrivateKey).build();

    JWSObject jwsObject2 = new JWSObject(new JWSHeader(JWSAlgorithm.RS256), new Payload("test123"));
    signer = new RSASSASigner(rsaJWK.toRSAPrivateKey());
    jwsObject2.sign(signer);
    Base64URL sig2 = jwsObject2.getSignature();

    assertTrue("Signature comparison", sig1.equals(sig2));

    // Verifier from raw Java RSA key
    JWSVerifier verifier = new RSASSAVerifier(rsaPublicKey);
View Full Code Here

  public void testParsePlainHeaderFromBase64URL()
    throws Exception {

    // Example BASE64URL from JWT spec
    Base64URL in = new Base64URL("eyJhbGciOiJub25lIn0");

    Header header = Header.parse(in);

    assertTrue(header instanceof PlainHeader);
    assertEquals(in, header.toBase64URL());
View Full Code Here

TOP

Related Classes of com.nimbusds.jose.util.Base64URL

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.