parse-base64-like-crypto-js
v0.1.0
Published
Parse Base64 and Base64URL like crypto-js, returns Uint8Array.
Maintainers
Readme
Parse Base64 and Base64URL like crypto-js, returns Uint8Array.
This library implements the following logic:
- Invalid characters in the input are treated as 0 (equivalent to 'A').
- When parsing Base64, the '=' in the input is treated as a terminator; it and any characters after it will be ignored.
- If the last block has only 1 character, that character will be discarded.
Built for @openccw/sb3-crypto.
Setup
npm
npm i parse-base64-like-crypto-js@latestimport parseBase64 from "parse-base64-like-crypto-js";Other Package Managers
You can also use other package managers (e.g. pnpm or yarn) in place of npm.
jsDelivr
See https://www.jsdelivr.com/package/npm/parse-base64-like-crypto-js
<script src="https://cdn.jsdelivr.net/npm/parse-base64-like-crypto-js"></script>UNPKG
<script src="https://unpkg.com/parse-base64-like-crypto-js"></script>Inline
You can embed browser.min.js directly into your script.
It will define the parseBase64 function using var.
Example
// Base64
parseBase64("ABCD.EFG0 12+/+/====")
// Base64URL
// It treats '=' as 0, not as a terminator.
parseBase64("ABCD.EFG0 12-_-_====", true)
// parse Base64 from bytes
var b64 = Uint8Array.of(65,66,67,68,46,69,70,71,48,32,49,50,43,47,43,47,61,61,61,61)
parseBase64(b64)
// parse Base64 with output length
parseBase64("KzdnFCBRvq3e5516c98e2044f55d85b07a4db4a185f", false, 32)It will return a Uint8Array.
Benchmark
> pnpm benchmark
$ node scripts/benchmark.mjs
==============================================================================
Benchmark: parse-base64-like-crypto-js vs crypto-js
Node.js: v26.8.1 | OS: win32 x64
==============================================================================
Scenario: Small string (32 B decoded, ~44 chars Base64)
crypto-js (parse): 399,466 ops/s ( 2.50 µs/op)
crypto-js (parse -> Uint8Array): 292,563 ops/s ( 3.42 µs/op)
parseBase64: 7,144,176 ops/s ( 140 ns/op) -> 17.88x (vs parse) / 24.42x (vs full)
Scenario: Medium string (1 KB decoded, ~1.37 KB Base64)
crypto-js (parse): 114,822 ops/s ( 8.71 µs/op)
crypto-js (parse -> Uint8Array): 87,560 ops/s ( 11.42 µs/op)
parseBase64: 264,885 ops/s ( 3.78 µs/op) -> 2.31x (vs parse) / 3.03x (vs full)
Scenario: Large string (64 KB decoded, ~87 KB Base64)
crypto-js (parse): 2,076 ops/s ( 481.69 µs/op)
crypto-js (parse -> Uint8Array): 1,732 ops/s ( 577.26 µs/op)
parseBase64: 4,653 ops/s ( 214.90 µs/op) -> 2.24x (vs parse) / 2.69x (vs full)
Scenario: Base64URL string (1 KB decoded)
crypto-js (parse): 95,224 ops/s ( 10.50 µs/op)
crypto-js (parse -> Uint8Array): 81,858 ops/s ( 12.22 µs/op)
parseBase64: 285,898 ops/s ( 3.50 µs/op) -> 3.00x (vs parse) / 3.49x (vs full)
Scenario: Uint8Array input (32 B decoded, ASCII bytes)
crypto-js (parse): 352,035 ops/s ( 2.84 µs/op)
crypto-js (parse -> Uint8Array): 280,866 ops/s ( 3.56 µs/op)
parseBase64: 8,286,401 ops/s ( 121 ns/op) -> 23.54x (vs parse) / 29.50x (vs full)
Scenario: Uint8Array input (1 KB decoded, ASCII bytes)
crypto-js (parse): 100,676 ops/s ( 9.93 µs/op)
crypto-js (parse -> Uint8Array): 81,310 ops/s ( 12.30 µs/op)
parseBase64: 394,818 ops/s ( 2.53 µs/op) -> 3.92x (vs parse) / 4.86x (vs full)
==============================================================================
Summary Table
==============================================================================
┌─────────┬─────────────────────────────────────────────────┬───────────────────┬──────────────────┬─────────────────┬────────────────────┬───────────────────┐
│ (index) │ Scenario │ crypto-js (parse) │ crypto-js (full) │ parseBase64 │ Speedup (vs parse) │ Speedup (vs full) │
├─────────┼─────────────────────────────────────────────────┼───────────────────┼──────────────────┼─────────────────┼────────────────────┼───────────────────┤
│ 0 │ 'Small string (32 B decoded, ~44 chars Base64)' │ '399.5k ops/s' │ '292.6k ops/s' │ '7144.2k ops/s' │ '17.88x' │ '24.42x' │
│ 1 │ 'Medium string (1 KB decoded, ~1.37 KB Base64)' │ '114.8k ops/s' │ '87.6k ops/s' │ '264.9k ops/s' │ '2.31x' │ '3.03x' │
│ 2 │ 'Large string (64 KB decoded, ~87 KB Base64)' │ '2.1k ops/s' │ '1.7k ops/s' │ '4.7k ops/s' │ '2.24x' │ '2.69x' │
│ 3 │ 'Base64URL string (1 KB decoded)' │ '95.2k ops/s' │ '81.9k ops/s' │ '285.9k ops/s' │ '3.00x' │ '3.49x' │
│ 4 │ 'Uint8Array input (32 B decoded, ASCII bytes)' │ '352.0k ops/s' │ '280.9k ops/s' │ '8286.4k ops/s' │ '23.54x' │ '29.50x' │
│ 5 │ 'Uint8Array input (1 KB decoded, ASCII bytes)' │ '100.7k ops/s' │ '81.3k ops/s' │ '394.8k ops/s' │ '3.92x' │ '4.86x' │
└─────────┴─────────────────────────────────────────────────┴───────────────────┴──────────────────┴─────────────────┴────────────────────┴───────────────────┘