@@ -26,7 +26,7 @@ const base64UrlMatcher = /[=+/]/g
2626const encoderMap = { '=' : '' , '+' : '-' , '/' : '_' }
2727
2828const privateKeyPemMatcher = / ^ - - - - - B E G I N (?: ( R S A | E C | E N C R Y P T E D ) ) ? P R I V A T E K E Y - - - - - /
29- const publicKeyPemMatcher = ' -----BEGIN PUBLIC KEY-----'
29+ const publicKeyPemMatcher = / ^ - - - - - B E G I N ( R S A ) ? P U B L I C K E Y - - - - - /
3030const publicKeyX509CertMatcher = '-----BEGIN CERTIFICATE-----'
3131const privateKeysCache = new Cache ( 1000 )
3232const publicKeysCache = new Cache ( 1000 )
@@ -44,7 +44,7 @@ const ecCurves = {
4444
4545/* istanbul ignore next */
4646if ( ! useNewCrypto ) {
47- directSign = function ( alg , data , options ) {
47+ directSign = function ( alg , data , options ) {
4848 if ( typeof alg === 'undefined' ) {
4949 throw new TokenError ( TokenError . codes . signError , 'EdDSA algorithms are not supported by your Node.js version.' )
5050 }
@@ -55,7 +55,7 @@ if (!useNewCrypto) {
5555 }
5656}
5757
58- const PrivateKey = asn . define ( 'PrivateKey' , function ( ) {
58+ const PrivateKey = asn . define ( 'PrivateKey' , function ( ) {
5959 this . seq ( ) . obj (
6060 this . key ( 'version' ) . int ( ) ,
6161 this . key ( 'algorithm' )
@@ -69,7 +69,7 @@ const PrivateKey = asn.define('PrivateKey', function() {
6969 )
7070} )
7171
72- const PublicKey = asn . define ( 'PublicKey' , function ( ) {
72+ const PublicKey = asn . define ( 'PublicKey' , function ( ) {
7373 this . seq ( ) . obj (
7474 this . key ( 'algorithm' )
7575 . seq ( )
@@ -82,7 +82,7 @@ const PublicKey = asn.define('PublicKey', function() {
8282 )
8383} )
8484
85- const ECPrivateKey = asn . define ( 'ECPrivateKey' , function ( ) {
85+ const ECPrivateKey = asn . define ( 'ECPrivateKey' , function ( ) {
8686 this . seq ( ) . obj (
8787 this . key ( 'version' ) . int ( ) ,
8888 this . key ( 'privateKey' ) . octstr ( ) ,
@@ -103,7 +103,7 @@ function cacheSet(cache, key, value, error) {
103103}
104104
105105function performDetectPrivateKeyAlgorithm ( key ) {
106- if ( key . includes ( publicKeyPemMatcher ) || key . includes ( publicKeyX509CertMatcher ) ) {
106+ if ( key . match ( publicKeyPemMatcher ) || key . includes ( publicKeyX509CertMatcher ) ) {
107107 throw new TokenError ( TokenError . codes . invalidKey , 'Public keys are not supported for signing.' )
108108 }
109109
@@ -157,7 +157,7 @@ function performDetectPrivateKeyAlgorithm(key) {
157157function performDetectPublicKeyAlgorithms ( key ) {
158158 if ( key . match ( privateKeyPemMatcher ) ) {
159159 throw new TokenError ( TokenError . codes . invalidKey , 'Private keys are not supported for verifying.' )
160- } else if ( ! key . includes ( publicKeyPemMatcher ) && ! key . includes ( publicKeyX509CertMatcher ) ) {
160+ } else if ( ! key . match ( publicKeyPemMatcher ) && ! key . includes ( publicKeyX509CertMatcher ) ) {
161161 // Not a PEM, assume a plain secret
162162 return hsAlgorithms
163163 }
@@ -226,7 +226,6 @@ function detectPublicKeyAlgorithms(key) {
226226 if ( ! key ) {
227227 return 'none'
228228 }
229-
230229 // Check cache first
231230 const [ cached , error ] = publicKeysCache . get ( key ) || [ ]
232231
@@ -243,7 +242,6 @@ function detectPublicKeyAlgorithms(key) {
243242 } else if ( typeof key !== 'string' ) {
244243 throw new TokenError ( TokenError . codes . invalidKey , 'The public key must be a string or a buffer.' )
245244 }
246-
247245 return cacheSet ( publicKeysCache , key , performDetectPublicKeyAlgorithms ( key ) )
248246 } catch ( e ) {
249247 throw cacheSet (
0 commit comments