import { describe, test, expect } from "bun:test"; import { base43Decode } from './base43'; // Helper to convert hex strings to Uint8Array const toHex = (bytes: Uint8Array) => Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join(''); describe('Base43 Decoding (Krux Official Test Vectors)', () => { test('should decode empty string to empty Uint8Array', () => { expect(base43Decode('')).toEqual(new Uint8Array(0)); }); test('should throw error for forbidden characters', () => { expect(() => base43Decode('INVALID!')).toThrow('forbidden character ! for base 43'); expect(() => base43Decode('INVALID_')).toThrow('forbidden character _ for base 43'); }); // Test cases adapted directly from Krux's test_baseconv.py const kruxBase43TestVectors = [ { hex: "61", b43: "2B", }, { hex: "626262", b43: "1+45$", }, { hex: "636363", b43: "1+-U-", }, { hex: "73696d706c792061206c6f6e6720737472696e67", b43: "2YT--DWX-2WS5L5VEX1E:6E7C8VJ:E", }, { hex: "00eb15231dfceb60925886b67d065299925915aeb172c06647", b43: "03+1P14XU-QM.WJNJV$OBH4XOF5+E9OUY4E-2", }, { hex: "516b6fcd0f", b43: "1CDVY/HG", }, { hex: "bf4f89001e670274dd", b43: "22DOOE00VVRUHY", }, { hex: "572e4794", b43: "9.ZLRA", }, { hex: "ecac89cad93923c02321", b43: "F5JWS5AJ:FL5YV0", }, { hex: "10c8511e", b43: "1-FFWO", }, { hex: "00000000000000000000", b43: "0000000000", }, { hex: "000111d38e5fc9071ffcd20b4a763cc9ae4f252bb4e48fd66a835e252ada93ff480d6dd43dc62a641155a5", b43: "05V$PS0ZWYH7M1RH-$2L71TF23XQ*HQKJXQ96L5E9PPMWXXHT3G1IP.HT-540H", }, { hex: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", b43: "060PLMRVA3TFF18/LY/QMLZT76BH2EO*BDNG7S93KP5BBBLO2BW0YQXFWP8O$/XBSLCYPAIOZLD2O$:XX+XMI79BSZP-B7U8U*$/A3ML:P+RISP4I-NQ./-B4.DWOKMZKT4:5+M3GS/5L0GWXIW0ES5J-J$BX$FIWARF.L2S/J1V9SHLKBSUUOTZYLE7O8765J**C0U23SXMU$.-T9+0/8VMFU*+0KIF5:5W:/O:DPGOJ1DW2L-/LU4DEBBCRIFI*497XHHS0.-+P-2S98B/8MBY+NKI2UP-GVKWN2EJ4CWC3UX8K3AW:MR0RT07G7OTWJV$RG2DG41AGNIXWVYHUBHY8.+5/B35O*-Z1J3$H8DB5NMK6F2L5M/1", }, ]; kruxBase43TestVectors.forEach(({ hex, b43 }) => { test(`should decode Base43 "${b43}" to hex "${hex}"`, () => { const decodedBytes = base43Decode(b43); expect(toHex(decodedBytes)).toEqual(hex); }); }); const specialKruxTestVectors = [ { data: "1334+HGXM$F8PPOIRNHX0.R*:SBMHK$X88LX$*/Y417R/6S1ZQOB2LHM-L+4T1YQVU:B*CKGXONP7:Y/R-B*:$R8FK", expectedErrorMessage: "Krux decryption failed - wrong passphrase or corrupted data" // This error is thrown by crypto.subtle.decrypt } ]; // We cannot fully test the user's specific case here without a corresponding Python encrypt function // to get the expected decrypted bytes. However, we can at least confirm this decodes to *some* bytes. specialKruxTestVectors.forEach(({ data }) => { test(`should attempt to decode the user's special Base43 string "${data.substring(0,20)}..."`, () => { const decodedBytes = base43Decode(data); expect(decodedBytes).toBeInstanceOf(Uint8Array); expect(decodedBytes.length).toBeGreaterThan(0); // Further validation would require the exact Python output (decrypted bytes) }); }); test('should correctly decode the user-provided failing case', () => { const b43 = "1334+HGXM$F8PPOIRNHX0.R*:SBMHK$X88LX$*/Y417R/6S1ZQOB2LHM-L+4T1YQVU:B*CKGXONP7:Y/R-B*:$R8FK"; const expectedHex = "0835646363373062641401a026315e057b79d6fa85280f20493fe0d310e8638ce9738dddcd458342cbc54a744b63057ee919ad05af041bb652561adc2e"; const decodedBytes = base43Decode(b43); expect(toHex(decodedBytes)).toEqual(expectedHex); }); });