Skip to content

Commit

Permalink
fix: ui test
Browse files Browse the repository at this point in the history
  • Loading branch information
ioanmo226 committed Dec 23, 2024
1 parent b01bd67 commit b0681ba
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions extension/js/common/core/crypto/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,20 +492,40 @@ export class KeyUtil {
public static validateChecksum = (armoredText: string): boolean => {
const lines = armoredText.split('\n').map(l => l.trim());

// Filter out known non-data lines
const dataCandidates = lines.filter(line => line.length > 0 && !line.startsWith('-----') && !line.startsWith('Version:') && !line.startsWith('Comment:'));

// Find checksum line
const checksumIndex = dataCandidates.findIndex(line => line.startsWith('='));
if (checksumIndex === -1) return false;

const checksumLine = dataCandidates[checksumIndex].slice(1);
const dataLines = dataCandidates.slice(0, checksumIndex);

const providedBytes = atob(checksumLine);
// Decode checksum
let providedBytes: string;
try {
providedBytes = atob(checksumLine);
} catch {
return false;
}
if (providedBytes.length !== 3) return false;

const providedCRC = (providedBytes.charCodeAt(0) << 16) | (providedBytes.charCodeAt(1) << 8) | providedBytes.charCodeAt(2);

const rawData = atob(dataLines.join(''));
// Attempt to decode all data lines (some may not be base64)
const decodedChunks: string[] = [];
for (const line of dataLines) {
try {
decodedChunks.push(atob(line));
} catch {
// Not a valid base64 line, skip it
}
}

if (decodedChunks.length === 0) return false;

const rawData = decodedChunks.join('');
const dataBytes = new Uint8Array([...rawData].map(c => c.charCodeAt(0)));

return KeyUtil.crc24(dataBytes) === providedCRC;
Expand Down

0 comments on commit b0681ba

Please sign in to comment.