mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
* Add a zig fmt action * add failing file * Setup prettier better * Update prettier-fmt.yml * Fail on error * Update prettier-fmt.yml * boop * boop2 * tar.gz * Update zig-fmt.yml * Update zig-fmt.yml * Update zig-fmt.yml * Update zig-fmt.yml * Update zig-fmt.yml * boop * Update prettier-fmt.yml * tag * newlines * multiline * fixup * Update zig-fmt.yml * update it * fixup * both * w * Update prettier-fmt.yml * prettier all the things * Update package.json * zig fmt * ❌ ✅ * bump * . * quotes * fix prettier ignore * once more * Update prettier-fmt.yml * Update fallback.ts * consistentcy --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
82 lines
2.3 KiB
JavaScript
82 lines
2.3 KiB
JavaScript
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);
|
|
};
|
|
|
|
export const randomUUID = () => {
|
|
return crypto.randomUUID();
|
|
};
|
|
|
|
export const timingSafeEqual =
|
|
"timingSafeEqual" in crypto
|
|
? (a, b) => {
|
|
const { byteLength: byteLengthA } = a;
|
|
const { byteLength: byteLengthB } = b;
|
|
if (typeof byteLengthA !== "number" || typeof byteLengthB !== "number") {
|
|
throw new TypeError("Input must be an array buffer view");
|
|
}
|
|
|
|
if (byteLengthA !== byteLengthB) {
|
|
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;
|