function _convertPasscodeToKey(passcode) { const digest = await crypto.subtle.digest('SHA-256', (new TextEncoder()).encode(String(passcode).padStart(4, '0'))) const keyMaterial = await crypto.subtle.importKey('raw', digest, 'PBKDF2', false, ['deriveKey']) return await crypto.subtle.deriveKey( { name: 'PBKDF2', salt, iterations: 1, hash: 'SHA-256' }, keyMaterial, { name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt'] ) } async function _decrypt(key, data) { const buffer = new Uint8Array(atob(data).split('').map((c) => c.charCodeAt(0))) const iv = buffer.slice(0, 12) const encrypted = buffer.slice(12) const decrypted = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, encrypted) return (new TextDecoder()).decode(decrypted) } async function unlock(passcode) { const key = await _convertPasscodeToKey(passcode) return await _decrypt(key, encrypted) }