Node.js - Crypto模块

Node.js - Crypto模块 首页 / Node.js入门教程 / Node.js - Crypto模块

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

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

技术教程推荐

编辑训练营 -〔总编室〕

网络编程实战 -〔盛延敏〕

说透中台 -〔王健〕

深入浅出云计算 -〔何恺铎〕

Redis核心技术与实战 -〔蒋德钧〕

零基础实战机器学习 -〔黄佳〕

Go进阶 · 分布式爬虫实战 -〔郑建勋〕

Dubbo源码剖析与实战 -〔何辉〕

Rust 语言从入门到实战 -〔唐刚〕

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