改成esbuild打包;公钥加密增加版本判断, 10.0之后, 不再使用createCipher

master
yutent 2022-01-07 19:45:59 +08:00
parent f0240827db
commit 7bbec9afbf
7 changed files with 84 additions and 296 deletions

12
.npmignore Normal file
View File

@ -0,0 +1,12 @@
.Spotlight-V100
.Trashes
.DS_Store
.AppleDouble
.LSOverride
._*
.idea
.vscode
src/
build.js

24
build.js Normal file
View File

@ -0,0 +1,24 @@
/**
* {build}
* @author yutent<yutent.io@gmail.com>
* @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'
})

183
index.js
View File

@ -1,183 +0,0 @@
/**
* 加密类 md5/sha1/base64
* @author yutent<yutent.io@gmail.com>
* @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

View File

@ -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
}
}

View File

@ -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"
}
}

View File

@ -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)