From 95f5b85bb6ca3758b5bc3ec5d7c7455cf487a87a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Wed, 16 Sep 2020 20:01:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0esm=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.es7 | 175 ++++++++++++++++++++ index.js | 421 +++++++++++++++++++------------------------------ lib/helper.es7 | 88 +++++++++++ lib/helper.js | 88 +++++++++++ package.json | 19 ++- 5 files changed, 532 insertions(+), 259 deletions(-) create mode 100644 index.es7 create mode 100644 lib/helper.es7 create mode 100644 lib/helper.js diff --git a/index.es7 b/index.es7 new file mode 100644 index 0000000..d34b403 --- /dev/null +++ b/index.es7 @@ -0,0 +1,175 @@ +/** + * 加密类 md5/sha1/base64 + * @author yutent + * @date 2020/09/16 18:11:51 + */ + +import fs from 'fs' +import Helper from './lib/helper.es7' + +var __stamp__ = '' +var __inc__ = 1024 + +/** + * [base64encode base64编码] + * @param {Str/Num/Buffer} str [要编码的字符串] + * @param {bool} urlFriendly [是否对URL友好,默认否,是则会把+转成-,/转成_] + */ +export function base64encode(str, urlFriendly) { + var buf, str64 + + if (!Buffer.isBuffer(str)) { + 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友好处理] + */ +export function base64decode(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 [是否强制返回纯数字] + */ +export function rand(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 +export function uuid(pipe = '') { + var rand = Helper.origin.randomBytes(8).toString('hex') + var now = ~~(Date.now() / 1000).toString(16) + var inc + + if (__stamp__ === now) { + __inc__++ + } else { + __stamp__ = now + __inc__ = 1024 + } + inc = __inc__.toString(16).padStart(4, '0') + + return ( + __stamp__ + pipe + inc + pipe + rand.slice(0, 4) + pipe + rand.slice(-8) + ) +} + +/** + * [md5 md5加密] + * @param {Str/Num} str [要加密的字符串] + * @param {Str} encode [hex/base64] + */ +export function md5(str, encode) { + if (typeof str === 'number') { + str += '' + } + if (typeof str !== 'string' && !Buffer.isBuffer(str)) { + return str + } + + return Helper.hash('md5', str, encode) +} + +/** + * [md5Sign 获取文件的md5签名] + * @param {Str} file [文件路径] + */ +export function md5Sign(file) { + if (!fs.existsSync(file)) { + return null + } + + var buf = fs.readFileSync(file) + return Helper.hash('md5', buf) +} + +/** + * [sha1 sha1加密] + * @param {Str/Num} str [要加密的字符串] + * @param {Str} encode [hex/base64] + */ +export function sha1(str, encode) { + if (typeof str === 'number') { + str += '' + } + if (typeof str !== 'string' && !Buffer.isBuffer(str)) { + return str + } + + return Helper.hash('sha1', str, encode) +} + +/** + * [sha1Sign 获取文件的sha1签名] + * @param {Str} file [文件路径] + */ +export function sha1Sign(file) { + if (!fs.existsSync(file)) { + return null + } + + var buf = fs.readFileSync(file) + return Helper.hash('sha1', buf) +} + +/** + * [sha256 sha256加密] + * @param {Str/Num} str [要加密的字符串] + * @param {Str} encoding [hex/base64] + */ +export function sha256(str, encoding) { + if (typeof str === 'number') { + str += '' + } + if (typeof str !== 'string' && !Buffer.isBuffer(str)) { + return str + } + + return Helper.hash('sha256', str, encoding) +} + +/** + * [sha256Sign 获取文件的sha256签名] + * @param {Str} file [文件路径] + */ +export function sha256Sign(file) { + if (!fs.existsSync(file)) { + return null + } + + var buf = fs.readFileSync(file) + return Helper.hash('sha256', buf) +} + +export default Helper diff --git a/index.js b/index.js index 4ebd775..7785fb2 100644 --- a/index.js +++ b/index.js @@ -1,264 +1,175 @@ /** * 加密类 md5/sha1/base64 - * @authors yutent (yutent@doui.cc) - * @date 2015-09-10 13:56:18 + * @author yutent + * @date 2020/09/16 18:11:51 */ -'use strict' +const fs = require('fs') +const Helper = require('./lib/helper.js') -const CRYPTO = require('crypto') -const FS = require('fs') -const GCM_MODE = ['aes-128-gcm', 'aes-192-gcm', 'aes-256-gcm'] +var __stamp__ = '' +var __inc__ = 1024 -module.exports = { - origin: CRYPTO, - __stamp__: '', - __inc__: 0, +/** + * [base64encode base64编码] + * @param {Str/Num/Buffer} str [要编码的字符串] + * @param {bool} urlFriendly [是否对URL友好,默认否,是则会把+转成-,/转成_] + */ +exports.base64encode = function(str, urlFriendly) { + var buf, str64 - 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 - }, - - /** - * [rand 生成指定长度的随机字符串] - * @param {[type]} len [要得到的字符串长度] - * @param {[type]} forceNum [是否强制返回纯数字] - */ - rand(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 - uuid(pipe = '-') { - let rand = CRYPTO.randomBytes(8).toString('hex') - let now = (Date.now() / 1000).toString(16).slice(0, 8) - - if (this.__stamp__ === now) { - this.__inc__++ - } else { - this.__stamp__ = now - this.__inc__ = 0 - } - rand = this.__inc__.toString(16) + rand - - return ( - this.__stamp__ + - pipe + - rand.slice(0, 4) + - pipe + - rand.slice(4, 8) + - pipe + - rand.slice(8, 16) - ) - }, - - /** - * [md5 md5加密] - * @param {Str/Num} str [要加密的字符串] - * @param {Str} encode [hex/base64] - */ - md5(str, encode) { - if (typeof str === 'number') { - str += '' - } - if (typeof str !== 'string' && !Buffer.isBuffer(str)) { - return str - } - - return this.hash('md5', str, encode) - }, - - /** - * [md5Sign 获取文件的md5签名] - * @param {Str} file [文件路径] - */ - md5Sign(file) { - if (!FS.existsSync(file)) { - return null - } - - let fileStream = FS.readFileSync(file) - return this.hash('md5', fileStream) - }, - - /** - * [sha1 sha1加密] - * @param {Str/Num} str [要加密的字符串] - * @param {Str} encode [hex/base64] - */ - sha1(str, encode) { - if (typeof str === 'number') { - str += '' - } - if (typeof str !== 'string' && !Buffer.isBuffer(str)) { - return str - } - - return this.hash('sha1', str, encode) - }, - - /** - * [sha1Sign 获取文件的sha1签名] - * @param {Str} file [文件路径] - */ - sha1Sign(file) { - if (!FS.existsSync(file)) { - return null - } - - let fileStream = FS.readFileSync(file) - return this.hash('sha1', fileStream) - }, - - /** - * [sha256 sha256加密] - * @param {Str/Num} str [要加密的字符串] - * @param {Str} encoding [hex/base64] - */ - sha256(str, encoding) { - if (typeof str === 'number') { - str += '' - } - if (typeof str !== 'string' && !Buffer.isBuffer(str)) { - return str - } - - return this.hash('sha256', str, encoding) - }, - - /** - * [sha256Sign 获取文件的sha256签名] - * @param {Str} file [文件路径] - */ - sha256Sign(file) { - if (!FS.existsSync(file)) { - return null - } - - let fileStream = FS.readFileSync(file) - return this.hash('sha256', fileStream) - }, - - /** - * [base64encode base64加密] - * @param {Str/Num/Buffer} str [要加密的字符串] - * @param {bool} urlFriendly [是否对URL友好,默认否,是则会把+转成-,/转成_] - */ - base64encode(str, urlFriendly) { - let buf - if (!Buffer.isBuffer(str)) { - buf = Buffer.from(str + '') - } - let encode = buf.toString('base64') - if (urlFriendly) { - return encode - .replace(/\+/g, '-') - .replace(/\//g, '_') - .replace(/=/g, '') - } - return encode - }, - - /** - * [base64decode base64解密, 返回Buffer对象] - * @param {Str} str [要解密的字符串] - * @param {bool} urlFriendly [之前是否对结果采用了URL友好处理] - */ - base64decode(str, urlFriendly, encoding) { - if (urlFriendly) { - str = str - .replace(/-/g, '+') - .replace(/_/g, '/') - .replace(/[^A-Za-z0-9\+\/]/g, '') - } - - return Buffer.from(str, 'base64') + if (!Buffer.isBuffer(str)) { + 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友好处理] + */ +exports.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 [是否强制返回纯数字] + */ +exports.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 +exports.uuid = function(pipe = '') { + var rand = Helper.origin.randomBytes(8).toString('hex') + var now = ~~(Date.now() / 1000).toString(16) + var inc + + if (__stamp__ === now) { + __inc__++ + } else { + __stamp__ = now + __inc__ = 1024 + } + inc = __inc__.toString(16).padStart(4, '0') + + return ( + __stamp__ + pipe + inc + pipe + rand.slice(0, 4) + pipe + rand.slice(-8) + ) +} + +/** + * [md5 md5加密] + * @param {Str/Num} str [要加密的字符串] + * @param {Str} encode [hex/base64] + */ +exports.md5 = function(str, encode) { + if (typeof str === 'number') { + str += '' + } + if (typeof str !== 'string' && !Buffer.isBuffer(str)) { + return str + } + + return Helper.hash('md5', str, encode) +} + +/** + * [md5Sign 获取文件的md5签名] + * @param {Str} file [文件路径] + */ +exports.md5Sign = function(file) { + if (!fs.existsSync(file)) { + return null + } + + var buf = fs.readFileSync(file) + return Helper.hash('md5', buf) +} + +/** + * [sha1 sha1加密] + * @param {Str/Num} str [要加密的字符串] + * @param {Str} encode [hex/base64] + */ +exports.sha1 = function(str, encode) { + if (typeof str === 'number') { + str += '' + } + if (typeof str !== 'string' && !Buffer.isBuffer(str)) { + return str + } + + return Helper.hash('sha1', str, encode) +} + +/** + * [sha1Sign 获取文件的sha1签名] + * @param {Str} file [文件路径] + */ +exports.sha1Sign = function(file) { + if (!fs.existsSync(file)) { + return null + } + + var buf = fs.readFileSync(file) + return Helper.hash('sha1', buf) +} + +/** + * [sha256 sha256加密] + * @param {Str/Num} str [要加密的字符串] + * @param {Str} encoding [hex/base64] + */ +exports.sha256 = function(str, encoding) { + if (typeof str === 'number') { + str += '' + } + if (typeof str !== 'string' && !Buffer.isBuffer(str)) { + return str + } + + return Helper.hash('sha256', str, encoding) +} + +/** + * [sha256Sign 获取文件的sha256签名] + * @param {Str} file [文件路径] + */ +exports.sha256Sign = function(file) { + if (!fs.existsSync(file)) { + return null + } + + var buf = fs.readFileSync(file) + return Helper.hash('sha256', buf) +} + +module.exports = Helper diff --git a/lib/helper.es7 b/lib/helper.es7 new file mode 100644 index 0000000..e8b34d6 --- /dev/null +++ b/lib/helper.es7 @@ -0,0 +1,88 @@ +import crypto from 'crypto' + +const GCM_MODE = ['aes-128-gcm', 'aes-192-gcm', 'aes-256-gcm'] + +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, 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/lib/helper.js b/lib/helper.js new file mode 100644 index 0000000..438e5f5 --- /dev/null +++ b/lib/helper.js @@ -0,0 +1,88 @@ +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 50bcb33..2736aa1 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,25 @@ { "name": "crypto.js", - "version": "1.3.1", + "version": "2.0.0", "description": "原生crypto加密模块的二次封装,简化常用加密函数的使用", - "keywords": ["md5", "sha1", "base64", "fivejs", "crypto"], - "author": "yutent ", + "keywords": [ + "md5", + "sha1", + "sha256", + "hmac", + "base64", + "fivejs", + "crypto", + "crypto-js", + "crypto.js" + ], + "author": "yutent ", "repository": { "type": "git", "url": "https://github.com/yutent/crypto.js.git" }, "dependencies": {}, "devDependencies": {}, - "main": "index.js" + "main": "index.js", + "module": "index.es.js" }