summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYoshiya Hinosawa <stibium121@gmail.com>2023-04-19 23:24:26 +0900
committerGitHub <noreply@github.com>2023-04-19 23:24:26 +0900
commitfdebb7e7930b175b5dd80f891253000a29c82a4a (patch)
tree7035fe982f41b6b8e64de20b790c57379cd2851a
parent5a77bb884416753b85f2acecd4895c75f1c53187 (diff)
fix(ext/node): add crypto.sign|verify methods (#18765)
-rw-r--r--cli/tests/unit_node/crypto_sign_test.ts34
-rw-r--r--ext/node/polyfills/internal/crypto/sig.ts94
2 files changed, 87 insertions, 41 deletions
diff --git a/cli/tests/unit_node/crypto_sign_test.ts b/cli/tests/unit_node/crypto_sign_test.ts
index 1016d0f3e..9d346e7d0 100644
--- a/cli/tests/unit_node/crypto_sign_test.ts
+++ b/cli/tests/unit_node/crypto_sign_test.ts
@@ -4,7 +4,7 @@ import {
assert,
assertEquals,
} from "../../../test_util/std/testing/asserts.ts";
-import { createSign, createVerify } from "node:crypto";
+import { createSign, createVerify, sign, verify } from "node:crypto";
import { Buffer } from "node:buffer";
const rsaPrivatePem = Buffer.from(
@@ -41,32 +41,50 @@ const table = [
},
];
+const data = Buffer.from("some data to sign");
+
Deno.test({
- name: "crypto.Sign - RSA PEM with SHA224, SHA256, SHA384, SHA512 digests",
+ name:
+ "crypto.Sign|sign - RSA PEM with SHA224, SHA256, SHA384, SHA512 digests",
fn() {
for (const testCase of table) {
for (const algorithm of testCase.algorithms) {
- const signature = createSign(algorithm)
- .update("some data to sign")
- .sign(rsaPrivatePem, "hex");
- assertEquals(signature, testCase.signature);
+ assertEquals(
+ createSign(algorithm)
+ .update(data)
+ .sign(rsaPrivatePem, "hex"),
+ testCase.signature,
+ );
+ assertEquals(
+ sign(algorithm, data, rsaPrivatePem),
+ Buffer.from(testCase.signature, "hex"),
+ );
}
}
},
});
Deno.test({
- name: "crypto.Verify - RSA PEM with SHA224, SHA256, SHA384, SHA512 digests",
+ name:
+ "crypto.Verify|verify - RSA PEM with SHA224, SHA256, SHA384, SHA512 digests",
fn() {
for (const testCase of table) {
for (const algorithm of testCase.algorithms) {
assert(
- createVerify(algorithm).update("some data to sign").verify(
+ createVerify(algorithm).update(data).verify(
rsaPublicPem,
testCase.signature,
"hex",
),
);
+ assert(
+ verify(
+ algorithm,
+ data,
+ rsaPublicPem,
+ Buffer.from(testCase.signature, "hex"),
+ ),
+ );
}
}
},
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 {