Skip to content

Instantly share code, notes, and snippets.

@carsonfarmer
Created June 14, 2018 06:45
Show Gist options
  • Select an option

  • Save carsonfarmer/2104f77b924fcf901522fe1f24474f41 to your computer and use it in GitHub Desktop.

Select an option

Save carsonfarmer/2104f77b924fcf901522fe1f24474f41 to your computer and use it in GitHub Desktop.
main.js step 3
import 'babel-polyfill' // We need this for async/await polyfills
// Import our two IPFS-based modules
import getIpfs from 'window.ipfs-fallback'
import crypto from 'libp2p-crypto'
let ipfs
// Setup a very simple async setup function to run on page load
const setup = async () => {
try {
ipfs = await getIpfs() // Init an IPFS peer node
const button = document.getElementById('button')
button.addEventListener("click", (e) => {
e.preventDefault()
const message = document.getElementById('message')
const password = document.getElementById('password')
// Compute a derived key to use in AES encryption algorithm
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) {
cipher.encrypt(Buffer.from(message.value), async (err, encrypted) => {
if (!err) {
const hashed = (await ipfs.files.add(encrypted))[0]
output.innerText = `/ipfs/${hashed.hash}`
}
})
}
})
})
} catch(err) {
console.log(err) // Just pass along the error
}
}
setup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment