[Bun.js] Implement crypto.randomBytes() and crypto.randomUUID()

This commit is contained in:
Jarred Sumner
2022-02-18 02:37:18 -08:00
parent 17ab2c18eb
commit 7b8de76267
2 changed files with 194 additions and 1 deletions

View File

@@ -90,14 +90,95 @@ pub const GlobalClasses = [_]type{
js_ast.Macro.JSNode.BunJSXCallbackFunction,
Performance.Class,
Crypto.Class,
Crypto.Prototype,
// The last item in this array becomes "process.env"
Bun.EnvironmentVariables.Class,
};
const UUID = @import("./uuid.zig");
const Blob = @import("../../blob.zig");
pub const Buffer = MarkedArrayBuffer;
const Lock = @import("../../lock.zig").Lock;
pub const Crypto = struct {
pub const Class = NewClass(void, .{ .name = "crypto" }, .{
.getRandomValues = .{
.rfn = getRandomValues,
},
.randomUUID = .{
.rfn = randomUUID,
},
}, .{});
pub const Prototype = NewClass(
void,
.{ .name = "Crypto" },
.{
.call = .{
.rfn = call,
},
},
.{},
);
pub fn getRandomValues(
// this
_: void,
ctx: js.JSContextRef,
// function
_: js.JSObjectRef,
// thisObject
_: js.JSObjectRef,
arguments: []const js.JSValueRef,
exception: js.ExceptionRef,
) js.JSValueRef {
if (arguments.len == 0) {
JSError(getAllocator(ctx), "Expected typed array but received nothing", .{}, ctx, exception);
return JSValue.jsUndefined().asObjectRef();
}
var array_buffer = MarkedArrayBuffer.fromJS(ctx.ptr(), JSValue.fromRef(arguments[0]), exception) orelse {
JSError(getAllocator(ctx), "Expected typed array", .{}, ctx, exception);
return JSValue.jsUndefined().asObjectRef();
};
var slice = array_buffer.slice();
if (slice.len > 0)
std.crypto.random.bytes(slice);
return arguments[0];
}
pub fn call(
// this
_: void,
_: js.JSContextRef,
// function
_: js.JSObjectRef,
// thisObject
_: js.JSObjectRef,
_: []const js.JSValueRef,
_: js.ExceptionRef,
) js.JSValueRef {
return JSValue.jsUndefined().asObjectRef();
}
pub fn randomUUID(
// this
_: void,
ctx: js.JSContextRef,
// function
_: js.JSObjectRef,
// thisObject
_: js.JSObjectRef,
_: []const js.JSValueRef,
_: js.ExceptionRef,
) js.JSValueRef {
var uuid = UUID.init();
var out: [128]u8 = undefined;
var str = std.fmt.bufPrint(&out, "{s}", .{uuid}) catch unreachable;
return ZigString.init(str).toValueGC(ctx.ptr()).asObjectRef();
}
};
pub const Bun = struct {
threadlocal var css_imports_list_strings: [512]ZigString = undefined;
threadlocal var css_imports_list: [512]Api.StringPointer = undefined;

112
src/javascript/jsc/uuid.zig Normal file
View File

@@ -0,0 +1,112 @@
//https://github.com/dmgk/zig-uuid
const std = @import("std");
const crypto = std.crypto;
const fmt = std.fmt;
const testing = std.testing;
pub const Error = error{InvalidUUID};
const UUID = @This();
bytes: [16]u8,
pub fn init() UUID {
var uuid = UUID{ .bytes = undefined };
crypto.random.bytes(&uuid.bytes);
// Version 4
uuid.bytes[6] = (uuid.bytes[6] & 0x0f) | 0x40;
// Variant 1
uuid.bytes[8] = (uuid.bytes[8] & 0x3f) | 0x80;
return uuid;
}
// Indices in the UUID string representation for each byte.
const encoded_pos = [16]u8{ 0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34 };
// Hex to nibble mapping.
const hex_to_nibble = [256]u8{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};
pub fn format(
self: UUID,
comptime layout: []const u8,
options: fmt.FormatOptions,
writer: anytype,
) !void {
_ = options; // currently unused
if (layout.len != 0 and layout[0] != 's')
@compileError("Unsupported format specifier for UUID type: '" ++ layout ++ "'.");
var buf: [36]u8 = undefined;
const hex = "0123456789abcdef";
buf[8] = '-';
buf[13] = '-';
buf[18] = '-';
buf[23] = '-';
inline for (encoded_pos) |i, j| {
buf[i + 0] = hex[self.bytes[j] >> 4];
buf[i + 1] = hex[self.bytes[j] & 0x0f];
}
try fmt.format(writer, "{s}", .{buf});
}
pub fn parse(buf: []const u8) Error!UUID {
var uuid = UUID{ .bytes = undefined };
if (buf.len != 36 or buf[8] != '-' or buf[13] != '-' or buf[18] != '-' or buf[23] != '-')
return Error.InvalidUUID;
inline for (encoded_pos) |i, j| {
const hi = hex_to_nibble[buf[i + 0]];
const lo = hex_to_nibble[buf[i + 1]];
if (hi == 0xff or lo == 0xff) {
return Error.InvalidUUID;
}
uuid.bytes[j] = hi << 4 | lo;
}
return uuid;
}
// Zero UUID
pub const zero: UUID = .{ .bytes = .{0} ** 16 };
// Convenience function to return a new v4 UUID.
pub fn newV4() UUID {
return UUID.init();
}