Skip to content

Commit

Permalink
feat(node): use libuv threadpool to verify 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 cf5074e commit ae9a7f4
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/runtime/node/verify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { verify as oneShotVerify, timingSafeEqual, KeyObject } from 'crypto'
import * as crypto from 'crypto'
import { promisify } from 'util'

import type { KeyLike } from '../../types.d'
import type { VerifyFunction } from '../interfaces.d'
Expand All @@ -7,12 +8,18 @@ import nodeKey from './node_key.js'
import sign from './sign.js'
import { isCryptoKey, getKeyObject } from './webcrypto.js'

let oneShotVerify = crypto.verify
if (oneShotVerify.length > 4) {
// @ts-expect-error
oneShotVerify = promisify(oneShotVerify)
}

const verify: VerifyFunction = async (alg, key: KeyLike, signature, data) => {
if (alg.startsWith('HS')) {
const expected = await sign(alg, key, data)
const actual = signature
try {
return timingSafeEqual(actual, expected)
return crypto.timingSafeEqual(actual, expected)
} catch {
// handle incorrect signature lengths
return false
Expand All @@ -24,7 +31,7 @@ const verify: VerifyFunction = async (alg, key: KeyLike, signature, data) => {
if (isCryptoKey(key)) {
// eslint-disable-next-line no-param-reassign
key = getKeyObject(key)
} else if (!(key instanceof KeyObject)) {
} else if (!(key instanceof crypto.KeyObject)) {
throw new TypeError('invalid key object type provided')
}

Expand Down

0 comments on commit ae9a7f4

Please sign in to comment.