From 5281159c2119d3dd95d4f15f3c557c43958d51a4 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 13 Feb 2023 17:19:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BC=95=E5=85=A5=E6=96=B9?= =?UTF-8?q?=E5=BC=8F,=E5=8F=8Auuid=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 9 +- package.json | 6 +- src/helper.mjs | 234 ++++++++++++++++++++++++++++++------------------- src/index.mjs | 74 ++++++++++------ 4 files changed, 203 insertions(+), 120 deletions(-) diff --git a/Readme.md b/Readme.md index 53adccb..a9bc07b 100644 --- a/Readme.md +++ b/Readme.md @@ -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, diff --git a/package.json b/package.json index 5c17cb8..173b49e 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/helper.mjs b/src/helper.mjs index 070196d..3904b13 100644 --- a/src/helper.mjs +++ b/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('.') // 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 +} diff --git a/src/index.mjs b/src/index.mjs index 1213374..3a0343a 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -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 +}