Node.jsCrypto模块

Node.jsCrypto模块 首页 / Node.js入门教程 / 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.js命令提示符并运行以下代码:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/nodejs/nodejs-crypto.html

来源:LearnFk无涯教程网

node crypto_example1.js
Node.js crypto example 1

使用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.js命令提示符并运行以下代码:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/nodejs/nodejs-crypto.html

来源:LearnFk无涯教程网

node crypto_example2.js
Node.js crypto example 2

使用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.js命令提示符并运行以下代码:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/nodejs/nodejs-crypto.html

来源:LearnFk无涯教程网

node crypto_example3.js
Node.js crypto example 3

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

云原生基础架构实战课 -〔潘野〕

超级访谈:对话道哥 -〔吴翰清(道哥)〕

手把手带你写一个MiniSpring -〔郭屹〕

人人都用得上的数字化思维课 -〔付晓岩〕

反爬虫兵法演绎20讲 -〔DS Hunter〕

程序员的个人财富课 -〔王喆〕

技术面试官识人手册 -〔熊燚(四火)〕

说透中台 -〔王健〕

软件工程之美 -〔宝玉〕

好记忆不如烂笔头。留下您的足迹吧 :)