SPB Git

Repository Public

generate-parity.ts 19.4 KB 212 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : SP Vault8 * File             : apple/SharedVaultKit/Tests/Tools/generate-parity.ts9 * Module           : Tools10 * Repository       : https://git.spboucher.ai/sp-vault11 * Project Website  : https://vault.spboucher.ai12 * Created          : 2026-11-1913 * Last Modified    : 2026-11-1914 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243// Regenerates Tests/SharedVaultKitTests/Fixtures/parity.json from the TypeScript reference implementation.44// Run from the repository root:  node --import tsx apple/SharedVaultKit/Tests/Tools/generate-parity.ts45// The Swift test suite (ParityTests) replays every case and must produce identical results.46import { writeFileSync, readFileSync } from "node:fs";47import * as P from "../../../../packages/protocol/src/index.ts";4849const tryOr = <T>(f: () => T): T | { error: true } => { try { return f(); } catch { return { error: true }; } };5051// deterministic byte stream shared with the Swift tests (xorshift32)52function makeRnd(seed: number) {53  let s = seed >>> 0;54  return (len: number) => {55    const out = new Uint8Array(len);56    for (let i = 0; i < len; i++) { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; out[i] = s & 0xff; }57    return out;58  };59}6061const originInputs = [62  "https://example.com", "example.com", "  Example.COM  ", "http://example.com:8080/path?q#f", "https://example.com:443", "http://example.com:80",63  "https://www.example.co.uk/login", "https://a.b.example.co.uk", "https://co.uk", "https://github.io", "https://alice.github.io", "https://x.alice.github.io",64  "https://www.ck", "https://foo.ck", "https://a.foo.ck", "https://b.a.foo.ck", "https://ck", "https://127.0.0.1:3000", "http://192.168.1.10/admin", "https://[::1]:8443/",65  "https://[2001:db8::1]", "https://[2001:DB8:0:0:0:0:0:1]/x", "https://[::ffff:1.2.3.4]", "localhost", "http://localhost:3000", "https://intranet", "https://foo.localhost",66  "https://bank.com@evil.example", "https://user:pw@example.com", "https://user@example.com", "https://@example.com", "https://:@example.com",67  "https://bücher.example", "https://xn--bcher-kva.example", "https://пример.рф", "https://xn--e1afmkfd.xn--p1ai", "https://аpple.com", "https://xn--pple-43d.com",68  "https://食狮.com.cn", "https://www.食狮.公司.cn", "https://faß.de", "https://EXAMPLE.com", "https://example.com.", "https://example.com..", "https://.example.com",69  "ftp://example.com", "file:///etc/passwd", "javascript:alert(1)", "mailto:a@b.c", "", "   ", "not a url", "https://", "https:///example.com", "https:example.com",70  "https:/example.com", "HTTPS://EXAMPLE.COM/Path", "http:\\\\example.com\\path", "https://exa mple.com", "https://example.com:99999", "https://example.com:abc",71  "https://example.com:0443", "https://0x7f.1", "https://2130706433", "https://1.2.3", "https://1.2.3.4.5", "https://999.1.1.1", "https://01.02.03.04", "https://1.2.3.4.",72  "https://a_b.example.com", "https://-a.example.com", "https://a-.example.com", "https://a,b.com", "https://a,b", "https://ex%61mple.com", "https://example.com%2f.evil.com",73  "https://a.b.c.d.e.kawasaki.jp", "https://city.kawasaki.jp", "https://foo.kawasaki.jp", "https://s3.amazonaws.com", "https://bucket.s3.amazonaws.com",74  "https://foo.blogspot.com", "https://blogspot.com", "https://foo.compute.amazonaws.com", "https://a.b.compute.amazonaws.com", "https://example.unknowntld",75  "https://a.example.unknowntld", "example.com/path", "example.com:8443", "user@example.com", "//example.com", "https://EXAMPLE.com:8443", "https://example.com\t/x",76  "https://ex\nample.com", "https://xn--ls8h.la", "https://xn--a.com", "https://paypa1.com", "https://rnicrosoft.com", "https://www.pm.me", "https://日本語.jp",77  "https://münchen.de", "https://ελλάδα.gr", "https://gооgle.com", "wss://example.com", "https://example.com:65535", "https://example.com:65536", "https://example.com:",78  "https://[::1", "https://[zz::1]", "https://1.1", "https://0", "https://example.com#@evil.com", "https://example.com?@evil.com", "https://example.com/@evil.com",79  "https://example.com\\@evil.com", "https://evil.com\\.example.com", "https://a..b.com", "https://x.y.z.co.jp", "https://www.gov.uk", "https://service.gov.uk", "https://a.service.gov.uk",80];8182const matchCases: { saved: P.ItemUrl; page: string }[] = [];83const savedUrls = ["https://example.com", "http://example.com", "example.com", "https://login.example.com/account", "https://example.com:8443", "https://alice.github.io",84  "https://github.io", "http://localhost:3000", "https://localhost", "https://127.0.0.1", "http://192.168.1.10", "https://example.com/a/b/", "https://bücher.example", "https://example.co.uk/x/../y", "http://[::1]:3000"];85const pages = ["https://example.com", "http://example.com", "https://www.example.com/login", "https://login.example.com/account/", "https://login.example.com/account?x=1",86  "https://example.com:8443/", "https://bob.github.io", "https://alice.github.io/x", "http://localhost:3000/x", "http://localhost", "http://127.0.0.1", "http://192.168.1.10/",87  "https://192.168.1.10/", "https://example.com/a/b", "https://example.com/a/./b/", "https://xn--bcher-kva.example/", "https://evil.com", "example.com", "https://example.com@evil.com",88  "https://example.co.uk/y", "https://example.co.uk/Y", "http://example.co.uk/y", "ftp://example.com", "https://EXAMPLE.com/a/%62", "http://[::1]:3000/x", "http://[::1]/"];89for (const url of savedUrls) for (const match of ["domain", "host", "exact", "never"] as const) for (const page of pages) matchCases.push({ saved: { url, match }, page });9091const skeletonInputs = ["example.com", "paypa1.com", "rnicrosoft.com", "vvikipedia.org", "xn--pple-43d.com", "bücher.example", "xn--bcher-kva.example", "g00gle.com", "EXAMPLE.com", "xn--80ak6aa92e.com", "xn--a.com", "xn---.com", "xn--0.com", "xn--9.com", "xn--aa.com", "xn--a:b.com", "xn--a@.com", "xn--a[.com", "xn--a`.com", "xn--a{.com", "xn--a/.com", "xn--b.com", "xn--c.com", "xn--ab-.com"];92const lookalikeCases = [93  { page: "https://paypa1.com", saved: ["paypal.com"] }, { page: "https://paypal.com", saved: ["paypal.com"] }, { page: "https://paypal.co.uk", saved: ["paypal.com"] },94  { page: "https://paypol.com", saved: ["paypal.com"] }, { page: "https://paypall.com", saved: ["paypal.com"] }, { page: "https://xn--pple-43d.com", saved: ["apple.com"] },95  { page: "https://rnicrosoft.com", saved: ["microsoft.com"] }, { page: "https://gооgle.com", saved: ["github.com"] }, { page: "https://abc.com", saved: ["abd.com"] },96  { page: "https://example.org", saved: [] }, { page: "https://github.io", saved: ["github.com"] }, { page: "https://githab.com", saved: ["gitlab.com", "github.com"] },97  { page: "https://аpple.com", saved: ["example.com"] },98];99100const passwords = ["", "password", "Password123", "password123", "abc", "aaaaaaaa", "abcdefgh", "12345678", "Tr0ub4dor&3", "correct horse battery staple", "x9$Lq!7zP@4w#Ks2", "été", "🔐🔑🔐🔑", "qwerty99", "ZZZZ", "a1!", "A", "aB3$aB3$aB3$"];101102const rnd1 = makeRnd(1), rnd2 = makeRnd(2), rnd3 = makeRnd(3), rnd4 = makeRnd(4), rnd5 = makeRnd(5), rnd6 = makeRnd(6);103const wordlist = readFileSync(new URL("../../../../packages/protocol/data/eff-large-wordlist.txt", import.meta.url), "utf8").split("\n").map((x) => x.trim()).filter(Boolean);104const pwOptions: P.PasswordOptions[] = [105  { length: 24, lower: true, upper: true, digits: true, symbols: true, avoidAmbiguous: false },106  { length: 4, lower: true, upper: true, digits: true, symbols: true, avoidAmbiguous: true },107  { length: 64, lower: true, upper: false, digits: true, symbols: false, avoidAmbiguous: true },108  { length: 256, lower: false, upper: false, digits: false, symbols: true },109];110const ppOptions: P.PassphraseOptions[] = [111  { words: 6, separator: "-", capitalize: false, number: false }, { words: 3, separator: " ", capitalize: true, number: true }, { words: 20, separator: "", capitalize: true, number: false },112];113114const normalizeInputs: unknown[] = [115  {}, [], { type: "login", title: "T", fields: { username: "u", password: "p", n: 5, ["k".repeat(65)]: "x" }, urls: [{ url: "https://a.com", match: "host" }, { url: 5 }, null, { url: "b.com", match: "weird" }],116    custom: [{ label: "l", value: "v", kind: "secret" }, { label: "l2", value: "v2", kind: "nope" }, { label: 1, value: "v" }], notes: "n", tags: [" a ", "a", "b", 3, "", "  "],117    favorite: true, collection: "c", attachments: [{ id: "i", key: "k", prefix: "p", size: 3, chunks: 1, sha256: "s", name: "n", mime: "m" }, { id: "x" }],118    passwordHistory: [{ value: "old", at: "2026-01-01T00:00:00.000Z" }, { value: 1, at: "x" }], expiresAt: "2027-01-01", rotateEveryDays: 90, createdAt: "2026-01-01T00:00:00.000Z",119    updatedAt: "2026-02-01T00:00:00.000Z", conflictOf: "r", trashedAt: "2026-03-01T00:00:00.000Z", extra: { unknown: true } },120  { type: "unknown-type", title: 5, favorite: "yes", rotateEveryDays: -1, tags: "x", urls: "y" }, { type: "ssh-key", rotateEveryDays: 1.5, title: "x".repeat(600) },121  { type: "api-token", title: "😀".repeat(300), tags: Array.from({ length: 60 }, (_, i) => "t" + i), passwordHistory: Array.from({ length: 25 }, (_, i) => ({ value: "v" + i, at: "a" + i })) },122  { type: "wifi", fields: { ssid: "home", password: "pw" }, createdAt: "", updatedAt: 7 },123];124125const csvTexts = [126  "a,b,c\r\n1,2,3\r\n", "title,url\n\"x, y\",\"he said \"\"hi\"\"\"\n\n,\n", "a\rb\rc", "\"multi\nline\",x\n\"\"\n\"\",\"\"\nlast", "a,\"b\"c,d\n", "x,y\n1", "",127  "é,ü\n😀,\"😀\"\n",128];129const importTexts = [130  "Title,URL,Username,Password,Notes,OTPAuth\nExample,https://example.com,alice, pw with space ,hello,otpauth://totp/x?secret=JBSWY3DPEHPK3PXP\n,https://www.site.org/login,bob,pw2,,\n,,carol,pw3,,\n,,,,,\n",131  "name,url,username,password\nShop,https://shop.example,me,secret\n,ftp://files.example.com/x,u,p\n,\"https://a.com,https://b.com\",u,p\n",132  "folder,favorite,type,name,notes,fields,reprompt,login_uri,login_username,login_password,login_totp\nWork/Sub,1,login,Site,note,,0,\"https://a.example,https://b.example\",u,p,JBSWY3DPEHPK3PXP\n,,note,My note,body,,,,,,\n",133  "Title,URL,Username,Password,OTPAuth,Favorite,Archived,Tags,Notes\nOne,https://one.example,u,p,,true,false,a;b,n\n",134  "site,login,pass\nexample.net,u,p\n", "account,email,password,comments\nAcct,e@x.org,p,c\n",135];136const bitwardenJson = JSON.stringify({ encrypted: false, folders: [{ id: "f1", name: "Folder" }], items: [137  { type: 1, name: "Login", notes: "n", favorite: true, folderId: "f1", login: { username: "u", password: "p", totp: "JBSWY3DPEHPK3PXP", uris: [{ uri: "https://a.com", match: null }, { uri: "https://b.com/x", match: 3 }, { uri: "c.com", match: 1 }, { uri: "d.com", match: 5 }, { match: 0 }] },138    fields: [{ name: "pin", value: "1234", type: 1 }, { name: "txt", value: "v", type: 0 }, { name: "", value: "x" }, { name: "nul", value: null }] },139  { type: 2, name: "Note", notes: "secret note" },140  { type: 3, name: "Card", card: { cardholderName: "A B", number: "4111111111111111", code: "123", expMonth: "4", expYear: "2031" } },141  { type: 3, name: "Card2", card: { cardholderName: "C", number: "5", code: "9" } },142  { type: 4, name: "Id", identity: { firstName: "A", middleName: null, lastName: "B", passportNumber: null, licenseNumber: "L1", ssn: "S", country: "CA" } },143  { type: 9, name: "Unknown" }, { type: 1, name: "", login: { username: "onlyuser" } },144] });145const stripTimes = (r: P.ImportResult) => ({ ...r, items: r.items.map((i) => ({ ...i, createdAt: "", updatedAt: "" })) });146147const totpInputs = ["JBSWY3DPEHPK3PXP", "jbsw y3dp-ehpk 3pxp==", "otpauth://totp/ACME:alice@example.com?secret=JBSWY3DPEHPK3PXP&issuer=ACME%20Co&digits=8&period=60&algorithm=SHA256",148  "otpauth://totp/alice?secret=JBSWY3DPEHPK3PXP", "OTPAUTH://TOTP/Issuer%3Abob?secret=JBSWY3DPEHPK3PXP&algorithm=sha-512", "otpauth://hotp/x?secret=JBSWY3DPEHPK3PXP",149  "otpauth://totp/x?secret=JBSWY3DPEHPK3PXP&digits=9", "otpauth://totp/x?secret=JBSWY3DPEHPK3PXP&period=4", "otpauth://totp/x?secret=JBSWY3DPEHPK3PXP&algorithm=MD5",150  "otpauth://totp/x?secret=!!!", "otpauth://totp/?secret=JBSWY3DPEHPK3PXP", "otpauth://totp/A:b:c?secret=JBSWY3DPEHPK3PXP&issuer=I+J", "not base32 !", "",151  "otpauth://totp/x%3Ay?secret=JBSW+Y3DP&secret=AAAA", "otpauth://totp/Big%20Co:%20me?secret=JBSWY3DPEHPK3PXP&period=30.0"];152153const rk = Uint8Array.from({ length: 32 }, (_, i) => (i * 11 + 3) & 0xff);154const rkText = await P.encodeRecoveryKey(rk);155const rkVariants = [rkText, rkText.toLowerCase(), rkText.replace(/-/g, " "), rkText.replace(/-/g, ""), rkText.slice(5), "  " + rkText + "\n", rkText.replace(/0/g, "O").replace(/1/g, "l"),156  rkText.replace(/1/g, "I"), rkText.slice(0, -1), rkText + "A", rkText.slice(0, -1) + (rkText.endsWith("A") ? "B" : "A"), rkText.replace("SPV1-", "SPVI-"), "SPV1-" + "U".repeat(56), "",157  rkText.replace("SPV1-", "spvl-"), rkText.replace("SPV1-", "SPVL").toLowerCase()];158// a key whose data itself starts with "SPV1" once encoded: typed without the prefix it must not be truncated159const rkSpv = Uint8Array.from({ length: 32 }, (_, i) => [0xcd, 0xb6, 0x10][i] ?? (i * 5 + 1) & 0xff);160const rkSpvText = await P.encodeRecoveryKey(rkSpv);161rkVariants.push(rkSpvText, rkSpvText.slice(5), rkSpvText.slice(5).replace(/-/g, "").toLowerCase(), "SPV1" + rkSpvText.slice(5, 60));162const recovery = [];163for (const t of rkVariants) { let out: string | null = null; try { out = P.b64u(await P.decodeRecoveryKey(t)); } catch { out = null; } recovery.push({ text: t, rk: out }); }164165const now = Date.parse("2026-11-19T00:00:00.000Z");166const mk = (type: P.ItemType, f: Partial<P.Item>): P.Item => ({ ...P.newItem(type, "t"), createdAt: "2026-01-01T00:00:00.000Z", updatedAt: "2026-01-01T00:00:00.000Z", ...f });167const analyzeItems = [168  { id: "a", item: mk("login", { fields: { username: "Alice", password: "password" }, urls: [{ url: "https://example.com/login", match: "domain" }] }) },169  { id: "b", item: mk("login", { fields: { username: "alice", password: "password" }, urls: [{ url: "http://www.example.com", match: "domain" }] }) },170  { id: "c", item: mk("login", { fields: { username: "alice", password: "x9$Lq!7zP@4w#Ks2x9$Lq!7zP@4w#Ks2" }, urls: [{ url: "http://localhost:8080", match: "host" }], expiresAt: "2026-12-01T00:00:00.000Z" }) },171  { id: "d", item: mk("wifi", { fields: { password: "short" }, expiresAt: "2026-01-01" }) },172  { id: "e", item: mk("api-token", { fields: { token: "tok" }, rotateEveryDays: 30 }) },173  { id: "f", item: mk("api-token", { fields: { token: "tok" }, rotateEveryDays: 30, passwordHistory: [{ value: "x", at: "2026-11-01T00:00:00.000Z" }] }) },174  { id: "g", item: mk("login", { fields: { username: "alice", password: "password" }, trashedAt: "2026-02-01T00:00:00.000Z" }) },175  { id: "h", item: mk("database", { fields: { password: "x9$Lq!7zP@4w#Ks2x9$Lq!7zP@4w#Ks2" }, expiresAt: "garbage" }) },176  { id: "i", item: mk("login", { fields: { username: "bob", password: "x9$Lq!7zP@4w#Ks2x9$Lq!7zP@4w#Ks2" }, urls: [{ url: "example.com", match: "domain" }], expiresAt: "2027-06-01T00:00:00Z" }) },177];178179const searchItems = [mk("login", { title: "Crème Brûlée", fields: { username: "ÉLODIE", password: "secret" }, urls: [{ url: "https://Example.com", match: "domain" }], tags: ["Work"], custom: [{ label: "PIN", value: "1234", kind: "secret" }, { label: "Hint", value: "first pet", kind: "text" }], notes: "Ångström" })];180181const jsonStrings = ["plain", "quote\" backslash\\ slash/", "ctl\u0000\u0001\b\f\n\r\t\u001f\u007f", "é ü 😀 
 
