crypto模塊提供通用的加密和哈希算法
MD5和SHA1
const crypto = require('crypto');
const hash = crypto.createHash('md5');
// 可任意多次調(diào)用update():
hash.update('Hello, world!');
hash.update('Hello, nodejs!');
//相當(dāng)于hash.update('Hello, world!Hello, nodejs!');
console.log(hash.digest('hex')); //7e1977739c748beac0c0fd14fd26a544
update()方法默認(rèn)字符串編碼為UTF-8
計(jì)算SHA1馏颂,只需要把'md5'改成'sha1'
還可以使用更安全的sha256和sha512示血。
Hmac
用隨機(jī)數(shù)“增強(qiáng)”的哈希算法。
const crypto = require('crypto');
const hmac = crypto.createHmac('sha256', 'secret-key');
hmac.update('Hello, world!');
hmac.update('Hello, nodejs!');
console.log(hmac.digest('hex')); // 80f7e22570...
AES
AES是一種常用的對(duì)稱加密算法救拉,加解密都用同一個(gè)密鑰难审。
const crypto = require('crypto');
function aesEncrypt(data, key) {
const cipher = crypto.createCipher('aes192', key);
var crypted = cipher.update(data, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
}
function aesDecrypt(encrypted, key) {
const decipher = crypto.createDecipher('aes192', key);
var decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
var data = 'Hello, this is a secret message!';
var key = 'Password!';
var encrypted = aesEncrypt(data, key);
var decrypted = aesDecrypt(encrypted, key);
console.log('Plain text: ' + data);
console.log('Encrypted text: ' + encrypted);
console.log('Decrypted text: ' + decrypted);
運(yùn)行結(jié)果
Plain text: Hello, this is a secret message!
Encrypted text: 8a944d97bdabc157a5b7a40cb180e7...
Decrypted text: Hello, this is a secret message!
Diffie-Hellman
DH算法是一種密鑰交換協(xié)議,它可以讓雙方在不泄漏密鑰的情況下協(xié)商出一個(gè)密鑰來亿絮。DH算法基于數(shù)學(xué)原理告喊,比如小明和小紅想要協(xié)商一個(gè)密鑰,可以這么做:
小明先選一個(gè)素?cái)?shù)和一個(gè)底數(shù)派昧,例如黔姜,素?cái)?shù)p=23,底數(shù)g=5(底數(shù)可以任選)蒂萎,再選擇一個(gè)秘密整數(shù)a=6秆吵,計(jì)算A=g^a mod p=8,然后大聲告訴小紅:p=23五慈,g=5纳寂,A=8;
小紅收到小明發(fā)來的p泻拦,g毙芜,A后,也選一個(gè)秘密整數(shù)b=15争拐,然后計(jì)算B=g^b mod p=19腋粥,并大聲告訴小明:B=19;
小明自己計(jì)算出s=B^a mod p=2陆错,小紅也自己計(jì)算出s=A^b mod p=2灯抛,因此,最終協(xié)商的密鑰s為2音瓷。
在這個(gè)過程中对嚼,密鑰2并不是小明告訴小紅的,也不是小紅告訴小明的绳慎,而是雙方協(xié)商計(jì)算出來的纵竖。第三方只能知道p=23漠烧,g=5,A=8靡砌,B=19已脓,由于不知道雙方選的秘密整數(shù)a=6和b=15,因此無(wú)法計(jì)算出密鑰2通殃。
用crypto模塊實(shí)現(xiàn)DH算法如下:
const crypto = require('crypto');
// xiaoming's keys:
var ming = crypto.createDiffieHellman(512);
var ming_keys = ming.generateKeys();
var prime = ming.getPrime();
var generator = ming.getGenerator();
console.log('Prime: ' + prime.toString('hex'));
console.log('Generator: ' + generator.toString('hex'));
// xiaohong's keys:
var hong = crypto.createDiffieHellman(prime, generator);
var hong_keys = hong.generateKeys();
// exchange and generate secret:
var ming_secret = ming.computeSecret(hong_keys);
var hong_secret = hong.computeSecret(ming_keys);
// print secret:
console.log('Secret of Xiao Ming: ' + ming_secret.toString('hex'));
console.log('Secret of Xiao Hong: ' + hong_secret.toString('hex'));
$ node dh.js
Prime: a8224c...deead3
Generator: 02
Secret of Xiao Ming: 695308...d519be
Secret of Xiao Hong: 695308...d519be
注意每次輸出都不一樣度液,因?yàn)樗財(cái)?shù)的選擇是隨機(jī)的。