Skip to content

Commit

Permalink
feat(node): use libuv threadpool to sign in node >= 15.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Mar 17, 2021
1 parent 7f3cc44 commit cf5074e
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/runtime/node/sign.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { sign as oneShotSign, createHmac, KeyObject } from 'crypto'
import * as crypto from 'crypto'
import { promisify } from 'util'

import type { KeyLike } from '../../types.d'
import type { SignFunction } from '../interfaces.d'
import nodeDigest from './dsa_digest.js'
Expand All @@ -7,8 +9,14 @@ import nodeKey from './node_key.js'
import getSecretKey from './secret_key.js'
import { isCryptoKey, getKeyObject } from './webcrypto.js'

let oneShotSign = crypto.sign
if (oneShotSign.length > 3) {
// @ts-expect-error
oneShotSign = promisify(oneShotSign)
}

const sign: SignFunction = async (alg, key: KeyLike, data) => {
let keyObject: KeyObject
let keyObject: crypto.KeyObject
if (key instanceof Uint8Array) {
if (!alg.startsWith('HS')) {
throw new TypeError('symmetric keys are only applicable for HMAC-based JWA algorithms')
Expand All @@ -25,7 +33,7 @@ const sign: SignFunction = async (alg, key: KeyLike, data) => {
if (!keyObject.symmetricKeySize || keyObject.symmetricKeySize << 3 < bitlen) {
throw new TypeError(`${alg} requires symmetric keys to be ${bitlen} bits or larger`)
}
const hmac = createHmac(hmacDigest(alg), keyObject)
const hmac = crypto.createHmac(hmacDigest(alg), keyObject)
hmac.update(data)
return hmac.digest()
}
Expand Down

0 comments on commit cf5074e

Please sign in to comment.