", ""];182const exportItems = [mk("login", { title: "A \"quoted\", title", fields: { username: "u", password: "p,\"q\"", totp: "JBSWY3DPEHPK3PXP" }, urls: [{ url: "https://a.com", match: "domain" }], notes: "line1\nline2" }), mk("note", { notes: "skip" }), mk("login", {})];183184const out = {185  generatedBy: "typescript",186  origins: originInputs.map((input) => ({ input, out: tryOr(() => P.parseOrigin(input)) })),187  match: matchCases.map((c) => ({ ...c, out: tryOr(() => P.matchUrl(c.saved, c.page)) })),188  skeleton: skeletonInputs.map((input) => ({ input, out: tryOr(() => P.skeleton(input)) })),189  lookalike: lookalikeCases.map((c) => ({ ...c, out: tryOr(() => P.lookalike(c.page, c.saved)) })),190  estimateBits: passwords.map((pw) => ({ pw, bits: P.estimateBits(pw) })),191  uniform: { seed: 1, n: [6, 7776, 1, 10, 88, 0x1_0000_0000, 3_000_000_000], out: [6, 7776, 1, 10, 88, 0x1_0000_0000, 3_000_000_000].map((n) => Array.from({ length: 20 }, () => P.uniform(n, rnd1))) },192  passwords: pwOptions.map((o, i) => ({ seed: 10 + i, options: { avoidAmbiguous: false, ...o }, out: P.generatePassword(o, makeRnd(10 + i)), bits: P.passwordEntropyBits(o) })),193  passphrases: ppOptions.map((o, i) => ({ seed: 20 + i, options: o, out: P.generatePassphrase(wordlist, o, makeRnd(20 + i)), bits: P.passphraseEntropyBits(o) })),194  normalize: normalizeInputs.map((input) => ({ input, out: P.normalizeItem(input) })),195  csv: csvTexts.map((text) => ({ text, rows: P.parseCsv(text) })),196  csvError: tryOr(() => P.parseCsv("a,\"unterminated")),197  imports: importTexts.map((text) => ({ text, out: tryOr(() => stripTimes(P.importCsv(text))) })),198  bitwarden: { text: bitwardenJson, out: stripTimes(P.importBitwardenJson(bitwardenJson)) },199  totpParse: totpInputs.map((input) => ({ input, out: tryOr(() => { const p = P.parseTotp(input); return { ...p, secret: P.b64u(p.secret), issuer: p.issuer ?? null, account: p.account ?? null }; }) })),200  recovery,201  analyze: { now, items: analyzeItems, out: P.analyze(analyzeItems, now) },202  search: searchItems.map((item) => ({ item, text: P.searchText(item), hits: ["creme", "ELODIE brulee", "angstrom work", "1234", "first PET", "missing"].map((q) => ({ q, hit: P.matchesQuery(P.searchText(item), q) })) })),203  jsonStrings: jsonStrings.map((s) => ({ s, json: JSON.stringify({ k: s, n: [1, -2, 0, 1.5, true, null] }) })),204  exportCsv: { items: exportItems, out: P.exportCsv(exportItems) },205  breach: await Promise.all(["password", "été", ""].map(async (pw) => ({ pw, ...(await P.breachPrefix(pw)) }))),206  pad: [0, 1, 255, 256, 257, 1000].map((n) => ({ n, len: P.pad("x".repeat(n)).length })),207  primarySecret: (Object.keys(P.TEMPLATES) as P.ItemType[]).map((type) => ({ type, template: P.TEMPLATES[type] })),208};209void rnd2; void rnd3; void rnd4; void rnd5; void rnd6;210writeFileSync(new URL("../SharedVaultKitTests/Fixtures/parity.json", import.meta.url), JSON.stringify(out, null, 1) + "\n");211console.log("parity.json written:", Object.entries(out).map(([k, v]) => `${k}=${Array.isArray(v) ? v.length : 1}`).join(" "));212
Files main
Menu

Theme