|
59 | 59 | selectedTab.classList.add('active'); |
60 | 60 | } |
61 | 61 |
|
62 | | - // Convert a string to BigInt |
63 | | - function stringToBigInt(str) { |
64 | | - // Use the UTF-16 character codes of the string, join them, and turn into BigInt |
65 | | - let charCodes = Array.from(str).map(char => char.charCodeAt(0)).join(''); |
66 | | - return BigInt(charCodes); |
| 62 | + // Convert a string to BigInt using fixed-width (5 digits) encoding |
| 63 | + function stringToBigInt(str) { |
| 64 | + // Pad each char code to 5 digits to ensure unambiguous decoding |
| 65 | + let charCodes = Array.from(str).map(char => char.charCodeAt(0).toString().padStart(5, '0')).join(''); |
| 66 | + return BigInt(charCodes); |
| 67 | + } |
| 68 | + |
| 69 | + // Convert a BigInt back to a string, decoding in fixed-width (5 digits) |
| 70 | + function bigIntToString(bigInt) { |
| 71 | + let strRepresentation = bigInt.toString(); |
| 72 | + // Pad left if string length is not a multiple of 5 |
| 73 | + if (strRepresentation.length % 5 !== 0) { |
| 74 | + strRepresentation = strRepresentation.padStart(Math.ceil(strRepresentation.length / 5) * 5, '0'); |
67 | 75 | } |
68 | | - |
69 | | - // Convert a BigInt back to a string |
70 | | - function bigIntToString(bigInt) { |
71 | | - const charCodes = []; |
72 | | - let strRepresentation = bigInt.toString(); |
73 | | - |
74 | | - // Process the string representation of the BigInt as character codes in reverse |
75 | | - while (strRepresentation.length > 0) { |
76 | | - // Extract the last 2-3 digits (UTF-16 character code ranges from 0 to 65535) |
77 | | - let charCode = parseInt(strRepresentation.slice(-3), 10); |
78 | | - |
79 | | - // If the charCode is invalid, try 2 digits |
80 | | - if (charCode > 65535) { |
81 | | - charCode = parseInt(strRepresentation.slice(-2), 10); |
82 | | - strRepresentation = strRepresentation.slice(0, -2); |
83 | | - } else { |
84 | | - strRepresentation = strRepresentation.slice(0, -3); |
85 | | - } |
86 | | - |
87 | | - charCodes.push(charCode); |
88 | | - } |
89 | | - |
90 | | - // Reverse the charCodes and convert back to characters |
91 | | - return charCodes.reverse().map(code => String.fromCharCode(code)).join(''); |
| 76 | + const charCodes = []; |
| 77 | + for (let i = 0; i < strRepresentation.length; i += 5) { |
| 78 | + let code = parseInt(strRepresentation.slice(i, i+5), 10); |
| 79 | + charCodes.push(code); |
92 | 80 | } |
| 81 | + return charCodes.map(code => String.fromCharCode(code)).join(''); |
| 82 | + } |
93 | 83 |
|
94 | 84 | // Helper function to compute SHA-256 hash |
95 | 85 | async function sha256(message) { |
|
0 commit comments