-
Hi I need to sign JWT with PEM PCKS#8 private key.
I generated the key using openssl with this command
and converted it to PKCS#8 with this command
The code stops on return await s.sign(key) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Use In your code above just changing RS256 to PS256 should do the trick. |
Beta Was this translation helpful? Give feedback.
-
FWIW I've added a set of helper functions to aid in this PEM import process. It does not get rid of the need to know what algs a key is good for but it cuts down on the code you have to write to make a PEM string import happen. Also, X509 import. import * as keytools from 'jose/key/import'
const algorithm = 'ES256'
{
const pkcs8 = `-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgiyvo0X+VQ0yIrOaN
nlrnUclopnvuuMfoc8HHly3505OhRANCAAQWUcdZ8uTSAsFuwtNy4KtsKqgeqYxg
l6kwL5D4N3pEGYGIDjV69Sw0zAt43480WqJv7HCL0mQnyqFmSrxj8jMa
-----END PRIVATE KEY-----`
const ecPrivateKey = await keytools.importPKCS8(pkcs8, algorithm)
console.log(ecPrivateKey)
}
{
const spki = `-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEFlHHWfLk0gLBbsLTcuCrbCqoHqmM
YJepMC+Q+Dd6RBmBiA41evUsNMwLeN+PNFqib+xwi9JkJ8qhZkq8Y/IzGg==
-----END PUBLIC KEY-----`
const ecPublicKey = await keytools.importSPKI(spki, algorithm)
console.log(ecPublicKey)
}
{
const x509 = `-----BEGIN CERTIFICATE-----
MIIBXjCCAQSgAwIBAgIGAXvykuMKMAoGCCqGSM49BAMCMDYxNDAyBgNVBAMMK3Np
QXBNOXpBdk1VaXhXVWVGaGtjZXg1NjJRRzFyQUhXaV96UlFQTVpQaG8wHhcNMjEw
OTE3MDcwNTE3WhcNMjIwNzE0MDcwNTE3WjA2MTQwMgYDVQQDDCtzaUFwTTl6QXZN
VWl4V1VlRmhrY2V4NTYyUUcxckFIV2lfelJRUE1aUGhvMFkwEwYHKoZIzj0CAQYI
KoZIzj0DAQcDQgAE8PbPvCv5D5xBFHEZlBp/q5OEUymq7RIgWIi7tkl9aGSpYE35
UH+kBKDnphJO3odpPZ5gvgKs2nwRWcrDnUjYLDAKBggqhkjOPQQDAgNIADBFAiEA
1yyMTRe66MhEXID9+uVub7woMkNYd0LhSHwKSPMUUTkCIFQGsfm1ecXOpeGOufAh
v+A1QWZMuTWqYt+uh/YSRNDn
-----END CERTIFICATE-----`
const ecPublicKey = await keytools.importX509(x509, algorithm)
console.log(ecPublicKey)
} |
Beta Was this translation helpful? Give feedback.
Use
RSASSA-PKCS1-v1_5
for RS256, RS384, and RS512, or useRSA-PSS
for PS256, PS384, and PS512. You cannot mix and match.In your code above just changing RS256 to PS256 should do the trick.