diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2023-04-19 23:24:26 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-19 23:24:26 +0900 |
commit | fdebb7e7930b175b5dd80f891253000a29c82a4a (patch) | |
tree | 7035fe982f41b6b8e64de20b790c57379cd2851a /ext/node/polyfills/internal/crypto/sig.ts | |
parent | 5a77bb884416753b85f2acecd4895c75f1c53187 (diff) |
fix(ext/node): add crypto.sign|verify methods (#18765)
Diffstat (limited to 'ext/node/polyfills/internal/crypto/sig.ts')
-rw-r--r-- | ext/node/polyfills/internal/crypto/sig.ts | 94 |
1 files changed, 61 insertions, 33 deletions
diff --git a/ext/node/polyfills/internal/crypto/sig.ts b/ext/node/polyfills/internal/crypto/sig.ts index 2996cb2ca..ab586ba65 100644 --- a/ext/node/polyfills/internal/crypto/sig.ts +++ b/ext/node/polyfills/internal/crypto/sig.ts @@ -2,7 +2,10 @@ // Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license. import { notImplemented } from "ext:deno_node/_utils.ts"; -import { validateString } from "ext:deno_node/internal/validators.mjs"; +import { + validateFunction, + validateString, +} from "ext:deno_node/internal/validators.mjs"; import { Buffer } from "ext:deno_node/buffer.ts"; import type { WritableOptions } from "ext:deno_node/_stream.d.ts"; import Writable from "ext:deno_node/internal/streams/writable.mjs"; @@ -17,6 +20,7 @@ import { KeyObject } from "ext:deno_node/internal/crypto/keys.ts"; import { createHash, Hash } from "ext:deno_node/internal/crypto/hash.ts"; import { KeyFormat, KeyType } from "ext:deno_node/internal/crypto/types.ts"; import { isArrayBufferView } from "ext:deno_node/internal/util/types.ts"; +import { ERR_CRYPTO_SIGN_KEY_REQUIRED } from "ext:deno_node/internal/errors.ts"; const { core } = globalThis.__bootstrap; const { ops } = core; @@ -42,7 +46,7 @@ export interface VerifyKeyObjectInput extends SigningOptions { export type KeyLike = string | Buffer | KeyObject; -export class Sign extends Writable { +export class SignImpl extends Writable { hash: Hash; #digestType: string; @@ -103,7 +107,13 @@ export class Sign extends Writable { } } -export class Verify extends Writable { +export function Sign(algorithm: string, options?: WritableOptions) { + return new SignImpl(algorithm, options); +} + +Sign.prototype = SignImpl.prototype; + +export class VerifyImpl extends Writable { hash: Hash; #digestType: string; @@ -165,47 +175,65 @@ export class Verify extends Writable { } } +export function Verify(algorithm: string, options?: WritableOptions) { + return new VerifyImpl(algorithm, options); +} + +Verify.prototype = VerifyImpl.prototype; + export function signOneShot( algorithm: string | null | undefined, data: ArrayBufferView, key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput, -): Buffer; -export function signOneShot( - algorithm: string | null | undefined, - data: ArrayBufferView, - key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput, - callback: (error: Error | null, data: Buffer) => void, -): void; -export function signOneShot( - _algorithm: string | null | undefined, - _data: ArrayBufferView, - _key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput, - _callback?: (error: Error | null, data: Buffer) => void, + callback?: (error: Error | null, data: Buffer) => void, ): Buffer | void { - notImplemented("crypto.sign"); + if (algorithm != null) { + validateString(algorithm, "algorithm"); + } + + if (callback !== undefined) { + validateFunction(callback, "callback"); + } + + if (!key) { + throw new ERR_CRYPTO_SIGN_KEY_REQUIRED(); + } + + const result = Sign(algorithm!).update(data).sign(key); + + if (callback) { + setTimeout(() => callback(null, result)); + } else { + return result; + } } export function verifyOneShot( algorithm: string | null | undefined, - data: ArrayBufferView, + data: BinaryLike, key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput, - signature: ArrayBufferView, -): boolean; -export function verifyOneShot( - algorithm: string | null | undefined, - data: ArrayBufferView, - key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput, - signature: ArrayBufferView, - callback: (error: Error | null, result: boolean) => void, -): void; -export function verifyOneShot( - _algorithm: string | null | undefined, - _data: ArrayBufferView, - _key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput, - _signature: ArrayBufferView, - _callback?: (error: Error | null, result: boolean) => void, + signature: BinaryLike, + callback?: (error: Error | null, result: boolean) => void, ): boolean | void { - notImplemented("crypto.verify"); + if (algorithm != null) { + validateString(algorithm, "algorithm"); + } + + if (callback !== undefined) { + validateFunction(callback, "callback"); + } + + if (!key) { + throw new ERR_CRYPTO_SIGN_KEY_REQUIRED(); + } + + const result = Verify(algorithm!).update(data).verify(key, signature); + + if (callback) { + setTimeout(() => callback(null, result)); + } else { + return result; + } } export default { |