jukebox
All repositories: gitoria
8.9 KB
\* hl:crypto — passwords first. Native realm wrapper (see server.js for the JS twin).The whole surface is eight calls for passwords and hashing, and then the Web Pushprimitives (ticket #173, RFC 8030/8291/8292) further down: ES256, P-256 ECDH,HKDF-SHA256, AES-128-GCM and base64url, all on Bytes (see the README).The first eight: six of them exist to serve the first two.What this plugin is FOR is that an application never stores a password: itstores the answer to "could this password have produced that", and the answercarries its own algorithm and cost so it stays readable when both change.hash(password, options) the stored value — a PHC stringverify(password, stored) true / false, in constant timeparsePhc(stored) what a stored value says about itselfkdf() which algorithm THIS host hashes withsha256(data) content hashing (fast on purpose — not for passwords)randomBytes(n, encoding) n bytes from the kernel CSPRNGtoBase64(data) a String's or a Bytes' bytes as base64 textfromBase64(text) base64 text back to a Bytes (null if it is not base64)The realm is SERVER (plugin.json). A password never crosses to the client, soimporting this file is also a declaration about where the importing code runs. *\\* Hash a password for storage. Returns a self-describing PHC string:$argon2id$v=19$m=32768,t=2,p=1$<salt>$<hash>$scrypt$ln=15,r=8,p=1$<salt>$<hash>Every call salts freshly, so hashing the same password twice gives twodifferent strings and both verify.`options` is optional: { cost = 15; kdf = "argon2id" }cost base-2 log of the working memory in KiB — 15 is 32 MiB, the default.CAPPED to 10..17 (1 MiB .. 128 MiB); the string records what wasactually used, so asking for 999 and reading the result back is howyou see the cap rather than being told about it.kdf force an algorithm instead of taking the host's best one. Normallyunnecessary: `hash` picks argon2id when the system libcrypto has it(OpenSSL >= 3.2) and scrypt otherwise, and `verify` reads both. *\hash(password, options) {return __native("crypto.hash", password, options)}\* Check a password against a stored PHC string. The comparison is constant-timeand the answer is a plain boolean: a wrong password, a truncated string, aflipped character and a string that is not PHC at all are all `false`. Astored string whose ALGORITHM this host cannot compute is a loud errorinstead, because answering `false` to that would read as "wrong password". *\verify(password, stored) {return __native("crypto.verify", password, stored)}\* Read a stored string without the password:{ kdf = "argon2id"; version = 19; params = { m; t; p }; saltLen; hashLen }{ kdf = "scrypt"; version = null; params = { ln; r; p }; saltLen; hashLen }`null` when the string is not a PHC string this plugin understands — which isalso the cheapest way to spot a store that was never migrated. *\parsePhc(stored) {return __native("crypto.parse", stored)}\* Which algorithm `hash()` writes with on this host. Reporting only — nothingneeds to branch on it, because every stored string names its own. *\kdf() {return __native("crypto.kdf")}\* SHA-256 of a string, as 64 lowercase hex characters. Content hashing: fast bydesign, and therefore exactly the wrong tool for a password. *\sha256(data) {return __native("crypto.sha256", data)}\* n random bytes from the kernel CSPRNG, rendered as "hex" (the default) or"base64". n is 1..1024. Suitable for session ids, one-time tokens and nonces. *\randomBytes(n, encoding) {return __native("crypto.random_bytes", n, encoding)}\* Base64 (the standard alphabet, padded) of a String's bytes — its UTF-8 — orof a Bytes, byte for byte:toBase64('user:pa55') \\ "dXNlcjpwYTU1"toBase64(req.bytes) \\ any bytes at all, NUL and 0xff included *\toBase64(data \\ a String or a Bytes) {if (hlTypeName(data) == 'Bytes') {return __native("crypto.base64_encode_hex", data.hex())}return __native("crypto.base64_encode", data)}\* Base64 text back to its bytes, as a Bytes; `.toString()` of it is the textwhen the bytes are UTF-8. Padded and unpadded text both decode; anythingthat is not base64 in the standard alphabet is null, not an error — amalformed `Authorization: Basic` header is an answer, not a crash:let b = fromBase64(req.headers.authorization.slice(6))if (b != null) { let pair = b.toString() } \\ "user:pa55" *\fromBase64(String text) {let raw = __native("crypto.base64_decode", text)if (raw == null) {return null}return toBytes(raw)}\* ── Web Push primitives (ticket #173) ──────────────────────────────────────std.crypto only, no OpenSSL; node:crypto twins in server.js. Everything takes andreturns Bytes; a function that can be handed bad input answers null instead ofthrowing, so "this subscription's key is garbage" is an answer, not a crash. *\\* base64url (RFC 4648 §5) WITHOUT padding: the form VAPID and Web Push use.base64urlEncode(toBytes("hi")) \\ "aGk" (a String encodes as its UTF-8) *\base64urlEncode(data \\ a String or a Bytes) {if (hlTypeName(data) == 'Bytes') {return __native("crypto.base64url_encode_hex", data.hex())}return __native("crypto.base64url_encode", data)}\* base64url text back to a Bytes; null when it is not base64url (the + and / of thestandard alphabet are refused). Padded and unpadded text both decode. *\base64urlDecode(String text) {let raw = __native("crypto.base64url_decode", text)if (raw == null) {return null}return toBytes(raw)}\* A fresh P-256 key pair from the kernel CSPRNG, for ES256 (VAPID) AND for ECDH (theWeb Push message key): { privateKey = Bytes 32; publicKey = Bytes 65 } where thepublic key is the uncompressed point 0x04 || X || Y. *\p256Generate() {let both = toBytes(__native("crypto.p256_generate"))return { privateKey = both.slice(0, 32); publicKey = both.slice(32, 97) }}\* The uncompressed 65-byte public point of a 32-byte private scalar; null when thescalar is not a valid key (zero, or not below the group order). *\p256PublicKey(Bytes privateKey) {let raw = __native("crypto.p256_public", privateKey.hex())if (raw == null) {return null}return toBytes(raw)}\* ES256 (RFC 7518 §3.4): ECDSA P-256 over SHA-256 of `message`, signature as the JWSwants it — raw r || s, 64 bytes, NOT DER. Hedged with fresh noise, so twosignatures of one message differ and both verify. *\es256Sign(Bytes privateKey, Bytes message) {return toBytes(__native("crypto.es256_sign", privateKey.hex(), message.hex()))}\* true when `signature` (64 bytes r || s) is `publicKey`'s (65 bytes) ES256 signatureof `message`; false for every other case, malformed lengths and off-curve keysincluded. *\es256Verify(Bytes publicKey, Bytes message, Bytes signature) {return __native("crypto.es256_verify", publicKey.hex(), message.hex(), signature.hex())}\* ECDH on P-256: the shared secret, 32 bytes (the X coordinate of the product — whatRFC 8291 §3.4 calls ecdh_secret). `peerPublicKey` is a 65-byte uncompressed point;null when it is not a point on the curve. *\ecdhSharedSecret(Bytes privateKey, Bytes peerPublicKey) {let raw = __native("crypto.ecdh_shared", privateKey.hex(), peerPublicKey.hex())if (raw == null) {return null}return toBytes(raw)}\* HKDF-SHA256 extract (RFC 5869 §2.2): PRK (32 bytes) = HMAC(salt, ikm). *\hkdfExtract(Bytes salt, Bytes ikm) {return toBytes(__native("crypto.hkdf_extract", salt.hex(), ikm.hex()))}\* HKDF-SHA256 expand (RFC 5869 §2.3): `length` (1..8160) bytes from a 32-byte PRK. *\hkdfExpand(Bytes prk, Bytes info, Number length) {return toBytes(__native("crypto.hkdf_expand", prk.hex(), info.hex(), length))}\* extract then expand in one call. *\hkdf(Bytes salt, Bytes ikm, Bytes info, Number length) {return hkdfExpand(hkdfExtract(salt, ikm), info, length)}\* AES-128-GCM seal: key 16 bytes, nonce 12 bytes; answers ciphertext || 16-byte tag(plaintext length + 16). `aad` is optional (null = none; Web Push uses none). *\aes128gcmEncrypt(Bytes key, Bytes nonce, Bytes plaintext, aad) {let ad = aadif (ad == null) {ad = toBytes(0)}return toBytes(__native("crypto.aes128gcm_encrypt", key.hex(), nonce.hex(), plaintext.hex(), ad.hex()))}\* AES-128-GCM open: the plaintext, or null when the tag does not verify (tampered,wrong key, wrong nonce, wrong aad) or the input is shorter than a tag. *\aes128gcmDecrypt(Bytes key, Bytes nonce, Bytes ciphertext, aad) {let ad = aadif (ad == null) {ad = toBytes(0)}let raw = __native("crypto.aes128gcm_decrypt", key.hex(), nonce.hex(), ciphertext.hex(), ad.hex())if (raw == null) {return null}return toBytes(raw)}
Branches
Latest commits
- 14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre
- 0433e9a8jukebox: records: gitoria remote added and pushedmre
- 1722d707jukebox: records: repo creation handed to Gitoriamre
- b42e7dfbjukebox: conductor records (CONDUCTOR.md, STATUS.md), old STATUS archivedmre
- 1b0d0bc1jukebox: a startup error prints its message before exit 1 (hybriel#247)mre
- b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre
- 73867a2ejukebox#2: Style blocks in the language's nested form (rules named after the View's own elements, pseudo-classes nested, merged duplicates); the same CSS rules, the pages look the samemre
- 06c548d6jukebox JB15: report, log linemre
- 980859cbjukebox JB15 (jukebox#15): the now-playing overlay on a short screen (tablet or phone on its side) gave the queue 12 to 100 px because the big cover took the room; the cover now shrinks with the screen height and goes away under 41rem, the queue keeps 3+ rows; gate tests/jb15.mjs at 8 sizesmre
- 9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre
- 7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre
- 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
- cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre
- 88d3d5c1jukebox JB12b (jukebox#12): the scroll handle is big (2.75rem x 4rem, accent colour, grip) and drawn on every device, no media query; native bar hidden; gate tests/touch.mjs checks the drawn width with and without touchmre
- b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
- 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
- 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
- 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
- c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
- 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre