README.md fix (and associated JweReadmeTest) to ensure docs accurately reflected new Jwks.CRV references

This commit is contained in:
Les Hazlewood 2023-08-17 15:37:35 -07:00
parent 620cc5d97f
commit 7bb97fac72
2 changed files with 37 additions and 15 deletions

View File

@ -1560,8 +1560,7 @@ public key (`keyPair.getPublic()`) to parse/verify a JWS.
>
> **The `PS256`, `PS384`, and `PS512` algorithms require JDK 11 or a compatible JCA Provider
> (like BouncyCastle) in the runtime classpath.**
> **The `EdDSA`, `Ed25519` and `Ed448` algorithms require JDK 15 or a compatible JCA Provider
> (like BouncyCastle) in the runtime classpath.**
> **The `EdDSA` algorithms requires JDK 15 or a compatible JCA Provider (like BouncyCastle) in the runtime classpath.**
> If you want to use either set of algorithms, and you are on an earlier JDK that does not support them,
> see the [Installation](#Installation) section to see how to enable BouncyCastle. All other algorithms are
> natively supported by the JDK.
@ -3154,12 +3153,12 @@ using Bob's Edwards Curve public key:
```java
// Create a test key suitable for the EdDSA signature algorithm using Ed25519 or Ed448 keys:
SignatureAlgorithm alg = Jwts.SIG.Ed25519; //or Ed448
KeyPair pair = alg.keyPair().build();
Curve curve = Jwks.CRV.Ed25519; //or Ed448
KeyPair pair = curve.keyPair().build();
// Bob creates the compact JWS with his Edwards Curve private key:
String jws = Jwts.builder().subject("Alice")
.signWith(pair.getPrivate(), alg) // <-- Bob's Edwards Curve private key
.signWith(pair.getPrivate(), Jwts.SIG.EdDSA) // <-- Bob's Edwards Curve private key w/ EdDSA
.compact();
// Alice receives and verifies the compact JWS came from Bob:
@ -3477,7 +3476,7 @@ Example creating and parsing an Edwards Elliptic Curve (Ed25519, Ed448, X25519,
`OctetPublicJwk` interface names):
```java
PublicKey key = Jwks.CRV.Ed25519.keyPair().build().getPublic();
PublicKey key = Jwks.CRV.Ed25519.keyPair().build().getPublic(); // or Ed448, X25519, X448
OctetPublicJwk<PublicKey> jwk = builder().octetKey(key).idFromThumbprint().build();
assert jwk.getId().equals(jwk.thumbprint().toString());
@ -3499,7 +3498,7 @@ Example creating and parsing an Edwards Elliptic Curve (Ed25519, Ed448, X25519,
`OctetPrivateJwk` and `OctetPublicJwk` interface names):
```java
KeyPair pair = Jwks.CRV.Ed448.keyPair().build();
KeyPair pair = Jwks.CRV.Ed448.keyPair().build(); // or Ed25519, X25519, X448
PublicKey pubKey = pair.getPublic();
PrivateKey privKey = pair.getPrivate();

View File

@ -18,6 +18,7 @@ package io.jsonwebtoken.all;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.jackson.io.JacksonSerializer;
import io.jsonwebtoken.security.AeadAlgorithm;
import io.jsonwebtoken.security.Curve;
import io.jsonwebtoken.security.EcPrivateJwk;
import io.jsonwebtoken.security.EcPublicJwk;
import io.jsonwebtoken.security.Jwk;
@ -85,7 +86,7 @@ public class JavaReadmeTest {
KeyPair pair = alg.keyPair().build();
// Bob creates the compact JWS with his RSA private key:
String jws = Jwts.builder().setSubject("Alice")
String jws = Jwts.builder().subject("Alice")
.signWith(pair.getPrivate(), alg) // <-- Bob's RSA private key
.compact();
@ -107,7 +108,7 @@ public class JavaReadmeTest {
KeyPair pair = alg.keyPair().build();
// Bob creates the compact JWS with his EC private key:
String jws = Jwts.builder().setSubject("Alice")
String jws = Jwts.builder().subject("Alice")
.signWith(pair.getPrivate(), alg) // <-- Bob's EC private key
.compact();
@ -119,6 +120,28 @@ public class JavaReadmeTest {
assert "Alice".equals(subject);
}
/**
* {@code README.md#example-jws-eddsa}
*/
@Test
public void testExampleJwsEdDSA() {
// Create a test key suitable for the EdDSA signature algorithm using Ed25519 or Ed448 keys:
Curve curve = Jwks.CRV.Ed25519; //or Ed448
KeyPair pair = curve.keyPair().build();
// Bob creates the compact JWS with his Edwards Curve private key:
String jws = Jwts.builder().subject("Alice")
.signWith(pair.getPrivate(), Jwts.SIG.EdDSA) // <-- Bob's Edwards Curve private key w/ EdDSA
.compact();
// Alice receives and verifies the compact JWS came from Bob:
String subject = Jwts.parser()
.verifyWith(pair.getPublic()) // <-- Bob's Edwards Curve public key
.build().parseClaimsJws(jws).getPayload().getSubject();
assert "Alice".equals(subject);
}
/**
* {@code README.md#example-jwe-dir}
*/
@ -155,7 +178,7 @@ public class JavaReadmeTest {
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...
// Bob creates the compact JWE with Alice's RSA public key so only she may read it:
String jwe = Jwts.builder().setAudience("Alice")
String jwe = Jwts.builder().audience("Alice")
.encryptWith(pair.getPublic(), alg, enc) // <-- Alice's RSA public key
.compact();
@ -180,7 +203,7 @@ public class JavaReadmeTest {
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...
// Create the compact JWE:
String jwe = Jwts.builder().setIssuer("me").encryptWith(key, alg, enc).compact();
String jwe = Jwts.builder().issuer("me").encryptWith(key, alg, enc).compact();
// Parse the compact JWE:
String issuer = Jwts.parser().decryptWith(key).build()
@ -203,7 +226,7 @@ public class JavaReadmeTest {
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...
// Bob creates the compact JWE with Alice's EC public key so only she may read it:
String jwe = Jwts.builder().setAudience("Alice")
String jwe = Jwts.builder().audience("Alice")
.encryptWith(pair.getPublic(), alg, enc) // <-- Alice's EC public key
.compact();
@ -239,7 +262,7 @@ public class JavaReadmeTest {
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...
// Create the compact JWE:
String jwe = Jwts.builder().setIssuer("me")
String jwe = Jwts.builder().issuer("me")
// Optional work factor is specified in the header:
//.header().pbes2Count(pbkdf2Iterations)).and()
.encryptWith(password, alg, enc)
@ -352,7 +375,7 @@ public class JavaReadmeTest {
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testExampleEdEcPublicJwk() {
PublicKey key = Jwks.CRV.Ed25519.keyPair().build().getPublic();
PublicKey key = Jwks.CRV.Ed25519.keyPair().build().getPublic(); // or Ed448, X25519, X448
OctetPublicJwk<PublicKey> jwk = builder().octetKey(key).idFromThumbprint().build();
assert jwk.getId().equals(jwk.thumbprint().toString());
@ -369,7 +392,7 @@ public class JavaReadmeTest {
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testExampleEdEcPrivateJwk() {
KeyPair pair = Jwks.CRV.Ed448.keyPair().build();
KeyPair pair = Jwks.CRV.Ed448.keyPair().build(); // or Ed25519, X25519, X448
PublicKey pubKey = pair.getPublic();
PrivateKey privKey = pair.getPrivate();