parent
3cc8c53a72
commit
5281159c21
|
@ -2,6 +2,10 @@
|
|||
> 本模块是对原生的`crypto`模块二次封装的,在使用上更加简单方便。
|
||||
|
||||
## 更新日志
|
||||
+ v3.1.0
|
||||
- 优化`uuid(),rand()`
|
||||
- 优化库引用方式
|
||||
|
||||
+ v3.0.0
|
||||
- Node.js 10.0.0之后不再推荐使用`crypto.createCipher()`, 所以 本库的`cipher()`方法, 内部改为调用`cipheriv()` (Node.js大于10.5.0时, 旧版本的不变)
|
||||
|
||||
|
@ -25,7 +29,7 @@ npm install crypto.js
|
|||
```js
|
||||
// 1、 传统的 commonJS引入, 所有的方法都在上面
|
||||
var {
|
||||
default: crypto,
|
||||
origin, // 原生crypto对象
|
||||
uuid,
|
||||
rand,
|
||||
md5,
|
||||
|
@ -41,7 +45,8 @@ var {
|
|||
|
||||
// 2、 全新的 ESM 方式
|
||||
import crypto from 'crypto.js'
|
||||
import {
|
||||
import crypto, {
|
||||
origin,
|
||||
uuid,
|
||||
rand,
|
||||
md5,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "crypto.js",
|
||||
"version": "3.0.0",
|
||||
"version": "3.1.0",
|
||||
"description": "原生crypto加密模块的二次封装,简化常用加密函数的使用",
|
||||
"keywords": [
|
||||
"md5",
|
||||
|
@ -21,7 +21,9 @@
|
|||
"dependencies": {},
|
||||
"devDependencies": {},
|
||||
"main": "dist/index.js",
|
||||
"files": ["dist/*"],
|
||||
"files": [
|
||||
"dist/*"
|
||||
],
|
||||
"scripts": {
|
||||
"start": "node ./build.js"
|
||||
},
|
||||
|
|
234
src/helper.mjs
234
src/helper.mjs
|
@ -11,102 +11,154 @@ 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('.')
|
||||
|
||||
// <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(' ')
|
||||
'0xd7 0x2c 0x87 0xd0 0xf0 0x77 0xc7 0x76 0x6f 0x29 0x85 0xdf 0xab 0x30 0xe8 0x95'.split(
|
||||
' '
|
||||
)
|
||||
)
|
||||
//
|
||||
if (!crypto.randomUUID) {
|
||||
crypto.randomUUID = function () {
|
||||
let str = crypto.randomBytes(16).toString('hex')
|
||||
return (
|
||||
str.slice(0, 8) +
|
||||
'-' +
|
||||
str.slice(8, 12) +
|
||||
'-' +
|
||||
str.slice(12, 16) +
|
||||
'-' +
|
||||
str.slice(16, 20) +
|
||||
'-' +
|
||||
str.slice(-12)
|
||||
)
|
||||
|
||||
export default {
|
||||
origin: crypto,
|
||||
|
||||
hash(mode, data, outEncode) {
|
||||
let sum = crypto.createHash(mode)
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
|
||||
sum.update(data, isBuffer ? 'binary' : 'utf8')
|
||||
return sum.digest(outEncode || 'hex')
|
||||
},
|
||||
|
||||
hmac(mode, data, key, outEncode) {
|
||||
key = key || ''
|
||||
let sum = crypto.createHmac(mode, key)
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
|
||||
sum.update(data, isBuffer ? 'binary' : 'utf8')
|
||||
return sum.digest(outEncode || 'hex')
|
||||
},
|
||||
|
||||
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)
|
||||
}
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
inEncode = isBuffer ? 'binary' : inEncode || 'utf8'
|
||||
outEncode = outEncode || 'base64'
|
||||
|
||||
let cipher = crypto.createCipher(mode, key)
|
||||
let enStr = cipher.update(data, inEncode, outEncode)
|
||||
enStr += cipher.final(outEncode)
|
||||
if (AUTH_MODE.indexOf(mode) > -1) {
|
||||
let authTag = cipher.getAuthTag()
|
||||
return { enStr, authTag }
|
||||
}
|
||||
return enStr
|
||||
},
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
inEncode = isBuffer ? 'binary' : inEncode || 'base64'
|
||||
outEncode = outEncode || 'utf8'
|
||||
|
||||
let cd = crypto.createDecipher(mode, key)
|
||||
if (AUTH_MODE.indexOf(mode) > -1) {
|
||||
cd.setAuthTag(tag)
|
||||
}
|
||||
let deStr = cd.update(data, inEncode, outEncode)
|
||||
deStr += cd.final(outEncode)
|
||||
return deStr
|
||||
},
|
||||
|
||||
cipheriv(mode, data, key = EMPTY_KEY, iv = EMPTY_KEY, inEncode, outEncode) {
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
inEncode = isBuffer ? 'binary' : inEncode || 'utf8'
|
||||
outEncode = outEncode || 'base64'
|
||||
|
||||
let cciv = crypto.createCipheriv(mode, key, iv)
|
||||
let enStr = cciv.update(data, inEncode, outEncode)
|
||||
enStr += cciv.final(outEncode)
|
||||
if (AUTH_MODE.indexOf(mode) > -1) {
|
||||
let authTag = cciv.getAuthTag()
|
||||
return { enStr, authTag }
|
||||
}
|
||||
return enStr
|
||||
},
|
||||
|
||||
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'
|
||||
|
||||
let dcpiv = crypto.createDecipheriv(mode, key, iv)
|
||||
if (AUTH_MODE.indexOf(mode) > -1) {
|
||||
dcpiv.setAuthTag(tag)
|
||||
}
|
||||
let deStr = dcpiv.update(data, inEncode, outEncode)
|
||||
deStr += dcpiv.final(outEncode)
|
||||
return deStr
|
||||
}
|
||||
}
|
||||
if (!crypto.randomInt) {
|
||||
crypto.randomInt = function (max) {
|
||||
if (max === void 0) {
|
||||
throw new TypeError(
|
||||
'[ERR_INVALID_ARG_TYPE]: The "max" argument must be a safe integer. Received undefined'
|
||||
)
|
||||
}
|
||||
return ~~(Math.random() * max)
|
||||
}
|
||||
}
|
||||
|
||||
export const origin = crypto
|
||||
|
||||
export const hash = function (mode, data, outEncode) {
|
||||
let sum = crypto.createHash(mode)
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
|
||||
sum.update(data, isBuffer ? 'binary' : 'utf8')
|
||||
return sum.digest(outEncode || 'hex')
|
||||
}
|
||||
|
||||
export const hmac = function (mode, data, key, outEncode) {
|
||||
key = key || ''
|
||||
let sum = crypto.createHmac(mode, key)
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
|
||||
sum.update(data, isBuffer ? 'binary' : 'utf8')
|
||||
return sum.digest(outEncode || 'hex')
|
||||
}
|
||||
|
||||
export const cipher = function (
|
||||
mode,
|
||||
data,
|
||||
key = EMPTY_KEY,
|
||||
inEncode,
|
||||
outEncode
|
||||
) {
|
||||
// 10.0.0之后, createCipher方法不推荐使用了
|
||||
if (VERSION >= 10.5) {
|
||||
return cipheriv(mode, data, key, EMPTY_KEY, inEncode, outEncode)
|
||||
}
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
inEncode = isBuffer ? 'binary' : inEncode || 'utf8'
|
||||
outEncode = outEncode || 'base64'
|
||||
|
||||
let cipher = crypto.createCipher(mode, key)
|
||||
let enStr = cipher.update(data, inEncode, outEncode)
|
||||
enStr += cipher.final(outEncode)
|
||||
if (AUTH_MODE.indexOf(mode) > -1) {
|
||||
let authTag = cipher.getAuthTag()
|
||||
return { enStr, authTag }
|
||||
}
|
||||
return enStr
|
||||
}
|
||||
|
||||
export const decipher = function (
|
||||
mode,
|
||||
data,
|
||||
key = EMPTY_KEY,
|
||||
tag,
|
||||
inEncode,
|
||||
outEncode
|
||||
) {
|
||||
// 10.0.0之后, createCipher方法不推荐使用了
|
||||
if (VERSION >= 10.5) {
|
||||
return decipheriv(mode, data, key, EMPTY_KEY, tag, inEncode, outEncode)
|
||||
}
|
||||
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
inEncode = isBuffer ? 'binary' : inEncode || 'base64'
|
||||
outEncode = outEncode || 'utf8'
|
||||
|
||||
let cd = crypto.createDecipher(mode, key)
|
||||
if (AUTH_MODE.indexOf(mode) > -1) {
|
||||
cd.setAuthTag(tag)
|
||||
}
|
||||
let deStr = cd.update(data, inEncode, outEncode)
|
||||
deStr += cd.final(outEncode)
|
||||
return deStr
|
||||
}
|
||||
|
||||
export const cipheriv = function (
|
||||
mode,
|
||||
data,
|
||||
key = EMPTY_KEY,
|
||||
iv = EMPTY_KEY,
|
||||
inEncode,
|
||||
outEncode
|
||||
) {
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
inEncode = isBuffer ? 'binary' : inEncode || 'utf8'
|
||||
outEncode = outEncode || 'base64'
|
||||
|
||||
let cciv = crypto.createCipheriv(mode, key, iv)
|
||||
let enStr = cciv.update(data, inEncode, outEncode)
|
||||
enStr += cciv.final(outEncode)
|
||||
if (AUTH_MODE.indexOf(mode) > -1) {
|
||||
let authTag = cciv.getAuthTag()
|
||||
return { enStr, authTag }
|
||||
}
|
||||
return enStr
|
||||
}
|
||||
|
||||
export const decipheriv = function (
|
||||
mode,
|
||||
data,
|
||||
key = EMPTY_KEY,
|
||||
iv = EMPTY_KEY,
|
||||
tag,
|
||||
inEncode,
|
||||
outEncode
|
||||
) {
|
||||
let isBuffer = Buffer.isBuffer(data)
|
||||
inEncode = isBuffer ? 'binary' : inEncode || 'base64'
|
||||
outEncode = outEncode || 'utf8'
|
||||
|
||||
let dcpiv = crypto.createDecipheriv(mode, key, iv)
|
||||
if (AUTH_MODE.indexOf(mode) > -1) {
|
||||
dcpiv.setAuthTag(tag)
|
||||
}
|
||||
let deStr = dcpiv.update(data, inEncode, outEncode)
|
||||
deStr += dcpiv.final(outEncode)
|
||||
return deStr
|
||||
}
|
||||
|
|
|
@ -6,19 +6,28 @@
|
|||
|
||||
import os from 'os'
|
||||
import fs from 'fs'
|
||||
import Helper from './helper.mjs'
|
||||
import {
|
||||
origin,
|
||||
hash,
|
||||
hmac,
|
||||
cipher,
|
||||
decipher,
|
||||
cipheriv,
|
||||
decipheriv
|
||||
} from './helper.mjs'
|
||||
|
||||
const MAC = (function(ns) {
|
||||
const MAC = (function (ns) {
|
||||
for (let k in ns) {
|
||||
let _ = ns[k].pop()
|
||||
if (_.mac !== '00:00:00:00:00:00') {
|
||||
return _.mac
|
||||
if (k === 'lo') {
|
||||
continue
|
||||
}
|
||||
let _ = ns[k].pop()
|
||||
return _.mac.replace(/:/g, '').slice(-4)
|
||||
}
|
||||
return process.pid.toString(16) + process.ppid.toString(16)
|
||||
return Math.random().toString(16).slice(-4)
|
||||
})(os.networkInterfaces())
|
||||
|
||||
var __inc__ = 1024
|
||||
var __inc__ = 1
|
||||
|
||||
/**
|
||||
* [base64encode base64编码]
|
||||
|
@ -37,10 +46,7 @@ export function base64encode(str, urlFriendly) {
|
|||
str64 = buf.toString('base64')
|
||||
|
||||
if (urlFriendly) {
|
||||
return str64
|
||||
.replace(/\+/g, '-')
|
||||
.replace(/\//g, '_')
|
||||
.replace(/=/g, '')
|
||||
return str64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
|
||||
}
|
||||
return str64
|
||||
}
|
||||
|
@ -73,25 +79,23 @@ export function rand(len, forceNum) {
|
|||
let max = str.length
|
||||
let tmp = ''
|
||||
for (let i = 0; i < len; i++) {
|
||||
let r = (Math.random() * max) >> 0
|
||||
tmp += str[r]
|
||||
tmp += str[origin.randomInt(max)]
|
||||
}
|
||||
return tmp
|
||||
}
|
||||
|
||||
// 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID
|
||||
export function uuid(pipe = '-') {
|
||||
var rand = Helper.origin.randomBytes(8).toString('hex')
|
||||
var str = origin.randomUUID()
|
||||
var now = (~~(Date.now() / 1000)).toString(16)
|
||||
var str
|
||||
|
||||
__inc__++
|
||||
if (__inc__ > 65535) {
|
||||
__inc__ = 1024
|
||||
__inc__ = 1
|
||||
}
|
||||
str = md5(MAC + rand + __inc__)
|
||||
str += __inc__.toString(16)
|
||||
|
||||
return now + pipe + str.slice(0, 4) + pipe + str.slice(4, 8) + pipe + str.slice(-8)
|
||||
return now + pipe + MAC + pipe + str.slice(0, 4) + pipe + str.slice(-8)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -105,7 +109,7 @@ export function md5(str, encode) {
|
|||
}
|
||||
|
||||
if (typeof str === 'string' || Buffer.isBuffer(str)) {
|
||||
return Helper.hash('md5', str, encode)
|
||||
return hash('md5', str, encode)
|
||||
}
|
||||
|
||||
return str
|
||||
|
@ -118,7 +122,7 @@ export function md5(str, encode) {
|
|||
export function md5Sign(file) {
|
||||
if (fs.accessSync(file, fs.constants.R_OK)) {
|
||||
var buf = fs.readFileSync(file)
|
||||
return Helper.hash('md5', buf)
|
||||
return hash('md5', buf)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
@ -133,7 +137,7 @@ export function sha1(str, encode) {
|
|||
str += ''
|
||||
}
|
||||
if (typeof str === 'string' || Buffer.isBuffer(str)) {
|
||||
return Helper.hash('sha1', str, encode)
|
||||
return hash('sha1', str, encode)
|
||||
}
|
||||
|
||||
return str
|
||||
|
@ -146,7 +150,7 @@ export function sha1(str, encode) {
|
|||
export function sha1Sign(file) {
|
||||
if (fs.accessSync(file, fs.constants.R_OK)) {
|
||||
var buf = fs.readFileSync(file)
|
||||
return Helper.hash('sha1', buf)
|
||||
return hash('sha1', buf)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
@ -161,7 +165,7 @@ export function sha256(str, encoding) {
|
|||
str += ''
|
||||
}
|
||||
if (typeof str === 'string' || Buffer.isBuffer(str)) {
|
||||
return Helper.hash('sha256', str, encoding)
|
||||
return hash('sha256', str, encoding)
|
||||
}
|
||||
|
||||
return str
|
||||
|
@ -174,9 +178,29 @@ export function sha256(str, encoding) {
|
|||
export function sha256Sign(file) {
|
||||
if (fs.accessSync(file, fs.constants.R_OK)) {
|
||||
var buf = fs.readFileSync(file)
|
||||
return Helper.hash('sha256', buf)
|
||||
return hash('sha256', buf)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export default Helper
|
||||
export { origin, hash, hmac, cipher, decipher, cipheriv, decipheriv }
|
||||
|
||||
export default {
|
||||
base64encode,
|
||||
base64decode,
|
||||
rand,
|
||||
uuid,
|
||||
md5,
|
||||
md5Sign,
|
||||
sha1,
|
||||
sha1Sign,
|
||||
sha256,
|
||||
sha256Sign,
|
||||
origin,
|
||||
hash,
|
||||
hmac,
|
||||
cipher,
|
||||
decipher,
|
||||
cipheriv,
|
||||
decipheriv
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue