Skip to content

Commit f9f2c5a

Browse files
committed
add ES256 functionality example
1 parent 67d4088 commit f9f2c5a

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

examples/ES256.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const crypto = require('isomorphic-webcrypto')
2+
const JWA = require('../src/jose/JWA')
3+
4+
let privateKey, publicKey
5+
6+
console.log('Testing ES256 support in @solid/jose library...')
7+
8+
crypto.subtle
9+
10+
// use webcrypto to generate an ECDSA keypair
11+
.generateKey(
12+
{
13+
name: 'ECDSA',
14+
namedCurve: 'P-256',
15+
hash: {
16+
name: 'SHA-256'
17+
}
18+
},
19+
true,
20+
['sign', 'verify']
21+
)
22+
23+
// use key with JWA to create a signature
24+
.then(keypair => {
25+
privateKey = keypair.privateKey
26+
publicKey = keypair.publicKey
27+
28+
console.log('✅ ES256 key pair generated successfully')
29+
30+
return JWA.sign('ES256', privateKey, 'header.payload')
31+
})
32+
33+
// verify the signature
34+
.then(signature => {
35+
console.log('✅ ES256 signature created:', signature.substring(0, 20) + '...')
36+
37+
return Promise.all([
38+
Promise.resolve(signature),
39+
JWA.verify('ES256', publicKey, signature, 'header.payload'),
40+
JWA.verify('ES256', publicKey, signature, 'wrong'),
41+
])
42+
})
43+
44+
// look at the output
45+
.then(result => {
46+
console.log('✅ ES256 verify results:')
47+
console.log(' Signature:', result[0].substring(0, 20) + '...')
48+
console.log(' Valid signature verified:', result[1])
49+
console.log(' Invalid signature verified:', result[2])
50+
51+
if (result[1] === true && result[2] === false) {
52+
console.log('🎉 ES256 is FULLY FUNCTIONAL in @solid/jose!')
53+
}
54+
})
55+
56+
// look at any errors
57+
.catch(error => {
58+
console.error('❌ ES256 test failed:', error.message)
59+
if (error.message.includes('normalizedAlgorithm.importKey')) {
60+
console.error('❌ This is the importKey error we were seeing!')
61+
}
62+
})

0 commit comments

Comments
 (0)