Skip to content

Commit

Permalink
refactor: removed asn1.js in favor of slimmer @panva/asn1.js
Browse files Browse the repository at this point in the history
resolves #61
  • Loading branch information
panva committed Feb 18, 2020
1 parent a0cdeb6 commit 9e7444b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
3 changes: 1 addition & 2 deletions lib/help/asn1/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const asn1 = require('asn1.js')
const asn1 = require('@panva/asn1.js')

const types = new Map()

Expand Down Expand Up @@ -32,4 +32,3 @@ const OID = asn1.define('OID', function () {
types.set('OID', OID)

module.exports = types
module.exports.bignum = asn1.bignum
26 changes: 24 additions & 2 deletions lib/help/base64url.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global BigInt */

const fromBase64 = (base64) => {
return base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
}
Expand Down Expand Up @@ -35,11 +37,31 @@ b64uJSON.decode.try = (input, encoding = 'utf8') => {
}
}

const encodeBN = (bn) => encodeBuffer(bn.toBuffer())
const bnToBuf = (bn) => {
let hex = BigInt(bn).toString(16)
if (hex.length % 2) {
hex = `0${hex}`
}

const len = hex.length / 2
const u8 = new Uint8Array(len)

let i = 0
let j = 0
while (i < len) {
u8[i] = parseInt(hex.slice(j, j + 2), 16)
i += 1
j += 2
}

return u8
}

const encodeBigInt = (bn) => encodeBuffer(Buffer.from(bnToBuf(bn)))

module.exports.decode = decode
module.exports.decodeToBuffer = decodeToBuffer
module.exports.encode = encode
module.exports.encodeBuffer = encodeBuffer
module.exports.JSON = b64uJSON
module.exports.encodeBN = encodeBN
module.exports.encodeBigInt = encodeBigInt
2 changes: 1 addition & 1 deletion lib/help/key_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ if (keyObjectSupported) {
const parsed = RSAPublicKey.decode(key, format, { label })

// special case when private pkcs1 PEM / DER is used with createPublicKey
if (parsed.n.toBuffer().equals(Buffer.alloc(1))) {
if (parsed.n === 0n) {
return createPublicKey(createPrivateKey({ key, format, type, passphrase }))
}

Expand Down
42 changes: 21 additions & 21 deletions lib/help/key_utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global BigInt */
const { EOL } = require('os')

const { name: secp256k1 } = require('../jwk/key/secp256k1_crv')
Expand All @@ -9,7 +10,6 @@ const asn1 = require('./asn1')
const computePrimes = require('./rsa_primes')
const { OKP_CURVES, EC_CURVES } = require('../registry')

const BN = asn1.bignum
const oidHexToCurve = new Map([
['06082a8648ce3d030107', 'P-256'],
['06052b8104000a', secp256k1],
Expand Down Expand Up @@ -75,14 +75,14 @@ const keyObjectToJWK = {

return {
kty: 'RSA',
n: base64url.encodeBN(n),
e: base64url.encodeBN(e),
d: base64url.encodeBN(d),
p: base64url.encodeBN(p),
q: base64url.encodeBN(q),
dp: base64url.encodeBN(dp),
dq: base64url.encodeBN(dq),
qi: base64url.encodeBN(qi)
n: base64url.encodeBigInt(n),
e: base64url.encodeBigInt(e),
d: base64url.encodeBigInt(d),
p: base64url.encodeBigInt(p),
q: base64url.encodeBigInt(q),
dp: base64url.encodeBigInt(dp),
dq: base64url.encodeBigInt(dq),
qi: base64url.encodeBigInt(qi)
}
},
public (keyObject) {
Expand All @@ -96,8 +96,8 @@ const keyObjectToJWK = {

return {
kty: 'RSA',
n: base64url.encodeBN(n),
e: base64url.encodeBN(e)
n: base64url.encodeBigInt(n),
e: base64url.encodeBigInt(e)
}
}
},
Expand Down Expand Up @@ -227,23 +227,23 @@ const jwkToPem = {

return RSAPrivateKey.encode({
version: 0,
n: new BN(base64url.decodeToBuffer(jwk.n)),
e: new BN(base64url.decodeToBuffer(jwk.e)),
d: new BN(base64url.decodeToBuffer(jwk.d)),
p: new BN(base64url.decodeToBuffer(jwk.p)),
q: new BN(base64url.decodeToBuffer(jwk.q)),
dp: new BN(base64url.decodeToBuffer(jwk.dp)),
dq: new BN(base64url.decodeToBuffer(jwk.dq)),
qi: new BN(base64url.decodeToBuffer(jwk.qi))
n: BigInt(`0x${base64url.decodeToBuffer(jwk.n).toString('hex')}`),
e: BigInt(`0x${base64url.decodeToBuffer(jwk.e).toString('hex')}`),
d: BigInt(`0x${base64url.decodeToBuffer(jwk.d).toString('hex')}`),
p: BigInt(`0x${base64url.decodeToBuffer(jwk.p).toString('hex')}`),
q: BigInt(`0x${base64url.decodeToBuffer(jwk.q).toString('hex')}`),
dp: BigInt(`0x${base64url.decodeToBuffer(jwk.dp).toString('hex')}`),
dq: BigInt(`0x${base64url.decodeToBuffer(jwk.dq).toString('hex')}`),
qi: BigInt(`0x${base64url.decodeToBuffer(jwk.qi).toString('hex')}`)
}, 'pem', { label: 'RSA PRIVATE KEY' })
},
public (jwk) {
const RSAPublicKey = asn1.get('RSAPublicKey')

return RSAPublicKey.encode({
version: 0,
n: new BN(base64url.decodeToBuffer(jwk.n)),
e: new BN(base64url.decodeToBuffer(jwk.e))
n: BigInt(`0x${base64url.decodeToBuffer(jwk.n).toString('hex')}`),
e: BigInt(`0x${base64url.decodeToBuffer(jwk.e).toString('hex')}`)
}, 'pem', { label: 'RSA PUBLIC KEY' })
}
},
Expand Down
2 changes: 1 addition & 1 deletion lib/help/rsa_primes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const TWO = BigInt(2)

const toJWKParameter = (n) => {
const hex = n.toString(16)
return base64url.encodeBuffer(Buffer.from(hex.length % 2 === 1 ? `0${hex}` : hex, 'hex'))
return base64url.encodeBuffer(Buffer.from(hex.length % 2 ? `0${hex}` : hex, 'hex'))
}
const fromBuffer = buf => BigInt(`0x${buf.toString('hex')}`)
const bitLength = n => n.toString(2).length
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
]
},
"dependencies": {
"asn1.js": "^5.3.0"
"@panva/asn1.js": "^1.0.0"
},
"devDependencies": {
"@commitlint/cli": "^8.3.4",
Expand Down

0 comments on commit 9e7444b

Please sign in to comment.