File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed
Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ repositories {
1515dependencies {
1616 implementation " org.jetbrains.kotlin:kotlin-stdlib-jdk8"
1717 implementation " com.nimbusds:nimbus-jose-jwt:7.8.1"
18+ implementation " com.google.crypto.tink:tink:1.2.2"
1819 testCompile group : ' junit' , name : ' junit' , version : ' 4.12'
1920}
2021
Original file line number Diff line number Diff line change 1+ import com.nimbusds.jose.JOSEObjectType
2+ import com.nimbusds.jose.JWSAlgorithm
3+ import com.nimbusds.jose.JWSHeader
4+ import com.nimbusds.jose.crypto.Ed25519Signer
5+ import com.nimbusds.jose.crypto.Ed25519Verifier
6+ import com.nimbusds.jose.jwk.Curve
7+ import com.nimbusds.jose.jwk.OctetKeyPair
8+ import com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator
9+ import com.nimbusds.jwt.JWTClaimsSet
10+ import com.nimbusds.jwt.SignedJWT
11+ import java.time.Instant
12+ import java.util.*
13+
14+ fun main () {
15+ val key: OctetKeyPair = OctetKeyPairGenerator (Curve .Ed25519 )
16+ .keyID(" 123" )
17+ .generate()
18+
19+ val header = JWSHeader .Builder (JWSAlgorithm .EdDSA )
20+ .type(JOSEObjectType .JWT )
21+ .keyID(key.keyID)
22+ .build();
23+ val payload = JWTClaimsSet .Builder ()
24+ .issuer(" me" )
25+ .audience(" you" )
26+ .subject(" bob" )
27+ .expirationTime(Date .from(Instant .now().plusSeconds(120 )))
28+ .build()
29+
30+ val signedJWT = SignedJWT (header, payload)
31+ signedJWT.sign(Ed25519Signer (key))
32+
33+ val jwt: String = signedJWT.serialize()
34+ println (jwt)
35+
36+
37+ // validate signature (and only signature)
38+ val isValid: Boolean = SignedJWT
39+ .parse(jwt)
40+ .verify(Ed25519Verifier (key.toPublicJWK()))
41+ println (" Valid signature: $isValid " )
42+ }
You can’t perform that action at this time.
0 commit comments