This question already has an answer here:
I am attempting to write a function to encrypt and decrypt data using NodeJs following current best practices.
In order to achieve this I am doing the following:
- Taking an end user password and enforcing password strength requirements
- Generating 128 random bytes for a salt
- Hashing the user password with pbkdf2 and the salt
- Generating 16 random bytes for an IV
- Encrypting my data with AES-256-CBC using the IV, and hashed password
- Storing the encrypted data as a string in the format
I'm assuming that using something like Argon2 for the hashed password would be superior, although everything I have read so far about encrypting files recommends pbkdf2.
Is it safe to store the salt and IV in the encrypted file? I'd like to only require a password for decrypting the file, so this would be preferable. I am under the impression that this protects me against a rainbow table style attack. I am not certain if this significantly increases the likelihood of someone being able to decrypt the data.
Is there anything else I could be doing to improve the security of the encrypted file?
This gist contains my exact code: https://gist.github.com/briancw/485e2645f7da16eaa3b530dd913297a5