aboutsummaryrefslogtreecommitdiff
path: root/src/node-fallbacks/crypto.js
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-12-13 00:15:29 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-12-13 00:15:29 -0800
commit5741d4f9b41ae136b315b55260175135aa12b2a0 (patch)
tree541cb575f585ce02276a080d333a6c840d9ff8c0 /src/node-fallbacks/crypto.js
parent1bed749d8cdc83bbc53636b11b552b4ed0b2264f (diff)
downloadbun-5741d4f9b41ae136b315b55260175135aa12b2a0.tar.gz
bun-5741d4f9b41ae136b315b55260175135aa12b2a0.tar.zst
bun-5741d4f9b41ae136b315b55260175135aa12b2a0.zip
[crypto] Implement `scryptSync`
Fixes https://github.com/oven-sh/bun/issues/1228
Diffstat (limited to 'src/node-fallbacks/crypto.js')
-rw-r--r--src/node-fallbacks/crypto.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/node-fallbacks/crypto.js b/src/node-fallbacks/crypto.js
index 0f428df13..4a0e4c735 100644
--- a/src/node-fallbacks/crypto.js
+++ b/src/node-fallbacks/crypto.js
@@ -1,5 +1,7 @@
export * from "crypto-browserify";
+export var DEFAULT_ENCODING = "buffer";
+
// we deliberately reference crypto. directly here because we want to preserve the This binding
export const getRandomValues = (array) => {
return crypto.getRandomValues(array);
@@ -25,15 +27,62 @@ export const timingSafeEqual =
throw new RangeError("Input buffers must have the same length");
}
+ // these error checks are also performed in the function
+ // however there is a bug where exceptions return no value
return crypto.timingSafeEqual(a, b);
}
: undefined;
+export const scryptSync =
+ "scryptSync" in crypto
+ ? (password, salt, keylen, options) => {
+ const res = crypto.scryptSync(password, salt, keylen, options);
+ return DEFAULT_ENCODING !== "buffer"
+ ? new Buffer(res).toString(DEFAULT_ENCODING)
+ : new Buffer(res);
+ }
+ : undefined;
+
+export const scrypt =
+ "scryptSync" in crypto
+ ? function (password, salt, keylen, options, callback) {
+ if (typeof options === "function") {
+ callback = options;
+ options = undefined;
+ }
+
+ if (typeof callback !== "function") {
+ var err = new TypeError("callback must be a function");
+ err.code = "ERR_INVALID_CALLBACK";
+ throw err;
+ }
+
+ try {
+ const result = crypto.scryptSync(password, salt, keylen, options);
+ process.nextTick(
+ callback,
+ null,
+ DEFAULT_ENCODING !== "buffer"
+ ? new Buffer(result).toString(DEFAULT_ENCODING)
+ : new Buffer(result),
+ );
+ } catch (err) {
+ throw err;
+ }
+ }
+ : undefined;
+
if (timingSafeEqual) {
// hide it from stack trace
Object.defineProperty(timingSafeEqual, "name", {
value: "::bunternal::",
});
+ Object.defineProperty(scrypt, "name", {
+ value: "::bunternal::",
+ });
+ Object.defineProperty(scryptSync, "name", {
+ value: "::bunternal::",
+ });
}
export const webcrypto = crypto;