Node js crypto password example

node js crypto password

A private weblog by using Prashant Acharya.

node js crypto password

29th January, 2020 examine

Storing a password is a challenge whilst designing packages. We ought to do it nicely if we do not need to compromise the safety of our packages and customers. I would like to briefly explain the choices techniques we might don’t forget even as designing an authentication device that shops users’ passwords but first, it’s also surely crucial to speak approximately the methods we should in no way take.

This weblog is inspired from Tom Scott’s video about storing passwords on Computerphile channel on YouTube and additionally my know-how of Cryptography.

How no longer to save passwords on a database?

I do not think I actually have to mention this however plain textual content is a layout we must never consider while storing passwords. The undeniable text without a doubt is the password that the choices user has entered at the same time as signing up for the service.

For Example: If a consumer enters 123456 as a password, we definitely store 123456 on the choices database.

So definitely if the system gets compromised/hacked, we’re going to be displaying the real password of the choices consumer to the hacker. The major problem with that is, most of the human beings use the identical e mail and password aggregate at the same time as signing up for a couple of offerings. So the breacher may have get entry to to all the offerings.

Encryption approach changing a plain text (ordinary text) right into a cipher textual content (encrypted text) with the choices assist of some key.

For Example: We may have an set of rules that maps letters of the alphabet with the choices letters which can be shifted 3 positions. So, if plain text is abc then the cipher textual content might be def.

Technically, we’re adding a layer of security by means of really now not exposing the choices password however there is a problem with this approach. If the choices breacher gets the key then he can decrypt (achieve authentic message) from the choices encrypted password without difficulty. It’s easy to achieve this because the choices encryption algorithm is known to us and the only factor we don’t know is the key. So hypothetically, if the hacker has obtained the key then all the encrypted passwords in the database get compromised.

A cryptographic hash function is much like encryption set of rules. The simplest distinction is, if the choices message is processed with the cryptographic hash characteristic then it’s miles irreversible (i.e. we will now not get lower back the choices unique password/message from the choices hash). A cryptographic hash characteristic usually generates an output of fixed length and even a mild variation of plain text will bring about a very exclusive hash.

This is barely an improvisation over encryption however nevertheless, it possesses threats. The principal hassle with this method is, two passwords get converted to the choices same hash.

Simply, the breacher may carry out a bruteforce attack or a dictionary assault on the system to wager the choices password. He may take the choices listing guessed passwords, hash it with the choices hash function and if any of the choices hash suits the hash on the database then he has successfully decrypted the choices password from the choices hash.

How to do it proper?

As we’ve got discarded the choices opportunities of the way no longer to store passwords on the choices database, let’s communicate about how we ought to store passwords well.

The approach that we need to take at the same time as doing so is hashing and salting. In this mechanism, a salt is simply a string of random characters that gets delivered to the choices password on every occasion we save a new one on the choices database. If we try this, two distinct customers with the identical password could have a one-of-a-kind hash.

The salt is generated randomly whenever a new password is saved on the database. The salt is an extended and random string which will increase the choices price of doing a bruteforce attack.

The salt is covered in the password, consequently we don’t ought to one by one save the salt.

Example of using salting and hashing on Node.JS

To try this, we need a package referred to as bcrypt which we can down load from the choices NPM registry.

To hash the password, we need to generate a salt. After the technology of salt, we hash the password along with the choices randomly generated salt. All that is dealt with via the choices bcrypt package deal so we do not ought to assume an awful lot about it.

I ran the application multiple instances and each unmarried time, I turned into capable of get a distinct hash for the choices same password 123456.

Here are a number of the choices effects.

Some guidelines on creating passwords for everyday user

As a consumer, your intuition might be to save a password this is commonplace for every platform because you are probably to forget about your password in case you use a distinct password for each different platform. This isn’t always what you ought to be doing and I enormously suggest you to apply a password manager in this situation.

Also, do no longer create a password your self. Let the password supervisor generate a random password and keep it for you. If you achieve this, you most effective need to remember a strong grasp password that unlocks the password supervisor which in go back gives you get entry to to each password you have saved on the choices password manager.

Interested within the contents I share on the website?