crypto 简单了解
文章是我整理出来的笔记略微粗糙,记录crypto库的简单了解和用法。
- crypto
- Hash(散列)算法
- Hmac算法
- PBKDF2函数
- 对称加密
- 小结
1. crypto
2.Hash(散列)算法
const crypto = require('crypto')
//创建并返回一个 Hash 对象,该对象可用于生成哈希摘要(参数为给定的算法)。
// const hash = crypto.createHash('md5');
const hash = crypto.createHash('sha256')
// 可任意多次调用update():
// 指定要摘要的原始内容,可以在摘要被输出之前使用多次update方法来添加摘要内容
hash.update('Hello, world!')
hash.update('Hello, nodejs!')
// 计算传入要被哈希(使用 hash.update() 方法)的所有数据的摘要。
// 如果提供了 encoding,则返回字符串,否则返回 Buffer。
console.log(hash.digest('hex').length)
crypto-js:
let crypto = require('crypto-js')
let hash = crypto.algo.SHA256.create()
hash.update('Hello, world!')
hash.update('Hello, nodejs!')
let result = hash.finalize()
// WordArray object
console.log(result.toString(crypto.enc.Hex));
- 作为可读写的流,其中写入数据以在可读侧生成计算后的哈希摘要(继承自: </stream.Transform>)。
- 使用 hash.update() 和 hash.digest() 方法生成计算后的哈希。
3. Hmac算法
let crypto = require('crypto')
// 密钥
let key = '密钥'
// 选定的摘要算法 sha256 选的密钥 key
let hmac = crypto.createHmac('sha256',key)
hmac.update('Hello, world!');
hmac.update('Hello, nodejs!');
let result = hmac.digest('hex');
console.log(result)
let CryptoJS = require('crypto-js')
// 密钥
let key = '密钥'
// 密钥 keyBit 摘要算法 sha256
let hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
hmac.update('Hello, world!')
hmac.update('Hello, nodejs!')
let result = hmac.finalize()
// WordArray object
console.log(result.toString(CryptoJS.enc.Hex));
- 作为可读写的流,其中写入数据以在可读侧生成计算后的 HMAC 摘要(继承自: </stream.Transform>)。
- 使用 hmac.update() 和 hmac.digest() 方法生成计算后的 HMAC 摘要。
4.PBKDF2函数
let crypto = require('crypto')
let password = 'luoxiaobuhaha' // 用来生成密钥的原文密码
let salt = 'salt' // 一个加密用的盐值。
let iterations = 10000 // 迭代次数
let keylen = 16 // 期望得到秘钥的长度
// 由选择HMAC摘要算法,digest 以导出所请求的字节长度
let digest = 'sha256' // 一个伪随机函数
crypto.pbkdf2(password, salt, iterations, keylen, digest, (err, derivedKey) => {
if (err) throw err
console.log(derivedKey.toString('hex'))
})
let CryptoJS = require('crypto-js')
var derivedKey = CryptoJS.PBKDF2(password, salt, {
keySize: 4, // // WordArray object
iterations: iterations,
hasher: CryptoJS.algo.SHA256
});
console.log(derivedKey.toString(CryptoJS.enc.Hex))
5.对称加密
let crypto = require('crypto')
// data:需要加解密的内容,
// key: 密钥
// 初始化向量(iv)
function aesEncrypt(data, key, iv) {
// 给定的算法,密钥和初始化向量(iv)创建并返回Cipher对象
const cipher = crypto.createCipheriv('aes-192-cbc', key, iv)
// Key length is dependent on the algorithm. In this case for aes192, it is 24 bytes (192 bits).
// 指定要摘要的原始内容,可以在摘要被输出之前使用多次update方法来添加摘要内容
// 数据的编码 utf8 返回值的编码 hex
var crypted = cipher.update(data, 'utf8', 'hex')
crypted += cipher.final('hex')
return crypted
}
function aesDecrypt(data, key, iv) {
// 给定的算法,密钥和初始化向量(iv)创建并返回Cipher对象
const decipher = crypto.createDecipheriv('aes-192-cbc', key, iv)
// 数据的编码 hex 返回值的编码 utf8
var decrypted = decipher.update(data, 'hex', 'utf8')
decrypted += decipher.final('utf8')
return decrypted
}
const IV = 'f710b45f04e37709' // 初始化向量(iv)
let data = 'luoxiaobu' // 需要加解密的内容,
let key = '123456789987654321123456' // 24 位秘钥密钥
let encryptData = aesEncrypt(data, key, IV)
let decryptData = aesDecrypt(encryptData, key, IV)
console.log(encryptData)
console.log(decryptData)
b98a1d87ea00fb47ade2d9cff0a9179d
luoxiaobu
const CryptoJS = require('crypto-js')
const IV = CryptoJS.enc.Utf8.parse('f710b45f04e37709') // 十六位十六进制数作为密钥偏移量
let data = 'luoxiaobu' // 需要加解密的内容,
let key = CryptoJS.enc.Utf8.parse('123456789987654321123456') // 24 位秘钥密钥
function decrypt(data, key, iv) {
let dataHexStr = CryptoJS.enc.Hex.parse(data);
let dataBase64 = CryptoJS.enc.Base64.stringify(dataHexStr);
// 接收的数据是 base64
let decrypt = CryptoJS.AES.decrypt(dataBase64, key, { iv: iv, mode: CryptoJS.mode.CBC});
let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
//加密方法
function encrypt(data, key, iv) {
let encrypted = CryptoJS.AES.encrypt(data, key, { iv: iv, mode: CryptoJS.mode.CBC});
return encrypted.ciphertext.toString().toUpperCase();;
}
let encryptData = encrypt(data, key, IV)
let decryptData = decrypt(encryptData, key, IV)
console.log(encryptData)
console.log(decryptData)
b98a1d87ea00fb47ade2d9cff0a9179d
luoxiaobu
6.小结
crypto 简单了解的更多相关文章
- nodejs加密Crypto简单例子
加密技术通常分为两大类:“对称式”和“非对称式”. 对称式加密: 就是加密和解密使用同一个密钥,通常称之为“Session Key ”这种加密技术在当今被广泛采用,如美国政府所采用的DES加密标准就是 ...
- salesforce 零基础学习(四十七) 数据加密简单介绍
对于一个项目来说,除了稳定性以及健壮性以外,还需要有较好的安全性,此篇博客简单描述salesforce中关于安全性的一点小知识,特别感谢公司中的nate大神和鹏哥让我学到了新得知识. 项目简单背景: ...
- 使用crypto模块实现md5加密功能(解决中文加密前后端不一致的问题)
正常情况下使用md5加密 var crypto = require('crypto'); var md5Sign = function (data) { var md5 = crypto.create ...
- Linux内核分析第三周学习总结:构造一个简单的Linux系统MenuOS
韩玉琪 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.Linux内 ...
- linux c libcurl的简单使用(转)
curl是Linux下一个非常著名的下载库,通过这个库,可以很简单的实现文件的下载等操作.看一个简单的例子: #include <curl/curl.h> #include <std ...
- openstack(liberty):部署实验平台(二,简单版本软件安装 part2)
继续前面的part1,将后续的compute以及network部分的安装过程记录完毕! 首先说说compute部分nova的安装. n1.准备工作.创建数据库,配置权限!(密码依旧是openstack ...
- NodeJs 中的Crypto 加密模块
加密技术通常分为两大类:“对称式”和“非对称式”. 对称式加密: 就是加密和解密使用同一个密钥,通常称之为“Session Key ”这种加密技术在当今被广泛采用,如美国政府所采用的DES加密标准就是 ...
- 设计一个简单的,低耗的能够区分红酒和白酒的感知器(sensor)
学习using weka in your javacode 主要学习两个部分的代码:1.过滤数据集 2 使用J48决策树进行分类.下面的例子没有对数据集进行分割,完全使用训练集作为测试集,所以不符合数 ...
- Linux 内核开发—内核简单介绍
内核简单介绍 Linux 构成 Linux 为什么被划分为系统空间和内核空间 隔离核心程序和应用程序,实现对核心程序和数据的保护. 什么内核空间,用户空间 内核空间和用户空间是程序执行的两种不同的状态 ...
随机推荐
- 学习笔记: Expression表达式目录树详解和扩展封装
1. 表达式链接扩展封装,ORM常用 And Or /// <summary> /// 表达式访问者 /// </summary> public class Expressi ...
- mybatis 开发规范
- sql面试总结
http://blog.csdn.net/a379850992/article/details/55655495
- nginx 错误502 upstream sent too big header while reading response header from upstream
查看nginx的错误日志,得到以下错误信息:upstream sent too big header while reading response header from upstream按字面意思理 ...
- 基于IPv6的数据包分析
1.首先我们来构建拓扑:如下所示 2.对各个路由器进行配置使得网络ping通:命令如下 a)配置各路由器接口的IPv6地址,可由上图注释配置 b)配置各路由器的静态路由(此处举例R4) (global ...
- CodeForces - 1013B And 与运算暴力
题目链接: https://vjudge.net/problem/1735275/origin 基本思路: 本题思路比较简单,首先,我们知道 a & x = b, b & x = b; ...
- 基于for循环的呼吸灯
#include "stm32f10x.h" #include "stm32f10x_gpio.h" //#include "led.h" ...
- css 制作圆角、圆形图形布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 输入、输出与Mad Libs 游戏
name1=input('请输入一个名字:') name2=input('再输入一个名字:') time1=input('请输入一段时间:') print('{},是傻子,{},{}找不到小栗旬'.f ...
- js的一些
// 1 定时器参数问题 无限个参数 /*setTimeout(function(num1,num2,num3,num4){ alert(num1+num2+num3+num4) },1000,1,2 ...