npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

crypto.js

v3.2.1

Published

原生crypto加密模块的二次封装,简化常用加密函数的使用

Downloads

2,449

Readme

加密工具(Crypto)

本模块是对原生的crypto模块二次封装的,在使用上更加简单方便。

更新日志

  • v3.2.0

    • 调整cipher,decipher,cipheriv,decipheriv的传参和返回结果, aes-gcm等算法,tag会拼接在密文后面。
    • 增加crypto属性返回, 该属性为原生crypto对象
    • origin属性标识为Deprecated,v4.0之后会直接移除。
    • cipher,cipheriv, 默认的密钥和向量均为Buffer.alloc(16)
  • v3.1.2

    • 优化uuid(), 增加有序性
  • v3.1.0

    • 优化uuid(),rand()
    • 优化库引用方式
  • v3.0.0

    • Node.js 10.0.0之后不再推荐使用crypto.createCipher(), 所以 本库的cipher()方法, 内部改为调用cipheriv() (Node.js大于10.5.0时, 旧版本的不变)
  • v2.1.0

    • 优化cipher()等公钥加密方法的keyiv的默认值为crypto.scryptSync('', '', 16)
    • 使用ESBuild进行打包。
  • v2.0.5

    • 优化uuid()的实现。

安装

npm install crypto.js

说明

本模块内置2种不同的引入方式。

// 1、 传统的 commonJS引入, 所有的方法都在上面
var {
  origin, // 原生crypto对象
  crypto, // 原生crypto对象
  uuid, 
  rand, 
  md5, 
  md5Sign, 
  sha1, 
  sha1Sign,
  sha256,
  sha256Sign,
  base64encode,
  base64decode 
} = require('crypto.js')


// 2、 全新的 ESM 方式
import crypto from 'crypto.js'
import crypto, {
  origin, // 原生crypto对象
  crypto, // 原生crypto对象
  uuid, 
  rand, 
  md5, 
  md5Sign, 
  sha1, 
  sha1Sign,
  sha256,
  sha256Sign,
  base64encode,
  base64decode,
} from 'crypto.js'

属性

origin (Deprecated)

即为原生的crypto对象,方便在封装的方法中无法满足需求时,可以自行调用原生的crypto实现。

crypto (v3.2.0新增)

即为原生的crypto对象,方便在封装的方法中无法满足需求时,可以自行调用原生的crypto实现。

常用API方法

对使用频率非常高的几种加密/编码进行更加简便的封装。

rand(len[, onlyNumber])

  • len <Number> 需要的字符长度
  • onlyNumber <Boolean> 返回纯数字字符串 [可选]

该方法用于生成指定长度的随机字符串[a-z-A-z0-9]

let { rand } = require('crypto.js')
rand(6) // ddjF7d
rand(16) // 4sf7dJH6tGHDjhdf
rand(6,  true) // 439875
rand(10, true) // 3458765234

uuid()

返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID

1、加入机器码, 减小不同机器生成的uuid相同的机率 2、每秒可生成20万个ID(测试机器: Intel [email protected] 16G内存)

let { uuid } = require('crypto.js')
uuid() // 076d029f-4927-ec5f-5b06e35e

md5(str[, encode])

  • str <Number> | <String>
  • encode <String> 可选

这方法,应该没有人不知道是做什么的了,encode是要返回的字符串编码,默认为hex, 可选base64 不过有这需求的人可能也许大概...很少吧。

md5(123456) // e10adc3949ba59abbe56e057f20f883e
md5('123456') // e10adc3949ba59abbe56e057f20f883e
md5('hello world') // 5eb63bbbe01eeed093cb22bb8f5acdc3
md5('hello world', 'base64') // XrY7u+Ae7tCTyyK7j1rNww==

md5Sign(file)

  • file <String>

该方法用于计算文件的md5签名,file即为文件的路径。

md5Sign('xx.jpg')

sha1(str[, encode])

  • str <Number> | <String>
  • encode <String> 可选

这方法也应该没有人不知道是做什么的了,encode是要返回的字符串编码,默认为hex, 可选base64 不过有这需求的人可能也许大概...很少吧。

sha1(123456) // 7c4a8d09ca3762af61e59520943dc26494f8941b
sha1('123456') // 7c4a8d09ca3762af61e59520943dc26494f8941b
sha1('hello world') // 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
sha1('hello world', 'base64') // Kq5sNclPz7QV2+lfQIuc6R7oRu0=

sha1Sign(file)

  • file <String>

该方法用于计算文件的sha1签名,file即为文件的路径。

sha1Sign('xx.jpg')

sha256(str[, encode])

  • str <Number> | <String>
  • encode <String> 可选

自然这方法,也没啥好说的了。

sha256Sign(file)

  • file <String>

都懂的。

base64encode(str[, urlFriendly])

  • str <Number> | <String> | <Buffer>
  • urlFriendly <Boolean> 可选

