aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-07-16 22:42:19 -0700
committerGravatar GitHub <noreply@github.com> 2023-07-16 22:42:19 -0700
commit7917ebd58f852685f5b8abc50541747bfbb2ee13 (patch)
tree8b07c61945b428583e8f2cdd9d59d3399811e8e4
parenta86d00c6728f0fdab2f9302c3e02bc712df4ba0c (diff)
downloadbun-7917ebd58f852685f5b8abc50541747bfbb2ee13.tar.gz
bun-7917ebd58f852685f5b8abc50541747bfbb2ee13.tar.zst
bun-7917ebd58f852685f5b8abc50541747bfbb2ee13.zip
Fixes base64url encoding for crypto (#3654)
* Fixes base64url encoding for crypto * :green_apple: * Update nodejs-apis.md --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r--docs/runtime/nodejs-apis.md3
-rw-r--r--src/bun.js/node/types.zig17
-rw-r--r--test/js/node/crypto/node-crypto.test.js4
3 files changed, 11 insertions, 13 deletions
diff --git a/docs/runtime/nodejs-apis.md b/docs/runtime/nodejs-apis.md
index db4844b7e..79b01ebff 100644
--- a/docs/runtime/nodejs-apis.md
+++ b/docs/runtime/nodejs-apis.md
@@ -26,8 +26,7 @@ This page is updated regularly to reflect compatibility status of the latest ver
---
- {% anchor id="node_buffer" %} [`node:buffer`](https://nodejs.org/api/buffer.html) {% /anchor %}
-- 🟡
-- Incomplete implementation of `base64` and `base64url` encodings.
+- 🟢
---
diff --git a/src/bun.js/node/types.zig b/src/bun.js/node/types.zig
index e035c01a6..0b19c7fcc 100644
--- a/src/bun.js/node/types.zig
+++ b/src/bun.js/node/types.zig
@@ -528,12 +528,10 @@ pub const Encoding = enum(u8) {
return JSC.ZigString.init(base64[0..len]).toValueGC(globalThis);
},
.base64url => {
- var buf: [std.base64.url_safe.Encoder.calcSize(size) + "data:;base64,".len]u8 = undefined;
- var encoded = std.base64.url_safe.Encoder.encode(buf["data:;base64,".len..], input);
- buf[0.."data:;base64,".len].* = "data:;base64,".*;
+ var buf: [std.base64.url_safe_no_pad.Encoder.calcSize(size)]u8 = undefined;
+ var encoded = std.base64.url_safe_no_pad.Encoder.encode(&buf, input);
- const result = JSC.ZigString.init(buf[0 .. "data:;base64,".len + encoded.len]).toValueGC(globalThis);
- return result;
+ return JSC.ZigString.init(buf[0..encoded.len]).toValueGC(globalThis);
},
.hex => {
var buf: [size * 4]u8 = undefined;
@@ -566,13 +564,10 @@ pub const Encoding = enum(u8) {
return result;
},
.base64url => {
- var buf_: [std.base64.url_safe.Encoder.calcSize(max_size) + "data:;base64,".len]u8 = undefined;
- var buf = buf_[0 .. std.base64.url_safe.Encoder.calcSize(size) + "data:;base64,".len];
- var encoded = std.base64.url_safe.Encoder.encode(buf["data:;base64,".len..], input);
- buf[0.."data:;base64,".len].* = "data:;base64,".*;
+ var buf: [std.base64.url_safe_no_pad.Encoder.calcSize(max_size)]u8 = undefined;
+ var encoded = std.base64.url_safe_no_pad.Encoder.encode(&buf, input);
- const result = JSC.ZigString.init(buf[0 .. "data:;base64,".len + encoded.len]).toValueGC(globalThis);
- return result;
+ return JSC.ZigString.init(buf[0..encoded.len]).toValueGC(globalThis);
},
.hex => {
var buf: [max_size * 4]u8 = undefined;
diff --git a/test/js/node/crypto/node-crypto.test.js b/test/js/node/crypto/node-crypto.test.js
index 2489f96c7..8db87d9f3 100644
--- a/test/js/node/crypto/node-crypto.test.js
+++ b/test/js/node/crypto/node-crypto.test.js
@@ -59,6 +59,10 @@ describe("createHash", () => {
97, 105, 50, 105, 67, 85, 79, 84, 72, 112, 103, 48, 47, 66, 76, 80, 53, 98, 116, 72, 117, 57, 109, 117, 81, 48,
105, 97, 77, 72, 74, 112, 89, 114, 86, 50, 57, 79, 79, 90, 80, 108, 65, 61,
],
+ base64url: [
+ 97, 105, 50, 105, 67, 85, 79, 84, 72, 112, 103, 48, 95, 66, 76, 80, 53, 98, 116, 72, 117, 57, 109, 117, 81, 48,
+ 105, 97, 77, 72, 74, 112, 89, 114, 86, 50, 57, 79, 79, 90, 80, 108, 65,
+ ],
hex: [
54, 97, 50, 100, 97, 50, 48, 57, 52, 51, 57, 51, 49, 101, 57, 56, 51, 52, 102, 99, 49, 50, 99, 102, 101, 53, 98,
98, 52, 55, 98, 98, 100, 57, 97, 101, 52, 51, 52, 56, 57, 97, 51, 48, 55, 50, 54, 57, 54, 50, 98, 53, 55, 54, 102,