diff options
-rw-r--r-- | src/bun.js/crypto.exports.js | 34 | ||||
-rw-r--r-- | test/bun.js/node-crypto.test.js | 8 |
2 files changed, 11 insertions, 31 deletions
diff --git a/src/bun.js/crypto.exports.js b/src/bun.js/crypto.exports.js index cd06879bb..6362a6ea1 100644 --- a/src/bun.js/crypto.exports.js +++ b/src/bun.js/crypto.exports.js @@ -8456,38 +8456,10 @@ var require_brorand = __commonJS({ return this._rand(len); }; Rand.prototype._rand = function (n) { - if (this.rand.getBytes) return this.rand.getBytes(n); - for (var res = new Uint8Array(n), i = 0; i < res.length; i++) - res[i] = this.rand.getByte(); - return res; + var out = new Buffer(n); + crypto.getRandomValues(out); + return out; }; - if (typeof self == "object") - self.crypto && self.crypto.getRandomValues - ? (Rand.prototype._rand = function (n) { - var arr = new Uint8Array(n); - return self.crypto.getRandomValues(arr), arr; - }) - : self.msCrypto && self.msCrypto.getRandomValues - ? (Rand.prototype._rand = function (n) { - var arr = new Uint8Array(n); - return self.msCrypto.getRandomValues(arr), arr; - }) - : typeof window == "object" && - (Rand.prototype._rand = function () { - throw new Error("Not implemented yet"); - }); - else - try { - if ( - ((crypto2 = require_crypto_browserify()), - typeof crypto2.randomBytes != "function") - ) - throw new Error("Not supported"); - Rand.prototype._rand = function (n) { - return crypto2.randomBytes(n); - }; - } catch {} - var crypto2; }, }); diff --git a/test/bun.js/node-crypto.test.js b/test/bun.js/node-crypto.test.js new file mode 100644 index 000000000..edd6f6e3d --- /dev/null +++ b/test/bun.js/node-crypto.test.js @@ -0,0 +1,8 @@ +import { it, expect } from "bun:test"; + +const nodeCrypto = require("node:crypto"); + +it("crypto.randomBytes should return a Buffer", () => { + expect(nodeCrypto.randomBytes(1) instanceof Buffer).toBe(true); + expect(Buffer.isBuffer(nodeCrypto.randomBytes(1))).toBe(true); +}); |