Crypto
Crypto?
- Crypto is built-in cryptographic module for node
- Crypto can be used for implementing cryptographic protocols, such as:
- TLS
- https
- Crypto offers functions for calculating hashes, using ciphers and more
Hashing
- Hashing is one way data encryption method
- A hash is (in theory) impossible to invert
- Hashes are commonly used for storing passwords in databases
- Storing just hash of the password is not considered secure
- Password should be hashed with a salt
- Salt is a random string that is included in password before hashing
- Salt should be unique for each stored password
Creating hash using crypto
var crypto = require("crypto");
// Hash in hex format
crypto.createHash("sha1").update("Lorem ipsum!").digest("hex");
// Output: 8a52c69607956f0a186fa4efc75e6f66db1568b3
Supported hashing algorithms:
- md5
- sha1
- sha512
- .. and much more (use crypto.getHashes() to check available algorithms)
Ciphers
- Ciphers allow messages to be encrypted with a password
- Ciphers look similar to hashes, but they are reversible if recipient knows the password for the cipher
Cipher example
var crypto = require('crypto')
var pwd = 'password'
var text = 'Salainen viesti!'
var cipher = crypto.createCipher('aes256', pwd)
var decipher = crypto.createDecipher('aes256', pwd);
var salattu = cipher.update(text, 'utf8', 'hex');
salattu += cipher.final('hex')
var salaamaton = decipher.update(salattu, 'hex', 'utf8');
salaamaton += decipher.final('utf8');
console.log('salattu :', salattu);
// fcf5304a2dc5763509f1de77ab0a457b4c9879ec34c3e5828dcacd91145b8cc4
console.log('salaamaton :', salaamaton);
// Salainen viesti!
crypto
By Oscar Lemström
crypto
- 919