Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
}
})
} else {
cipher.encrypt(Buffer.from(message.value), async (err, encrypted) => {
if (!err) {
const hashed = (await ipfs.files.add(encrypted))[0]
return new Promise((resolve, reject) => {
crypto.aes.create(Buffer.from(key), Buffer.from(iv), (err, key) => {
if (err) {
return reject(err)
}
resolve(key)
})
})
}
return new Promise((resolve, reject) => {
crypto.aes.create(Buffer.from(key), Buffer.from(iv), (err, key) => {
if (err) {
return reject(err)
}
resolve(key)
})
})
}
return (callback) => crypto.aes.create(keyBytes, iv, callback)
}
'use strict'
const b58Decode = require('bs58').decode
const crypto = require('libp2p-crypto')
const pify = require('pify')
const createKey = pify(crypto.aes.create.bind(crypto.aes))
const defaultOptions = {
keyLength: 32,
ivLength: 16
}
function parseSymmetricalKey (string, _options) {
const options = Object.assign({}, defaultOptions, _options)
const rawKey = b58Decode(string)
return createKey(
rawKey.slice(0, options.keyLength),
rawKey.slice(options.keyLength, options.keyLength + options.ivLength))
}
module.exports = parseSymmetricalKey
return new Promise((resolve, reject) => {
crypto.aes.create(rawKey.slice(0, options.keyLength), rawKey.slice(options.keyLength), (err, key) => {
if (err) {
return reject(err)
}
resolve({
raw: rawKey,
key: key
})
})
})
}
return new Promise((resolve, reject) => {
crypto.aes.create(Buffer.from(key).slice(0, 32), Buffer.from(iv).slice(0, 16), (err, key) => {
if (err) {
return reject(err)
}
resolve(key)
})
})
}