Skip to content

Instantly share code, notes, and snippets.

@bbstilson
Created April 20, 2018 02:21
Show Gist options
  • Save bbstilson/fd8d81d758731f354a9d1223ef45b132 to your computer and use it in GitHub Desktop.
Save bbstilson/fd8d81d758731f354a9d1223ef45b132 to your computer and use it in GitHub Desktop.
function encrypt(text, password) {
const initVect = crypto.randomBytes(16);
const key = getCipherKey(password);
const cipher = createCipheriv('aes256', key, initVect);
const cipherText = cipher.update(text).digest();
return initVect + ciphertext;
}
encrypt('hello', 'mygr8password');
@cullylarson
Copy link

In what way is this bad?

@bbstilson
Copy link
Author

bbstilson commented Feb 19, 2021

Hey Cully. "bad" is meant more as "not ideal". This function requires all the data be in memory as opposed to being processed through a stream. I go into more details in the accompanying blog post here. Grep for "Generating an initialization vector" to jump straight to the context for this code snippet.

@cullylarson
Copy link

That's helpful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment