Skip to content

Feat/btoa uri pbkdf2#104

Merged
cs01 merged 9 commits intomainfrom
feat/btoa-uri-pbkdf2
Mar 7, 2026
Merged

Feat/btoa uri pbkdf2#104
cs01 merged 9 commits intomainfrom
feat/btoa-uri-pbkdf2

Conversation

@cs01
Copy link
Owner

@cs01 cs01 commented Mar 6, 2026

feat: btoa/atob, encodeURIComponent, crypto.pbkdf2

Adds several Web API globals and a key derivation primitive: btoa/atob, encodeURIComponent/decodeURIComponent, and crypto.pbkdf2. Includes a new uri-bridge.c for percent-encoding, docs for all new APIs plus URL/URLSearchParams (merged separately), and CI fixes for artifact packaging and cross-compile reliability.

What's new

btoa / atob

const encoded = btoa("hello world");  // "aGVsbG8gd29ybGQ="
const decoded = atob(encoded);        // "hello world"

const auth = "Basic " + btoa(username + ":" + password);

encodeURIComponent / decodeURIComponent

const q = encodeURIComponent("hello world & foo=bar");
// "hello%20world%20%26%20foo%3Dbar"

const url = "https://api.example.com/search?q=" + encodeURIComponent(userInput);

RFC 3986 compliant — only unreserved chars (A-Z a-z 0-9 - _ . ~) are left unencoded.

crypto.pbkdf2(password, salt, iterations, keylen)

const key = crypto.pbkdf2(userPassword, randomSalt, 100000, 32);
// 64-char hex string (32 bytes)

Uses OpenSSL PKCS5_PBKDF2_HMAC with SHA-256. Known test vector: pbkdf2("password", "salt", 1, 20)"0c60c80f961f0e71f3a9b524af6012062fe037a6".

Implementation

  • c_bridges/base64-bridge.c — added cs_btoa and cs_atob to existing base64 bridge. Handles standard base64 with padding.
  • c_bridges/uri-bridge.c (new) — cs_encode_uri_component and cs_decode_uri_component using GC-aware allocation.
  • src/codegen/expressions/calls.ts — dispatch for btoa, atob, encodeURIComponent, decodeURIComponent as global functions (same pattern as parseInt/isNaN).
  • src/codegen/stdlib/crypto.tsgeneratePbkdf2 using OpenSSL PKCS5 PBKDF2 HMAC.

CI fixes

  • scripts/build-target-sdk.sh — add uri-bridge.o to cross-compile SDK tarball.
  • .github/workflows/ci.yml — add uri-bridge.o to all 4 artifact locations (Linux verify, Linux release, macOS verify, macOS release). Also add --target-cpu=x86-64 to stage0 compilation so native code doesn't use AVX/newer instructions that crash GitHub's baseline x86-64 runners.
  • tests/self-hosting.test.ts — pass --target-cpu=x86-64 to stage0 build.

Docs

  • docs/stdlib/encoding.md (new) — btoa/atob, encodeURIComponent/decodeURIComponent
  • docs/stdlib/crypto.md — added crypto.pbkdf2 section
  • docs/stdlib/url.md (new) — URL and URLSearchParams (feature landed in add url and urlsearchparams classes with c bridge #107, docs here)
  • docs/stdlib/index.md — updated globals example

@cs01 cs01 force-pushed the feat/btoa-uri-pbkdf2 branch 2 times, most recently from 974da3f to 8c82bea Compare March 6, 2026 22:58
@cs01 cs01 force-pushed the feat/btoa-uri-pbkdf2 branch from e048970 to d27aa71 Compare March 7, 2026 00:06
@cs01 cs01 merged commit 75b2afd into main Mar 7, 2026
12 checks passed
@cs01 cs01 deleted the feat/btoa-uri-pbkdf2 branch March 7, 2026 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant