From 7bbec9afbfd36815c0a94a48f999ea87fa91f7ee Mon Sep 17 00:00:00 2001 From: yutent Date: Fri, 7 Jan 2022 19:45:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E6=88=90esbuild=E6=89=93=E5=8C=85;?= =?UTF-8?q?=E5=85=AC=E9=92=A5=E5=8A=A0=E5=AF=86=E5=A2=9E=E5=8A=A0=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=88=A4=E6=96=AD,=2010.0=E4=B9=8B=E5=90=8E,=20?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E4=BD=BF=E7=94=A8createCipher?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .npmignore | 12 +++ build.js | 24 +++++ index.js | 183 ------------------------------------ lib/helper.js | 88 ----------------- package.json | 12 ++- index.mjs => src/index.mjs | 0 {lib => src/lib}/helper.mjs | 61 +++++++----- 7 files changed, 84 insertions(+), 296 deletions(-) create mode 100644 .npmignore create mode 100644 build.js delete mode 100644 index.js delete mode 100644 lib/helper.js rename index.mjs => src/index.mjs (100%) rename {lib => src/lib}/helper.mjs (55%) diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..980a7d2 --- /dev/null +++ b/.npmignore @@ -0,0 +1,12 @@ + +.Spotlight-V100 +.Trashes +.DS_Store +.AppleDouble +.LSOverride +._* +.idea +.vscode + +src/ +build.js \ No newline at end of file diff --git a/build.js b/build.js new file mode 100644 index 0000000..1487369 --- /dev/null +++ b/build.js @@ -0,0 +1,24 @@ +/** + * {build} + * @author yutent + * @date 2021/08/09 11:59:41 + */ + +const Es = require('esbuild') + +Es.build({ + entryPoints: ['src/index.mjs'], + outfile: 'dist/index.mjs', + platform: 'node', + bundle: true, + minify: true, + format: 'esm' +}) +Es.build({ + entryPoints: ['src/index.mjs'], + outfile: 'dist/index.js', + platform: 'node', + bundle: true, + minify: true, + format: 'cjs' +}) diff --git a/index.js b/index.js deleted file mode 100644 index 6b01694..0000000 --- a/index.js +++ /dev/null @@ -1,183 +0,0 @@ -/** - * 加密类 md5/sha1/base64 - * @author yutent - * @date 2020/09/16 18:11:51 - */ - -const os = require('os') -const fs = require('fs') -const Helper = require('./lib/helper.js') - -const MAC = (function(ns) { - for (let k in ns) { - let _ = ns[k].pop() - if (_.mac !== '00:00:00:00:00:00') { - return _.mac - } - } - return process.pid.toString(16) + process.ppid.toString(16) -})(os.networkInterfaces()) - -var __inc__ = 1024 - -/** - * [base64encode base64编码] - * @param {Str/Num/Buffer} str [要编码的字符串] - * @param {bool} urlFriendly [是否对URL友好,默认否,是则会把+转成-,/转成_] - */ -Helper.base64encode = function(str, urlFriendly) { - var buf, str64 - - if (Buffer.isBuffer(str)) { - buf = str - } else { - buf = Buffer.from(str + '') - } - - str64 = buf.toString('base64') - - if (urlFriendly) { - return str64 - .replace(/\+/g, '-') - .replace(/\//g, '_') - .replace(/=/g, '') - } - return str64 -} - -/** - * [base64decode base64解码, 返回Buffer对象] - * @param {Str} str [要解码的字符串] - * @param {bool} urlFriendly [之前是否对结果采用了URL友好处理] - */ -Helper.base64decode = function(str, urlFriendly) { - if (urlFriendly) { - str = str - .replace(/-/g, '+') - .replace(/_/g, '/') - .replace(/[^A-Za-z0-9\+\/]/g, '') - } - return Buffer.from(str, 'base64') -} - -/** - * [rand 生成指定长度的随机字符串] - * @param {[type]} len [要得到的字符串长度] - * @param {[type]} forceNum [是否强制返回纯数字] - */ -Helper.rand = function(len, forceNum) { - let str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789' - if (forceNum) { - str = '0123456789' - } - let max = str.length - let tmp = '' - for (let i = 0; i < len; i++) { - let r = (Math.random() * max) >> 0 - tmp += str[r] - } - return tmp -} - -// 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID -Helper.uuid = function(pipe = '-') { - var rand = Helper.origin.randomBytes(8).toString('hex') - var now = (~~(Date.now() / 1000)).toString(16) - var str - - __inc__++ - if (__inc__ > 65535) { - __inc__ = 1024 - } - str = md5(MAC + rand + __inc__) - - return ( - now + pipe + str.slice(0, 4) + pipe + str.slice(4, 8) + pipe + str.slice(-8) - ) -} - -/** - * [md5 md5加密] - * @param {Str/Num} str [要加密的字符串] - * @param {Str} encode [hex/base64] - */ -Helper.md5 = function(str, encode) { - if (typeof str === 'number') { - str += '' - } - if (typeof str === 'string' || Buffer.isBuffer(str)) { - return Helper.hash('md5', str, encode) - } - - return str -} - -/** - * [md5Sign 获取文件的md5签名] - * @param {Str} file [文件路径] - */ -Helper.md5Sign = function(file) { - if (fs.accessSync(file, fs.constants.R_OK)) { - var buf = fs.readFileSync(file) - return Helper.hash('md5', buf) - } - return null -} - -/** - * [sha1 sha1加密] - * @param {Str/Num} str [要加密的字符串] - * @param {Str} encode [hex/base64] - */ -Helper.sha1 = function(str, encode) { - if (typeof str === 'number') { - str += '' - } - if (typeof str === 'string' || Buffer.isBuffer(str)) { - return Helper.hash('sha1', str, encode) - } - - return str -} - -/** - * [sha1Sign 获取文件的sha1签名] - * @param {Str} file [文件路径] - */ -Helper.sha1Sign = function(file) { - if (fs.accessSync(file, fs.constants.R_OK)) { - var buf = fs.readFileSync(file) - return Helper.hash('sha1', buf) - } - return null -} - -/** - * [sha256 sha256加密] - * @param {Str/Num} str [要加密的字符串] - * @param {Str} encoding [hex/base64] - */ -Helper.sha256 = function(str, encoding) { - if (typeof str === 'number') { - str += '' - } - if (typeof str === 'string' || Buffer.isBuffer(str)) { - return Helper.hash('sha256', str, encoding) - } - - return str -} - -/** - * [sha256Sign 获取文件的sha256签名] - * @param {Str} file [文件路径] - */ -Helper.sha256Sign = function(file) { - if (fs.accessSync(file, fs.constants.R_OK)) { - var buf = fs.readFileSync(file) - return Helper.hash('sha256', buf) - } - return null -} - -module.exports = Helper diff --git a/lib/helper.js b/lib/helper.js deleted file mode 100644 index 438e5f5..0000000 --- a/lib/helper.js +++ /dev/null @@ -1,88 +0,0 @@ -const crypto = require('crypto') - -const GCM_MODE = ['aes-128-gcm', 'aes-192-gcm', 'aes-256-gcm'] - -module.exports = { - 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, inEncode, outEncode) { - key = key || '' - let isBuffer = Buffer.isBuffer(data) - inEncode = isBuffer ? 'binary' : inEncode || 'utf8' - outEncode = outEncode || 'base64' - - let cc = crypto.createCipher(mode, key) - let enStr = cc.update(data, inEncode, outEncode) - enStr += cc.final(outEncode) - if (GCM_MODE.indexOf(mode) > -1) { - let authTag = cc.getAuthTag() - return { enStr: enStr, authTag: authTag } - } - return enStr - }, - - decipher(mode, data, key, tag, inEncode, outEncode) { - key = key || '' - let isBuffer = Buffer.isBuffer(data) - inEncode = isBuffer ? 'binary' : inEncode || 'base64' - outEncode = outEncode || 'utf8' - - let cd = crypto.createDecipher(mode, key) - if (GCM_MODE.indexOf(mode) > -1) { - cd.setAuthTag(tag) - } - let deStr = cd.update(data, inEncode, outEncode) - deStr += cd.final(outEncode) - return deStr - }, - - cipheriv(mode, data, key, iv, inEncode, outEncode) { - key = key || '0000000000000000' - iv = iv || '' - 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 (GCM_MODE.indexOf(mode) > -1) { - let authTag = cciv.getAuthTag() - return { enStr: enStr, authTag: authTag } - } - return enStr - }, - - decipheriv(mode, data, key, iv, tag, inEncode, outEncode) { - key = key || '0000000000000000' - iv = iv || '' - let isBuffer = Buffer.isBuffer(data) - inEncode = isBuffer ? 'binary' : inEncode || 'base64' - outEncode = outEncode || 'utf8' - - let dcpiv = crypto.createDecipheriv(mode, key, iv) - if (GCM_MODE.indexOf(mode) > -1) { - dcpiv.setAuthTag(tag) - } - let deStr = dcpiv.update(data, inEncode, outEncode) - deStr += dcpiv.final(outEncode) - return deStr - } -} diff --git a/package.json b/package.json index 03615a2..8589bd5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crypto.js", - "version": "2.0.5", + "version": "2.1.0", "description": "原生crypto加密模块的二次封装,简化常用加密函数的使用", "keywords": [ "md5", @@ -20,9 +20,13 @@ }, "dependencies": {}, "devDependencies": {}, - "main": "index.js", + "main": "dist/index.js", + "files": ["dist/*"], + "scripts": { + "start": "node ./build.js" + }, "exports": { - "require": "./index.js", - "import": "./index.mjs" + "require": "./dist/index.js", + "import": "./dist/index.mjs" } } diff --git a/index.mjs b/src/index.mjs similarity index 100% rename from index.mjs rename to src/index.mjs diff --git a/lib/helper.mjs b/src/lib/helper.mjs similarity index 55% rename from lib/helper.mjs rename to src/lib/helper.mjs index e8b34d6..6cf6c71 100644 --- a/lib/helper.mjs +++ b/src/lib/helper.mjs @@ -1,6 +1,22 @@ import crypto from 'crypto' -const GCM_MODE = ['aes-128-gcm', 'aes-192-gcm', 'aes-256-gcm'] +const AUTH_MODE = [ + 'aes-128-gcm', + 'aes-192-gcm', + 'aes-256-gcm', + 'aes-128-ccm', + 'aes-192-ccm', + 'aes-256-ccm', + 'aes-128-ocb', + 'aes-192-ocb', + 'aes-256-ocb' +] +const VERSION = +process.versions.node + .split('.') + .slice(0, 2) + .join('.') + +const EMPTY_KEY = crypto.scryptSync('', '', 16) export default { origin: crypto, @@ -22,30 +38,37 @@ export default { return sum.digest(outEncode || 'hex') }, - cipher(mode, data, key, inEncode, outEncode) { - key = key || '' + 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 cc = crypto.createCipher(mode, key) - let enStr = cc.update(data, inEncode, outEncode) - enStr += cc.final(outEncode) - if (GCM_MODE.indexOf(mode) > -1) { - let authTag = cc.getAuthTag() - return { enStr: enStr, authTag: authTag } + 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, tag, inEncode, outEncode) { - key = key || '' + 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 (GCM_MODE.indexOf(mode) > -1) { + if (AUTH_MODE.indexOf(mode) > -1) { cd.setAuthTag(tag) } let deStr = cd.update(data, inEncode, outEncode) @@ -53,9 +76,7 @@ export default { return deStr }, - cipheriv(mode, data, key, iv, inEncode, outEncode) { - key = key || '0000000000000000' - iv = iv || '' + cipheriv(mode, data, key = EMPTY_KEY, iv = null, inEncode, outEncode) { let isBuffer = Buffer.isBuffer(data) inEncode = isBuffer ? 'binary' : inEncode || 'utf8' outEncode = outEncode || 'base64' @@ -63,22 +84,20 @@ export default { let cciv = crypto.createCipheriv(mode, key, iv) let enStr = cciv.update(data, inEncode, outEncode) enStr += cciv.final(outEncode) - if (GCM_MODE.indexOf(mode) > -1) { + if (AUTH_MODE.indexOf(mode) > -1) { let authTag = cciv.getAuthTag() - return { enStr: enStr, authTag: authTag } + return { enStr, authTag } } return enStr }, - decipheriv(mode, data, key, iv, tag, inEncode, outEncode) { - key = key || '0000000000000000' - iv = iv || '' + decipheriv(mode, data, key = EMPTY_KEY, iv = null, tag, inEncode, outEncode) { let isBuffer = Buffer.isBuffer(data) inEncode = isBuffer ? 'binary' : inEncode || 'base64' outEncode = outEncode || 'utf8' let dcpiv = crypto.createDecipheriv(mode, key, iv) - if (GCM_MODE.indexOf(mode) > -1) { + if (AUTH_MODE.indexOf(mode) > -1) { dcpiv.setAuthTag(tag) } let deStr = dcpiv.update(data, inEncode, outEncode)