Skip to content

Commit 2815f02

Browse files
authored
Merge pull request #811 from lightpanda-io/crypto-getrandomvalues-return
Return Random Array from crypto.GetRandomValues
2 parents 8bd7c8d + d9c26bb commit 2815f02

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/browser/crypto/crypto.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ const uuidv4 = @import("../../id.zig").uuidv4;
2121

2222
// https://w3c.github.io/webcrypto/#crypto-interface
2323
pub const Crypto = struct {
24-
pub fn _getRandomValues(_: *const Crypto, into: RandomValues) !void {
24+
pub fn _getRandomValues(_: *const Crypto, into: RandomValues) !RandomValues {
2525
const buf = into.asBuffer();
2626
if (buf.len > 65_536) {
2727
return error.QuotaExceededError;
2828
}
2929
std.crypto.random.bytes(buf);
30+
return into;
3031
}
3132

3233
pub fn _randomUUID(_: *const Crypto) [36]u8 {
@@ -69,14 +70,16 @@ test "Browser.Crypto" {
6970
.{ "const a = crypto.randomUUID();", "undefined" },
7071
.{ "const b = crypto.randomUUID();", "undefined" },
7172
.{ "a.length;", "36" },
72-
.{ "a.length;", "36" },
73+
.{ "b.length;", "36" },
7374
.{ "a == b;", "false" },
7475
}, .{});
7576

7677
try runner.testCases(&.{
7778
.{ "try { crypto.getRandomValues(new BigUint64Array(8193)) } catch(e) { e.message == 'QuotaExceededError' }", "true" },
7879
.{ "let r1 = new Int32Array(5)", "undefined" },
79-
.{ "crypto.getRandomValues(r1)", "undefined" },
80+
.{ "let r2 = crypto.getRandomValues(r1)", "undefined" },
8081
.{ "new Set(r1).size", "5" },
82+
.{ "new Set(r2).size", "5" },
83+
.{ "r1.every((v, i) => v === r2[i])", "true" },
8184
}, .{});
8285
}

0 commit comments

Comments
 (0)