@corbet/harness-core 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@
2
2
  export declare function sha256Hex(data: Uint8Array): string;
3
3
  /** UTF-8 bytes of text. */
4
4
  export declare function utf8Bytes(text: string): Uint8Array;
5
- /** Base64 bytes of a base64 string. Throws when the input is not valid base64. */
5
+ /** Base64 bytes of a base64 string, decoded exactly as Node's Buffer does (never throws). */
6
6
  export declare function base64ToBytes(value: string): Uint8Array;
7
7
  /** Base64 string of bytes. */
8
8
  export declare function bytesToBase64(data: Uint8Array): string;
@@ -13,13 +13,49 @@ export function sha256Hex(data) {
13
13
  export function utf8Bytes(text) {
14
14
  return new TextEncoder().encode(text);
15
15
  }
16
- /** Base64 bytes of a base64 string. Throws when the input is not valid base64. */
16
+ const BTOA_CHUNK_SIZE = 0x6000;
17
+ // Decoding rules copied exactly from Node's Buffer.from(value, "base64"), so 0.1.1
18
+ // accepts precisely what 0.1.0 accepted: both the standard and URL-safe
19
+ // alphabets; any other character skipped; decoding stops at the first "=";
20
+ // a trailing partial group decoded; each UTF-16 code unit read by its low byte
21
+ // (so an emoji's high surrogate, 0xD83D, reads as "=" and stops decoding, as in
22
+ // Node). Checked against Buffer on 500,000 random strings.
23
+ const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
24
+ const BASE64_LOOKUP = new Int16Array(128).fill(-1);
25
+ for (let index = 0; index < BASE64_ALPHABET.length; index++) {
26
+ BASE64_LOOKUP[BASE64_ALPHABET.charCodeAt(index)] = index;
27
+ }
28
+ BASE64_LOOKUP[45] = 62; // "-" (URL-safe)
29
+ BASE64_LOOKUP[95] = 63; // "_" (URL-safe)
30
+ /** Base64 bytes of a base64 string, decoded exactly as Node's Buffer does (never throws). */
17
31
  export function base64ToBytes(value) {
18
- return new Uint8Array(Buffer.from(value, "base64"));
32
+ const out = new Uint8Array(Math.ceil((value.length * 3) / 4));
33
+ let length = 0;
34
+ let accumulator = 0;
35
+ let bits = 0;
36
+ for (let index = 0; index < value.length; index++) {
37
+ const code = value.charCodeAt(index) & 0xff;
38
+ if (code === 61)
39
+ break; // "="
40
+ const sextet = code < 128 ? (BASE64_LOOKUP[code] ?? -1) : -1;
41
+ if (sextet < 0)
42
+ continue;
43
+ accumulator = ((accumulator << 6) | sextet) & 0xffffff;
44
+ bits += 6;
45
+ if (bits >= 8) {
46
+ bits -= 8;
47
+ out[length++] = (accumulator >> bits) & 0xff;
48
+ }
49
+ }
50
+ return out.slice(0, length);
19
51
  }
20
52
  /** Base64 string of bytes. */
21
53
  export function bytesToBase64(data) {
22
- return Buffer.from(data).toString("base64");
54
+ let encoded = "";
55
+ for (let offset = 0; offset < data.length; offset += BTOA_CHUNK_SIZE) {
56
+ encoded += btoa(String.fromCharCode(...data.subarray(offset, offset + BTOA_CHUNK_SIZE)));
57
+ }
58
+ return encoded;
23
59
  }
24
60
  /** Reads a text file as UTF-8. Throws when the file cannot be read. */
25
61
  export function readTextFile(path) {
@@ -57,18 +93,27 @@ export function generateEd25519PrivateKeyPem() {
57
93
  /** DER SPKI bytes of the Ed25519 public key for a PKCS#8 PEM private key, as base64. */
58
94
  export function ed25519PublicKeyDerBase64(privateKeyPem) {
59
95
  const privateKey = createPrivateKey(privateKeyPem);
60
- return createPublicKey(privateKey).export({ type: "spki", format: "der" }).toString("base64");
96
+ const der = createPublicKey(privateKey).export({ type: "spki", format: "der" });
97
+ return bytesToBase64(new Uint8Array(der));
61
98
  }
62
99
  /** Signs bytes with an Ed25519 PKCS#8 PEM private key. Deterministic: no nonce. */
63
100
  export function ed25519Sign(bytes, privateKeyPem) {
64
101
  return new Uint8Array(sign(null, bytes, createPrivateKey(privateKeyPem)));
65
102
  }
103
+ /** Parses DER SPKI base64 Ed25519 public key bytes. Throws on a bad key. */
104
+ function createEd25519PublicKey(publicKeyDerBase64) {
105
+ const input = {
106
+ key: base64ToBytes(publicKeyDerBase64),
107
+ format: "der",
108
+ type: "spki",
109
+ };
110
+ return createPublicKey(input);
111
+ }
66
112
  /** Verifies an Ed25519 signature against DER SPKI base64 public key bytes. Throws on a bad key. */
67
113
  export function ed25519Verify(bytes, signature, publicKeyDerBase64) {
68
- const key = createPublicKey({ key: Buffer.from(publicKeyDerBase64, "base64"), format: "der", type: "spki" });
69
- return verify(null, bytes, key, signature);
114
+ return verify(null, bytes, createEd25519PublicKey(publicKeyDerBase64), signature);
70
115
  }
71
116
  /** Parses DER SPKI base64 Ed25519 public key bytes and discards the result. Throws on a bad key. */
72
117
  export function assertEd25519PublicKeyDerBase64(publicKeyDerBase64) {
73
- createPublicKey({ key: Buffer.from(publicKeyDerBase64, "base64"), format: "der", type: "spki" });
118
+ createEd25519PublicKey(publicKeyDerBase64);
74
119
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const HARNESS_CORE_VERSION = "0.1.0";
1
+ export declare const HARNESS_CORE_VERSION = "0.1.1";
2
2
  export * from "./eventlog.js";
3
3
  export * from "./receipt.js";
4
4
  export { createRouter } from "./router.js";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export const HARNESS_CORE_VERSION = "0.1.0";
1
+ export const HARNESS_CORE_VERSION = "0.1.1";
2
2
  export * from "./eventlog.js";
3
3
  export * from "./receipt.js";
4
4
  export { createRouter } from "./router.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@corbet/harness-core",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {