cipher()内部判断Node.js版本, 大于10.5时,改为调用cipheriv()

master 3.0.0
yutent 2022-01-08 16:43:29 +08:00
parent 4617514a7c
commit 3cc8c53a72
3 changed files with 16 additions and 12 deletions

View File

@ -3,7 +3,7 @@
## 更新日志 ## 更新日志
+ v3.0.0 + v3.0.0
- Node.js 10.0.0之后不再推荐使用`crypto.createCipher()`, 所以 本库的`cipher()`方法, 内部改为调用`cipheriv()` - Node.js 10.0.0之后不再推荐使用`crypto.createCipher()`, 所以 本库的`cipher()`方法, 内部改为调用`cipheriv()` (Node.js大于10.5.0时, 旧版本的不变)
+ v2.1.0 + v2.1.0
- 优化`cipher()`等公钥加密方法的`key`和`iv`的默认值为`crypto.scryptSync('', '', 16)` - 优化`cipher()`等公钥加密方法的`key`和`iv`的默认值为`crypto.scryptSync('', '', 16)`
@ -252,6 +252,10 @@ crypto.cipher('aes-128-gcm', '123456', 'abcdefg')
// authTag: <Buffer c4 a0 3e ab e5 34 a0 ea 25 02 f0 91 06 f7 3b dd> // authTag: <Buffer c4 a0 3e ab e5 34 a0 ea 25 02 f0 91 06 f7 3b dd>
// } // }
// v3.x 之后, decipher()同理
crypto.cipher('aes-128-cbc', '123456', {key})
// 等价于
crypto.cipheriv('aes-128-cbc', '123456', {key}, EMPTY_IV) // 其中 EMPTY_IV = crypto.scryptSync('', '', 16)
``` ```

View File

@ -1,6 +1,6 @@
{ {
"name": "crypto.js", "name": "crypto.js",
"version": "2.1.0", "version": "3.0.0",
"description": "原生crypto加密模块的二次封装,简化常用加密函数的使用", "description": "原生crypto加密模块的二次封装,简化常用加密函数的使用",
"keywords": [ "keywords": [
"md5", "md5",

View File

@ -11,10 +11,10 @@ const AUTH_MODE = [
'aes-192-ocb', 'aes-192-ocb',
'aes-256-ocb' 'aes-256-ocb'
] ]
// const VERSION = +process.versions.node const VERSION = +process.versions.node
// .split('.') .split('.')
// .slice(0, 2) .slice(0, 2)
// .join('.') .join('.')
// <Buffer d7 2c 87 d0 f0 77 c7 76 6f 29 85 df ab 30 e8 95> // <Buffer d7 2c 87 d0 f0 77 c7 76 6f 29 85 df ab 30 e8 95>
const EMPTY_KEY = crypto.scryptSync const EMPTY_KEY = crypto.scryptSync
@ -45,9 +45,9 @@ export default {
cipher(mode, data, key = EMPTY_KEY, inEncode, outEncode) { cipher(mode, data, key = EMPTY_KEY, inEncode, outEncode) {
// 10.0.0之后, createCipher方法不推荐使用了 // 10.0.0之后, createCipher方法不推荐使用了
// if (VERSION >= 10.5) { if (VERSION >= 10.5) {
// return this.cipheriv(mode, data, key, EMPTY_KEY, inEncode, outEncode) return this.cipheriv(mode, data, key, EMPTY_KEY, inEncode, outEncode)
// } }
let isBuffer = Buffer.isBuffer(data) let isBuffer = Buffer.isBuffer(data)
inEncode = isBuffer ? 'binary' : inEncode || 'utf8' inEncode = isBuffer ? 'binary' : inEncode || 'utf8'
outEncode = outEncode || 'base64' outEncode = outEncode || 'base64'
@ -64,9 +64,9 @@ export default {
decipher(mode, data, key = EMPTY_KEY, tag, inEncode, outEncode) { decipher(mode, data, key = EMPTY_KEY, tag, inEncode, outEncode) {
// 10.0.0之后, createCipher方法不推荐使用了 // 10.0.0之后, createCipher方法不推荐使用了
// if (VERSION >= 10.5) { if (VERSION >= 10.5) {
// return this.decipheriv(mode, data, key, EMPTY_KEY, tag, inEncode, outEncode) return this.decipheriv(mode, data, key, EMPTY_KEY, tag, inEncode, outEncode)
// } }
let isBuffer = Buffer.isBuffer(data) let isBuffer = Buffer.isBuffer(data)
inEncode = isBuffer ? 'binary' : inEncode || 'base64' inEncode = isBuffer ? 'binary' : inEncode || 'base64'