Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Enforce NIST SP 800-132
if (!opts.passPhrase || opts.passPhrase.length < 20) {
throw new Error('passPhrase must be least 20 characters')
}
if (opts.dek.keyLength < NIST.minKeyLength) {
throw new Error(`dek.keyLength must be least ${NIST.minKeyLength} bytes`)
}
if (opts.dek.salt.length < NIST.minSaltLength) {
throw new Error(`dek.saltLength must be least ${NIST.minSaltLength} bytes`)
}
if (opts.dek.iterationCount < NIST.minIterationCount) {
throw new Error(`dek.iterationCount must be least ${NIST.minIterationCount}`)
}
// Create the derived encrypting key
const dek = crypto.pbkdf2(
opts.passPhrase,
opts.dek.salt,
opts.dek.iterationCount,
opts.dek.keyLength,
opts.dek.hash)
Object.defineProperty(this, '_', { value: () => dek })
}
button.addEventListener('click', (e) => {
output.innerHTML = ''
if (!password.validity.valid) {
return
}
e.preventDefault()
// Compute a derived key to use in AES encryption algorithm
// We aren't ever storing passwords, so no need to worry about salt
const key = crypto.pbkdf2(password.value, 'encryptoid', 5000, 24, 'sha2-256')
// We're only using the key once, so a fixed IV should be ok
const iv = Buffer.from([...Array(16).keys()])
// Create AES encryption object
crypto.aes.create(Buffer.from(key), iv, (err, cipher) => {
if (!err) {
if (isDecrypting) {
cipher.decrypt(Buffer.from(message.value, 'base64'), async (err, plaintext) => {
if (!err) {
const info = `Your super secret message is:
`
const msg = `${plaintext.toString('utf-8')}`
const create = `<br><a href="${base}">create your own...</a>`
output.innerText = info + '"' + msg + '"'
output.innerHTML = output.innerHTML + create
}
})