数据加密-crypto
hash 例子
const content = fs.readFileSync("./aa.txt", { encoding: "utf-8" });
const hash = crypto.createHash("sha256");
let output;
hash.update(content);
output = hash.digest("hex");
console.log(output);
例子也可以这样
const input = fs.createReadStream("./aa.txt", { encoding: "utf-8" });
const hash = crypto.createHash("sha256");
hash.setEncoding("hex");
input.pipe(hash).pipe(process.stdout);
hash.digest() 后,再次调用 digest() 或者 update()
const content = fs.readFileSync("./aa.txt", { encoding: "utf-8" });
const hash = crypto.createHash("sha256");
let output;
hash.update(content);
hash.digest("hex");
HMAC 例子 例子 1
const secret = "secret";
const hmac = crypto.createHmac("sha256", secret);
const input = fs.readFileSync("./aa.txt", { encoding: "utf-8" });
hmac.update(input);
console.log(hmac.digest("hex"));
HMAC 例子 例子 2
const secret = "secret";
const hmac = crypto.createHmac("sha256", secret);
const input = fs.createReadStream("./aa.txt", { encoding: "utf-8" });
hmac.setEncoding("hex");
input.pipe(hmac).pipe(process.stdout);
加密/解密
加密;
crypto.createCipher(algorithm, password);
crypto.createCipheriv(algorithm, key, iv);
解密;
crypto.createDecipher(algorithm, password);
crypto.createDecipheriv(algorithm, key, iv);
加密 crypto.createCipher(algorithm, password)
const secret = "secret";
const cipher = crypto.createCipher("aes192", secret);
const content = "hello";
let cryptedContent;
cipher.update(content);
cryptedContent = cipher.final("hex");
console.log(cryptedContent);
解密 crypto.createDecipher(algorithm, password)
const secret = "secret";
const cipher = crypto.createCipher("aes192", secret);
const content = "hello";
let cryptedContent;
cipher.update(content);
cryptedContent = cipher.final("hex");
console.log(cryptedContent);
const decipher = crypto.createDecipher("aes192", secret);
let decryptedContent;
decipher.update(cryptedContent, "hex");
decryptedContent = decipher.final("utf8");
console.log(decryptedContent);
加密 crypto.createCipheriv(algorithm, key, iv)
解密 crypto.createDecipheriv(algorithm, key, iv)
const key = crypto.randomBytes(192 / 8);
const iv = crypto.randomBytes(128 / 8);
const algorithm = "aes192";
function encrypt(text) {
const cipher = crypto.createCipheriv(algorithm, key, iv);
cipher.update(text);
return cipher.final("hex");
}
function decrypt(encrypted) {
const decipher = crypto.createDecipheriv(algorithm, key, iv);
decipher.update(encrypted, "hex");
return decipher.final("utf8");
}
const content = "hello";
const crypted = encrypt(content);
console.log(crypted);
const decrypted = decrypt(crypted);
console.log(decrypted);
数组签名/签名校验
假设:
1.服务端的原始信息为M,摘要算法未Hash,Hash(M)得出的摘要是H
2.公钥是Pub,私钥是Piv,非对称加密算法为Encrypt,非对称解密算法为Decrypt
3.Encrypt(H)得到的结果是S
4.客户端拿到的信息为M1,利用Hash(M1)得出的结果是H1
数字签名的产生,检验步骤分别如下
1.数字签名的陈生步骤:利用摘要算法Hash算出M的摘要,即Hash(M) == HTMLAllCollection,利用非对称加密算法对摘要进行加密Encrypt(H,Piv)得到数字签名S
2.数字签名的检验步骤,利用解密算法D对数字签名进行解密,即Decrypt(S) == H,计算M1的摘要 Hash(M1) == H1,如果两者相同,则通过检验.
const privateKey = fs.readFileSync('./private-key.pem')
const publicKey = fs.readFileSync('./public-key.pem')
const algorithm = "RSA-SHA256"
function sign(text) {
const sign = crypto.createSign(algorithm)
sign.update(text)
return sign.sign(privateKey, 'hex')
}
function verify(oriContent, signature) {
const verifer = crypto.createVerify(algorithm)
verifer.update(oriContent)
return verifer.verify(publicKey, signature, 'hex')
}
const content = 'hello world'
const signature = sign(content)
console.log(signature);
const verifed = verify(content, signature)
console.log(verifed);
diffieHellman
const alice = crypto.createDiffieHellman(2048);
const aliceKey = alice.generateKeys();
const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
const bobKey = bob.generateKeys();
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);
assert.equal(aliceSecret.toString("hex"), bobSecret.toString("hex"));
ECDH Elliptic Curve Diffie-Hellman 椭圆曲线
const alice = crypto.createECDH("secp521r1");
const aliceKey = alice.generateKeys();
const bob = crypto.createECDH("secp521r1");
const bobKey = bob.generateKeys();
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = alice.computeSecret(aliceKey);
assert(aliceSecret, bobSecret);
crypto.createECDH(curveName)
ecdh.computeSecret(otherPublicKey, inputEncoding, outputEncoding)
ecdn.generateKeys(encoding, format)
关键点
相关术语