2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-02-14 11:38:45 -05:00
|
|
|
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
|
|
|
|
|
2023-06-27 02:18:22 -04:00
|
|
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
|
|
|
// deno-lint-ignore-file prefer-primordials
|
|
|
|
|
2024-01-29 08:58:08 -05:00
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_node_create_hash,
|
|
|
|
op_node_get_hashes,
|
|
|
|
op_node_hash_clone,
|
|
|
|
op_node_hash_digest,
|
2024-01-29 08:58:08 -05:00
|
|
|
op_node_hash_digest_hex,
|
2024-01-10 17:37:25 -05:00
|
|
|
op_node_hash_update,
|
2024-01-29 08:58:08 -05:00
|
|
|
op_node_hash_update_str,
|
|
|
|
} from "ext:core/ops";
|
2024-06-24 05:47:12 -04:00
|
|
|
import { primordials } from "ext:core/mod.js";
|
2024-01-10 17:37:25 -05:00
|
|
|
|
2023-07-02 14:19:30 -04:00
|
|
|
import { Buffer } from "node:buffer";
|
|
|
|
import { Transform } from "node:stream";
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
|
|
|
forgivingBase64Encode as encodeToBase64,
|
|
|
|
forgivingBase64UrlEncode as encodeToBase64Url,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_web/00_infra.js";
|
|
|
|
import type { TransformOptions } from "ext:deno_node/_stream.d.ts";
|
2024-06-24 05:47:12 -04:00
|
|
|
import {
|
|
|
|
validateEncoding,
|
|
|
|
validateString,
|
|
|
|
validateUint32,
|
|
|
|
} from "ext:deno_node/internal/validators.mjs";
|
2023-02-14 11:38:45 -05:00
|
|
|
import type {
|
|
|
|
BinaryToTextEncoding,
|
|
|
|
Encoding,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/internal/crypto/types.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
2023-03-24 10:13:26 -04:00
|
|
|
getKeyMaterial,
|
2023-02-14 11:38:45 -05:00
|
|
|
KeyObject,
|
|
|
|
prepareSecretKey,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/internal/crypto/keys.ts";
|
2024-06-24 05:47:12 -04:00
|
|
|
import {
|
|
|
|
ERR_CRYPTO_HASH_FINALIZED,
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
NodeError,
|
|
|
|
} from "ext:deno_node/internal/errors.ts";
|
|
|
|
import LazyTransform from "ext:deno_node/internal/streams/lazy_transform.mjs";
|
|
|
|
import {
|
|
|
|
getDefaultEncoding,
|
|
|
|
toBuf,
|
|
|
|
} from "ext:deno_node/internal/crypto/util.ts";
|
|
|
|
import { isArrayBufferView } from "ext:deno_node/internal/util/types.ts";
|
|
|
|
|
|
|
|
const { ReflectApply, ObjectSetPrototypeOf } = primordials;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-03-05 22:28:04 -05:00
|
|
|
function unwrapErr(ok: boolean) {
|
2024-06-24 05:47:12 -04:00
|
|
|
if (!ok) throw new ERR_CRYPTO_HASH_FINALIZED();
|
2023-03-05 22:28:04 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 05:47:12 -04:00
|
|
|
declare const __hasher: unique symbol;
|
|
|
|
type Hasher = { __hasher: typeof __hasher };
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2024-06-24 05:47:12 -04:00
|
|
|
const kHandle = Symbol("kHandle");
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2024-06-24 05:47:12 -04:00
|
|
|
export function Hash(
|
|
|
|
this: Hash,
|
|
|
|
algorithm: string | Hasher,
|
|
|
|
options?: { outputLength?: number },
|
|
|
|
): Hash {
|
|
|
|
if (!(this instanceof Hash)) {
|
|
|
|
return new Hash(algorithm, options);
|
|
|
|
}
|
|
|
|
if (!(typeof algorithm === "object")) {
|
|
|
|
validateString(algorithm, "algorithm");
|
|
|
|
}
|
|
|
|
const xofLen = typeof options === "object" && options !== null
|
|
|
|
? options.outputLength
|
|
|
|
: undefined;
|
|
|
|
if (xofLen !== undefined) {
|
|
|
|
validateUint32(xofLen, "options.outputLength");
|
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2024-06-24 05:47:12 -04:00
|
|
|
try {
|
|
|
|
this[kHandle] = typeof algorithm === "object"
|
|
|
|
? op_node_hash_clone(algorithm, xofLen)
|
|
|
|
: op_node_create_hash(algorithm.toLowerCase(), xofLen);
|
|
|
|
} catch (err) {
|
|
|
|
// TODO(lucacasonato): don't do this
|
|
|
|
if (err.message === "Output length mismatch for non-extendable algorithm") {
|
|
|
|
throw new NodeError(
|
|
|
|
"ERR_OSSL_EVP_NOT_XOF_OR_INVALID_LENGTH",
|
|
|
|
"Invalid XOF digest length",
|
2023-02-14 11:38:45 -05:00
|
|
|
);
|
|
|
|
} else {
|
2024-06-24 05:47:12 -04:00
|
|
|
throw err;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
2024-06-24 05:47:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this[kHandle] === null) throw new ERR_CRYPTO_HASH_FINALIZED();
|
|
|
|
|
|
|
|
ReflectApply(LazyTransform, this, [options]);
|
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2024-06-24 05:47:12 -04:00
|
|
|
interface Hash {
|
|
|
|
[kHandle]: object;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectSetPrototypeOf(Hash.prototype, LazyTransform.prototype);
|
|
|
|
ObjectSetPrototypeOf(Hash, LazyTransform);
|
|
|
|
|
|
|
|
Hash.prototype.copy = function copy(options?: { outputLength: number }) {
|
|
|
|
return new Hash(this[kHandle], options);
|
|
|
|
};
|
|
|
|
|
|
|
|
Hash.prototype._transform = function _transform(
|
|
|
|
chunk: string | Buffer,
|
|
|
|
encoding: Encoding | "buffer",
|
|
|
|
callback: () => void,
|
|
|
|
) {
|
|
|
|
this.update(chunk, encoding);
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
|
|
|
Hash.prototype._flush = function _flush(callback: () => void) {
|
|
|
|
this.push(this.digest());
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
|
|
|
Hash.prototype.update = function update(
|
|
|
|
data: string | Buffer,
|
|
|
|
encoding: Encoding | "buffer",
|
|
|
|
) {
|
|
|
|
encoding = encoding || getDefaultEncoding();
|
|
|
|
|
|
|
|
if (typeof data === "string") {
|
|
|
|
validateEncoding(data, encoding);
|
|
|
|
} else if (!isArrayBufferView(data)) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
"data",
|
|
|
|
["string", "Buffer", "TypedArray", "DataView"],
|
|
|
|
data,
|
|
|
|
);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 05:47:12 -04:00
|
|
|
if (
|
|
|
|
typeof data === "string" && (encoding === "utf8" || encoding === "buffer")
|
|
|
|
) {
|
|
|
|
unwrapErr(op_node_hash_update_str(this[kHandle], data));
|
|
|
|
} else {
|
|
|
|
unwrapErr(op_node_hash_update(this[kHandle], toBuf(data, encoding)));
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 05:47:12 -04:00
|
|
|
return this;
|
|
|
|
};
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2024-06-24 05:47:12 -04:00
|
|
|
Hash.prototype.digest = function digest(outputEncoding: Encoding | "buffer") {
|
|
|
|
outputEncoding = outputEncoding || getDefaultEncoding();
|
|
|
|
outputEncoding = `${outputEncoding}`;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2024-06-24 05:47:12 -04:00
|
|
|
if (outputEncoding === "hex") {
|
|
|
|
const result = op_node_hash_digest_hex(this[kHandle]);
|
|
|
|
if (result === null) throw new ERR_CRYPTO_HASH_FINALIZED();
|
|
|
|
return result;
|
|
|
|
}
|
2023-03-05 22:28:04 -05:00
|
|
|
|
2024-06-24 05:47:12 -04:00
|
|
|
const digest = op_node_hash_digest(this[kHandle]);
|
|
|
|
if (digest === null) throw new ERR_CRYPTO_HASH_FINALIZED();
|
|
|
|
|
|
|
|
// TODO(@littedivy): Fast paths for below encodings.
|
|
|
|
switch (outputEncoding) {
|
|
|
|
case "binary":
|
|
|
|
return String.fromCharCode(...digest);
|
|
|
|
case "base64":
|
|
|
|
return encodeToBase64(digest);
|
|
|
|
case "base64url":
|
|
|
|
return encodeToBase64Url(digest);
|
|
|
|
case undefined:
|
|
|
|
case "buffer":
|
2023-02-14 11:38:45 -05:00
|
|
|
return Buffer.from(digest);
|
2024-06-24 05:47:12 -04:00
|
|
|
default:
|
|
|
|
return Buffer.from(digest).toString(outputEncoding);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
2024-06-24 05:47:12 -04:00
|
|
|
};
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
export function Hmac(
|
|
|
|
hmac: string,
|
|
|
|
key: string | ArrayBuffer | KeyObject,
|
|
|
|
options?: TransformOptions,
|
|
|
|
): Hmac {
|
|
|
|
return new HmacImpl(hmac, key, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
type Hmac = HmacImpl;
|
|
|
|
|
|
|
|
class HmacImpl extends Transform {
|
|
|
|
#ipad: Uint8Array;
|
|
|
|
#opad: Uint8Array;
|
|
|
|
#ZEROES = Buffer.alloc(128);
|
|
|
|
#algorithm: string;
|
|
|
|
#hash: Hash;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
hmac: string,
|
|
|
|
key: string | ArrayBuffer | KeyObject,
|
|
|
|
options?: TransformOptions,
|
|
|
|
) {
|
|
|
|
super({
|
|
|
|
transform(chunk: string, encoding: string, callback: () => void) {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2024-06-24 05:47:12 -04:00
|
|
|
self.update(Buffer.from(chunk), encoding as any);
|
2023-02-14 11:38:45 -05:00
|
|
|
callback();
|
|
|
|
},
|
|
|
|
flush(callback: () => void) {
|
|
|
|
this.push(self.digest());
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
// deno-lint-ignore no-this-alias
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
validateString(hmac, "hmac");
|
2023-03-24 10:13:26 -04:00
|
|
|
|
|
|
|
const u8Key = key instanceof KeyObject
|
|
|
|
? getKeyMaterial(key)
|
|
|
|
: prepareSecretKey(key, options?.encoding) as Buffer;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
const alg = hmac.toLowerCase();
|
|
|
|
this.#algorithm = alg;
|
|
|
|
const blockSize = (alg === "sha512" || alg === "sha384") ? 128 : 64;
|
|
|
|
const keySize = u8Key.length;
|
|
|
|
|
|
|
|
let bufKey: Buffer;
|
|
|
|
|
|
|
|
if (keySize > blockSize) {
|
2023-03-16 17:25:12 -04:00
|
|
|
const hash = new Hash(alg, options);
|
|
|
|
bufKey = hash.update(u8Key).digest() as Buffer;
|
2023-02-14 11:38:45 -05:00
|
|
|
} else {
|
|
|
|
bufKey = Buffer.concat([u8Key, this.#ZEROES], blockSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#ipad = Buffer.allocUnsafe(blockSize);
|
|
|
|
this.#opad = Buffer.allocUnsafe(blockSize);
|
|
|
|
|
|
|
|
for (let i = 0; i < blockSize; i++) {
|
|
|
|
this.#ipad[i] = bufKey[i] ^ 0x36;
|
|
|
|
this.#opad[i] = bufKey[i] ^ 0x5C;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#hash = new Hash(alg);
|
|
|
|
this.#hash.update(this.#ipad);
|
|
|
|
}
|
|
|
|
|
|
|
|
digest(): Buffer;
|
|
|
|
digest(encoding: BinaryToTextEncoding): string;
|
|
|
|
digest(encoding?: BinaryToTextEncoding): Buffer | string {
|
|
|
|
const result = this.#hash.digest();
|
|
|
|
|
2024-06-24 05:47:12 -04:00
|
|
|
return new Hash(this.#algorithm).update(this.#opad).update(result)
|
|
|
|
.digest(
|
|
|
|
encoding,
|
|
|
|
);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
update(data: string | ArrayBuffer, inputEncoding?: Encoding): this {
|
|
|
|
this.#hash.update(data, inputEncoding);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Hmac.prototype = HmacImpl.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates and returns a Hash object that can be used to generate hash digests
|
|
|
|
* using the given `algorithm`. Optional `options` argument controls stream behavior.
|
|
|
|
*/
|
|
|
|
export function createHash(algorithm: string, opts?: TransformOptions) {
|
|
|
|
return new Hash(algorithm, opts);
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:04:49 -04:00
|
|
|
/**
|
|
|
|
* Get the list of implemented hash algorithms.
|
|
|
|
* @returns Array of hash algorithm names.
|
|
|
|
*/
|
|
|
|
export function getHashes() {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_node_get_hashes();
|
2023-06-26 22:04:49 -04:00
|
|
|
}
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
export default {
|
|
|
|
Hash,
|
|
|
|
Hmac,
|
|
|
|
createHash,
|
|
|
|
};
|