|
| 1 | +// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) |
| 2 | +// |
| 3 | +// Francis Bouvier <[email protected]> |
| 4 | +// Pierre Tachoire <[email protected]> |
| 5 | +// |
| 6 | +// This program is free software: you can redistribute it and/or modify |
| 7 | +// it under the terms of the GNU Affero General Public License as |
| 8 | +// published by the Free Software Foundation, either version 3 of the |
| 9 | +// License, or (at your option) any later version. |
| 10 | +// |
| 11 | +// This program is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU Affero General Public License for more details. |
| 15 | +// |
| 16 | +// You should have received a copy of the GNU Affero General Public License |
| 17 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +const std = @import("std"); |
| 20 | +const uuidv4 = @import("../../id.zig").uuidv4; |
| 21 | + |
| 22 | +// https://w3c.github.io/webcrypto/#crypto-interface |
| 23 | +pub const Crypto = struct { |
| 24 | + pub fn _getRandomValues(_: *const Crypto, into: RandomValues) !void { |
| 25 | + const buf = into.asBuffer(); |
| 26 | + if (buf.len > 65_536) { |
| 27 | + return error.QuotaExceededError; |
| 28 | + } |
| 29 | + std.crypto.random.bytes(buf); |
| 30 | + } |
| 31 | + |
| 32 | + pub fn _randomUUID(_: *const Crypto) [36]u8 { |
| 33 | + var hex: [36]u8 = undefined; |
| 34 | + uuidv4(&hex); |
| 35 | + return hex; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +const RandomValues = union(enum) { |
| 40 | + int8: []i8, |
| 41 | + uint8: []u8, |
| 42 | + int16: []i16, |
| 43 | + uint16: []u16, |
| 44 | + int32: []i32, |
| 45 | + uint32: []u32, |
| 46 | + int64: []i64, |
| 47 | + uint64: []u64, |
| 48 | + |
| 49 | + fn asBuffer(self: RandomValues) []u8 { |
| 50 | + switch (self) { |
| 51 | + .int8 => |b| return (@as([]u8, @ptrCast(b)))[0..b.len], |
| 52 | + .uint8 => |b| return (@as([]u8, @ptrCast(b)))[0..b.len], |
| 53 | + .int16 => |b| return (@as([]u8, @ptrCast(b)))[0 .. b.len * 2], |
| 54 | + .uint16 => |b| return (@as([]u8, @ptrCast(b)))[0 .. b.len * 2], |
| 55 | + .int32 => |b| return (@as([]u8, @ptrCast(b)))[0 .. b.len * 4], |
| 56 | + .uint32 => |b| return (@as([]u8, @ptrCast(b)))[0 .. b.len * 4], |
| 57 | + .int64 => |b| return (@as([]u8, @ptrCast(b)))[0 .. b.len * 8], |
| 58 | + .uint64 => |b| return (@as([]u8, @ptrCast(b)))[0 .. b.len * 8], |
| 59 | + } |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +const testing = @import("../../testing.zig"); |
| 64 | +test "Browser.Crypto" { |
| 65 | + var runner = try testing.jsRunner(testing.tracking_allocator, .{}); |
| 66 | + defer runner.deinit(); |
| 67 | + |
| 68 | + try runner.testCases(&.{ |
| 69 | + .{ "const a = crypto.randomUUID();", "undefined" }, |
| 70 | + .{ "const b = crypto.randomUUID();", "undefined" }, |
| 71 | + .{ "a.length;", "36" }, |
| 72 | + .{ "a.length;", "36" }, |
| 73 | + .{ "a == b;", "false" }, |
| 74 | + }, .{}); |
| 75 | + |
| 76 | + try runner.testCases(&.{ |
| 77 | + .{ "try { crypto.getRandomValues(new BigUint64Array(8193)) } catch(e) { e.message == 'QuotaExceededError' }", "true" }, |
| 78 | + .{ "let r1 = new Int32Array(5)", "undefined" }, |
| 79 | + .{ "crypto.getRandomValues(r1)", "undefined" }, |
| 80 | + .{ "new Set(r1).size", "5" }, |
| 81 | + }, .{}); |
| 82 | +} |
0 commit comments