Node.jsCrypto模块
Node.js加密模块支持加密。它提供了加密功能,其中包括用于开放SSL的哈希HMAC,加密,解密,签名和验证功能的一组包装器。
什么是Hash - 哈希是一系列的比特串,即,从一些任意源数据块的过程和确定地生成。
什么是HMAC - HMAC代表基于哈希的消息身份验证码。这是一个将哈希算法应用于数据和密钥的过程,从而导致单个最终哈希。
使用Hash和HMAC加密
文件:crypto_example1.js.
const crypto = require('crypto'); const secret = 'abcdefg'; const hash = crypto.createHmac('sha256', secret) .update('Welcome to Learnfk') .digest('hex'); console.log(hash);
node crypto_example1.js

使用Cipher加密
文件:crypto_example2.js.
const crypto = require('crypto'); const cipher = crypto.createCipher('aes192', 'a password'); var encrypted = cipher.update('Hello Learnfk', 'utf8', 'hex'); encrypted += cipher.final('hex'); console.log(encrypted);
node crypto_example2.js

使用Decipher解密
文件:crypto_example3.js.
const crypto = require('crypto'); const decipher = crypto.createDecipher('aes192', 'a password'); var encrypted = '4ce3b761d58398aed30d5af898a0656a3174d9c7d7502e781e83cf6b9fb836d5'; var decrypted = decipher.update(encrypted, 'hex', 'utf8'); decrypted += decipher.final('utf8'); console.log(decrypted);
node crypto_example3.js
祝学习愉快! (发现内容有误?请选中要编辑的内容 -> 右键 -> 修改 -> 提交!帮助我们改进教程质量)
精选教程推荐
👇 以下精选教程可能对您有帮助,拓展您的技术视野
暂无学习笔记,成为第一个分享的人吧!
您的笔记将帮助成千上万的学习者