这是用来进行base64编码的,本身没啥好说。主要是第2个参数,是指编码的结果是否对URL友好,默认为否; 如果为true,则会把+转成-,/转成_ (遵循RFC4648标准)。

base64encode('hello world') //aGVsbG8gd29ybGQ=

base64decode(str[, urlFriendly])

  • str <String>
  • urlFriendly <Boolean> 可选

base64解码, 返回Buffer对象。同样urlFriendly是指要解码的字符串之前是否采用了URL友好处理,默认否。

base64decode('aGVsbG8gd29ybGQ=')//  .toString('utf-8')  === hello world

更强大的API方法

除去上面8个更为常用的方法之外,还有很多很多也经常要到的,比如AES加密等,那此时就可以使用下面这里方法,自行配置了。

1. 散列算法

hash(mode, data[, outEncode])

  • mode <String>
  • data <String> | <Buffer>
  • outEncode '' 可选

散列算法(也称为哈希算法),用来实现一些重要数据的模糊处理,以达到隐藏明文的目的。 上面的md5、sha1、sha256等,其实就是基于这个再次封装的结果; mode,即算法类型,常用的有 md5, sha1, sha256, sha512等; data 即为要加密的字符串; outEncode是输出的编码类型;

crypto.hash('md5', '123456') //e10adc3949ba59abbe56e057f20f883e
// 等价于
md5('123456')

2. HMAC算法

hmac(mode, data[, key][, outEncode])

  • mode <String>
  • data <String> | <Buffer>
  • key <String> 可选
  • outEncode '' 可选

HMAC算法,是在散列算法的基础上,与一个密钥结合在一起,以阻止对签名完整性的破坏。 与上面的散列算法相比,多了一个密钥的参数key;

//a21cf00de4343af1b8b2087af07eb7b9
crypto.hmac('md5', '123456', 'sdfvkjfhd') 

3. 公钥加密

在上面的2种算法中,加密都是不可逆的,也就是说,加密后的字符,我们是没办法再还原回去了,但是有很多场景,需要我们对拿到的加密字符,还原到明文状态。 所以出现了公钥加密这种算法; 而Node.js本身给我们提供了4种与公钥加密相关的类:Cipher/Decipher、Sign、Verify,这里只讲前面2个,以及它们衍生出来的Cipheriv/Decipheriv;

Nodejs v10.0之后该方法为Deprecated, 推荐使用 cipheriv()

cipher(mode, data[, key, inEncode, outEncode])

  • mode <String>
  • data <String> | <Buffer>
  • key <String> 可选, 默认为 <Buffer d7 2c 87 d0 f0 77 c7 76 6f 29 85 df ab 30 e8 95>, 即 crypto.scryptSync('', '', 16) 的结果
  • inEncode '' 可选
  • outEncode '' 可选,默认返回Buffer对象

crypto.js v3.x开始, cipher()内部改成调用 cipheriv(), 如果有特别原因, 仍然要调用的话, 请使用 2.x版本

mode为算法类型,常见的有aes-128-cbc、aes-128-gcm等等地,很多,具体有哪些可以通过 this.crypto.getCiphers() 来查看。 其他的参数与上面的HMAC算法相似; inEncode即声明要加密的数据是什么编码的,默认根据要加密的数据进行判断。

// 这里给出一个AES-128-CBC的加密例子

crypto.cipher('aes-128-cbc', '123456', 'abcdefg')
// mqA9ZPh9VV+fwKlfpicGVg==

crypto.cipher('aes-128-cbc', '123456', 'abcdefg', 'utf8', 'hex')
// 9aa03d64f87d555f9fc0a95fa6270656



// v3.x 之后, decipher()同理
crypto.cipher('aes-128-cbc', '123456', {key})
// 等价于
crypto.cipheriv('aes-128-cbc', '123456', {key}, EMPTY_IV) // 其中 EMPTY_IV = Buffer.alloc(16)

decipher(mode, data[, key, tag, inEncode, outEncode])

  • mode <String>
  • data <String> | <Buffer>
  • key <String> 可选
  • tag <Buffer> 可选(mode为gcm算法时必填)
  • inEncode '' 可选, 默认是base64
  • outEncode '' 可选,默认utf8

这是与上面的cipher对应的解密方法;

Nodejs v10.0之后该方法为Deprecated, 推荐使用 decipheriv()

// 这里不用指定编码,默认即为base64
crypto.decipher('aes-128-cbc', 'mqA9ZPh9VV+fwKlfpicGVg==', 'abcdefg')
// 123456

// 这里一定要指定,因为之前加密的时候,指定输出为hex,所以这里也要指定输入的是hex编码
crypto.decipher('aes-128-cbc', '9aa03d64f87d555f9fc0a95fa6270656', 'abcdefg', 'hex')
// 123456


至于另外的cipheriv/decipheriv这2个方法,这里就不细讲了,和上面的这2个是同样的用法,只是要多1个参数向量(iv)

特别要注意的一点是,选择128位的加密算法,那key的长度就必须是16位,256则是32位,依此类推,具体的请看相关文档