Slide 24
Slide 24 text
const encrypted = 's9Vo5kz2HD5y/H/I6ioBKweghEcM2zyoI9Hf74b4jxwALSgj9hk5V6dLe2LJZ/dVRXlO+FW6Kmv895UAhzN4WcDM40gYPN+PaT9hEBTwNyV2fxbe'
const salt = new TextEncoder().encode(atob('MTY2LDYxLDIxOCwyMTMsMjQ5LDEwNiwyNDEsMjQzLDE3Myw2LDAsMTY0LDYxLDEyMiwyMzYsNzg='))
async 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)
}