Encryption and Hashing
Encryption is the process of encoding information. This process converts the original representation of the information, known as plaintext, into an alternative form known as ciphertext. Ideally, only authorized parties can decipher a ciphertext back to plaintext and access the original information. Encryption does not itself prevent interference but denies the intelligible content to a would-be interceptor. Encryption is a two-way function; what is encrypted can be decrypted with the proper key.
Hashing is the process of converting a given key into another value. A hash function is used to generate the new value according to a mathematical algorithm. Once hashing has been done, it should be impossible to go from the output to the input.
#
EncryptionNode.js provides a built-in crypto module that you can use to encrypt and decrypt strings, numbers, buffers, streams, and more. Nest itself does not provide any additional package on top of this module to avoid introducing unnecessary abstractions.
As an example, let's use AES (Advanced Encryption System) 'aes-256-ctr'
algorithm CTR encryption mode.
Now to decrypt encryptedText
value:
#
HashingFor hashing, we recommed using either the bcrypt or argon2 packages. Nest itself does not provide any additional wrappers on top of these modules to avoid introducing unnecessary abstractions (making the learning curve short).
As an example, let's use bcrypt
to hash a random pasword.
First install required packages:
Once the installation is complete, you can use the hash
function, as follows:
To generate a salt, use the genSalt
function:
To compare/check a password, use the compare
function:
You can read more about available functions here.