更新readme
parent
7bbec9afbf
commit
83d3f04b44
68
Readme.md
68
Readme.md
|
@ -1,6 +1,16 @@
|
|||
# 加密工具(Crypto)
|
||||
> 本模块是对原生的`crypto`模块二次封装的,在使用上更加简单方便。
|
||||
|
||||
## 更新日志
|
||||
+ v3.0.0
|
||||
- Node.js 10.0.0之后不再推荐使用`crypto.createCipher()`, 所以 本库的`cipher()`方法, 内部改为调用`cipheriv()`
|
||||
|
||||
+ v2.1.0
|
||||
- 优化`cipher()`等公钥加密方法的`key`和`iv`的默认值为`crypto.scryptSync('', '', 16)`
|
||||
- 使用ESBuild进行打包。
|
||||
|
||||
+ v2.0.5
|
||||
- 优化`uuid()`的实现。
|
||||
|
||||
## 安装
|
||||
|
||||
|
@ -14,7 +24,19 @@ npm install crypto.js
|
|||
|
||||
```js
|
||||
// 1、 传统的 commonJS引入, 所有的方法都在上面
|
||||
var crypto = require('crypto.js')
|
||||
var {
|
||||
default: crypto,
|
||||
uuid,
|
||||
rand,
|
||||
md5,
|
||||
md5Sign,
|
||||
sha1,
|
||||
sha1Sign,
|
||||
sha256,
|
||||
sha256Sign,
|
||||
base64encode,
|
||||
base64decode
|
||||
} = require('crypto.js')
|
||||
|
||||
|
||||
// 2、 全新的 ESM 方式
|
||||
|
@ -49,11 +71,11 @@ import {
|
|||
> 该方法用于生成指定长度的随机字符串`[a-z-A-z0-9]`
|
||||
|
||||
```javascript
|
||||
let crypto = require('crypto.js')
|
||||
crypto.rand(6) // ddjF7d
|
||||
crypto.rand(16) // 4sf7dJH6tGHDjhdf
|
||||
crypto.rand(6, true) // 439875
|
||||
crypto.rand(10, true) // 3458765234
|
||||
let { rand } = require('crypto.js')
|
||||
rand(6) // ddjF7d
|
||||
rand(16) // 4sf7dJH6tGHDjhdf
|
||||
rand(6, true) // 439875
|
||||
rand(10, true) // 3458765234
|
||||
```
|
||||
|
||||
### uuid()
|
||||
|
@ -62,8 +84,8 @@ crypto.rand(10, true) // 3458765234
|
|||
>> 2、每秒可生成20万个ID(测试机器: Intel i5-8500B@3.00GHz 16G内存)
|
||||
|
||||
```javascript
|
||||
let crypto = require('crypto.js')
|
||||
crypto.uuid() // 076d029f-4927-ec5f-5b06e35e
|
||||
let { uuid } = require('crypto.js')
|
||||
uuid() // 076d029f-4927-ec5f-5b06e35e
|
||||
|
||||
```
|
||||
|
||||
|
@ -76,10 +98,10 @@ crypto.uuid() // 076d029f-4927-ec5f-5b06e35e
|
|||
> 这方法,应该没有人不知道是做什么的了,`encode`是要返回的字符串编码,默认为`hex`, 可选`base64` 不过有这需求的人可能也许大概...很少吧。
|
||||
|
||||
```javascript
|
||||
crypto.md5(123456) // e10adc3949ba59abbe56e057f20f883e
|
||||
crypto.md5('123456') // e10adc3949ba59abbe56e057f20f883e
|
||||
crypto.md5('hello world') // 5eb63bbbe01eeed093cb22bb8f5acdc3
|
||||
crypto.md5('hello world', 'base64') // XrY7u+Ae7tCTyyK7j1rNww==
|
||||
md5(123456) // e10adc3949ba59abbe56e057f20f883e
|
||||
md5('123456') // e10adc3949ba59abbe56e057f20f883e
|
||||
md5('hello world') // 5eb63bbbe01eeed093cb22bb8f5acdc3
|
||||
md5('hello world', 'base64') // XrY7u+Ae7tCTyyK7j1rNww==
|
||||
```
|
||||
|
||||
|
||||
|
@ -89,7 +111,7 @@ crypto.md5('hello world', 'base64') // XrY7u+Ae7tCTyyK7j1rNww==
|
|||
> 该方法用于计算文件的md5签名,`file`即为文件的路径。
|
||||
|
||||
```javascript
|
||||
crypto.md5Sign('xx.jpg')
|
||||
md5Sign('xx.jpg')
|
||||
```
|
||||
|
||||
|
||||
|
@ -100,10 +122,10 @@ crypto.md5Sign('xx.jpg')
|
|||
> 这方法也应该没有人不知道是做什么的了,`encode`是要返回的字符串编码,默认为`hex`, 可选`base64` 不过有这需求的人可能也许大概...很少吧。
|
||||
|
||||
```javascript
|
||||
crypto.sha1(123456) // 7c4a8d09ca3762af61e59520943dc26494f8941b
|
||||
crypto.sha1('123456') // 7c4a8d09ca3762af61e59520943dc26494f8941b
|
||||
crypto.sha1('hello world') // 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
|
||||
crypto.sha1('hello world', 'base64') // Kq5sNclPz7QV2+lfQIuc6R7oRu0=
|
||||
sha1(123456) // 7c4a8d09ca3762af61e59520943dc26494f8941b
|
||||
sha1('123456') // 7c4a8d09ca3762af61e59520943dc26494f8941b
|
||||
sha1('hello world') // 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
|
||||
sha1('hello world', 'base64') // Kq5sNclPz7QV2+lfQIuc6R7oRu0=
|
||||
```
|
||||
|
||||
|
||||
|
@ -113,7 +135,7 @@ crypto.sha1('hello world', 'base64') // Kq5sNclPz7QV2+lfQIuc6R7oRu0=
|
|||
> 该方法用于计算文件的sha1签名,`file`即为文件的路径。
|
||||
|
||||
```javascript
|
||||
crypto.sha1Sign('xx.jpg')
|
||||
sha1Sign('xx.jpg')
|
||||
```
|
||||
|
||||
|
||||
|
@ -139,7 +161,7 @@ crypto.sha1Sign('xx.jpg')
|
|||
> 这是用来进行base64编码的,本身没啥好说。主要是第2个参数,是指编码的结果是否对URL友好,默认为否; 如果为true,则会把+转成-,/转成_ (遵循RFC4648标准)。
|
||||
|
||||
```javascript
|
||||
crypto.base64encode('hello world') //aGVsbG8gd29ybGQ=
|
||||
base64encode('hello world') //aGVsbG8gd29ybGQ=
|
||||
|
||||
```
|
||||
|
||||
|
@ -152,7 +174,7 @@ crypto.base64encode('hello world') //aGVsbG8gd29ybGQ=
|
|||
> base64解码, 返回Buffer对象。同样`urlFriendly`是指要解码的字符串之前是否采用了URL友好处理,默认否。
|
||||
|
||||
```javascript
|
||||
crypto.base64decode('aGVsbG8gd29ybGQ=')// .toString('utf-8') === hello world
|
||||
base64decode('aGVsbG8gd29ybGQ=')// .toString('utf-8') === hello world
|
||||
|
||||
```
|
||||
|
||||
|
@ -175,6 +197,8 @@ crypto.base64decode('aGVsbG8gd29ybGQ=')// .toString('utf-8') === hello world
|
|||
|
||||
```javascript
|
||||
crypto.hash('md5', '123456') //e10adc3949ba59abbe56e057f20f883e
|
||||
// 等价于
|
||||
md5('123456')
|
||||
|
||||
```
|
||||
|
||||
|
@ -203,10 +227,12 @@ crypto.hmac('md5', '123456', 'sdfvkjfhd')
|
|||
#### cipher(mode, data[, key, inEncode, outEncode])
|
||||
- mode `<String>`
|
||||
- data `<String>` | `<Buffer>`
|
||||
- key `<String>` 可选
|
||||
- key `<String>` 可选, 默认为 `<Buffer d7 2c 87 d0 f0 77 c7 76 6f 29 85 df ab 30 e8 95>`, 即 `crypto.scryptSync('', '', 16)` 的结果
|
||||
- inEncode '<String>' 可选
|
||||
- outEncode '<String>' 可选,默认base64
|
||||
|
||||
> Node.js v10.0.0 之后, `crypto.createDecipher()`方法开始不推荐使用。所以, 在 `crypto.js v3.x`开始, `cipher()`内部改成调用 `cipheriv()`, 如果有特别原因, 仍然要调用的话, 请使用 `2.x版本`
|
||||
|
||||
> `mode`为算法类型,常见的有`aes-128-cbc、aes-128-gcm`等等地,很多,具体有哪些可以通过 `this.crypto.getCiphers()` 来查看。
|
||||
> 其他的参数与上面的HMAC算法相似; `inEncode`即声明要加密的数据是什么编码的,默认根据要加密的数据进行判断。
|
||||
>> 需要注意的是, 算法类型为`aes-***-gcm`时, 返回的不是一个字符串, 而是一个对象{ enStr, authTag }, 解密时, 需要提供这个 authTag方可解密
|
||||
|
|
3
build.js
3
build.js
|
@ -19,6 +19,5 @@ Es.build({
|
|||
outfile: 'dist/index.js',
|
||||
platform: 'node',
|
||||
bundle: true,
|
||||
minify: true,
|
||||
format: 'cjs'
|
||||
minify: true
|
||||
})
|
||||
|
|
|
@ -11,12 +11,17 @@ const AUTH_MODE = [
|
|||
'aes-192-ocb',
|
||||
'aes-256-ocb'
|
||||
]
|
||||
const VERSION = +process.versions.node
|
||||
.split('.')
|
||||
.slice(0, 2)
|
||||
.join('.')
|
||||
// const VERSION = +process.versions.node
|
||||
// .split('.')
|
||||
// .slice(0, 2)
|
||||
// .join('.')
|
||||
|
||||
const EMPTY_KEY = crypto.scryptSync('', '', 16)
|
||||
// <Buffer d7 2c 87 d0 f0 77 c7 76 6f 29 85 df ab 30 e8 95>
|
||||
const EMPTY_KEY = crypto.scryptSync
|
||||
? crypto.scryptSync('', '', 16)
|
||||
: Buffer.from(
|
||||
'0xd7 0x2c 0x87 0xd0 0xf0 0x77 0xc7 0x76 0x6f 0x29 0x85 0xdf 0xab 0x30 0xe8 0x95'.split(' ')
|
||||
)
|
||||
|
||||
export default {
|
||||
origin: crypto,
|
||||
|
@ -40,9 +45,9 @@ export default {
|
|||
|
||||
cipher(mode, data, key = EMPTY_KEY, inEncode, outEncode) {
|
||||
// 10.0.0之后, createCipher方法不推荐使用了
|
||||
if (VERSION >= 10.5) {
|
||||
return this.cipheriv(mode, data, key, EMPTY_KEY, inEncode, outEncode)
|
||||
}
|
||||
// if (VERSION >= 10.5) {
|
||||
// return this.cipheriv(mode, data, key, EMPTY_KEY, inEncode, outEncode)
|
||||
// }
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
inEncode = isBuffer ? 'binary' : inEncode || 'utf8'
|
||||
outEncode = outEncode || 'base64'
|
||||
|
@ -59,9 +64,9 @@ export default {
|
|||
|
||||
decipher(mode, data, key = EMPTY_KEY, tag, inEncode, outEncode) {
|
||||
// 10.0.0之后, createCipher方法不推荐使用了
|
||||
if (VERSION >= 10.5) {
|
||||
return this.decipheriv(mode, data, key, EMPTY_KEY, tag, inEncode, outEncode)
|
||||
}
|
||||
// if (VERSION >= 10.5) {
|
||||
// return this.decipheriv(mode, data, key, EMPTY_KEY, tag, inEncode, outEncode)
|
||||
// }
|
||||
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
inEncode = isBuffer ? 'binary' : inEncode || 'base64'
|
||||
|
@ -76,7 +81,7 @@ export default {
|
|||
return deStr
|
||||
},
|
||||
|
||||
cipheriv(mode, data, key = EMPTY_KEY, iv = null, inEncode, outEncode) {
|
||||
cipheriv(mode, data, key = EMPTY_KEY, iv = EMPTY_KEY, inEncode, outEncode) {
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
inEncode = isBuffer ? 'binary' : inEncode || 'utf8'
|
||||
outEncode = outEncode || 'base64'
|
||||
|
@ -91,7 +96,7 @@ export default {
|
|||
return enStr
|
||||
},
|
||||
|
||||
decipheriv(mode, data, key = EMPTY_KEY, iv = null, tag, inEncode, outEncode) {
|
||||
decipheriv(mode, data, key = EMPTY_KEY, iv = EMPTY_KEY, tag, inEncode, outEncode) {
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
inEncode = isBuffer ? 'binary' : inEncode || 'base64'
|
||||
outEncode = outEncode || 'utf8'
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
import os from 'os'
|
||||
import fs from 'fs'
|
||||
import Helper from './lib/helper.mjs'
|
||||
import Helper from './helper.mjs'
|
||||
|
||||
const MAC = (function(ns) {
|
||||
for (let k in ns) {
|
||||
|
@ -91,9 +91,7 @@ export function uuid(pipe = '-') {
|
|||
}
|
||||
str = md5(MAC + rand + __inc__)
|
||||
|
||||
return (
|
||||
now + pipe + str.slice(0, 4) + pipe + str.slice(4, 8) + pipe + str.slice(-8)
|
||||
)
|
||||
return now + pipe + str.slice(0, 4) + pipe + str.slice(4, 8) + pipe + str.slice(-8)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue