From c21a72a9a264d6dfba65deaf9d60bb6199aa8941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Thu, 24 Sep 2020 18:02:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbase64=E4=BC=A0buffer?= =?UTF-8?q?=E7=9A=84bug;=E4=BC=98=E5=8C=96=E5=90=84=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E7=B1=BB=E5=9E=8B=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 50 +++++++++++++++++++++++++------------------------- index.mjs | 51 ++++++++++++++++++++++++++------------------------- package.json | 2 +- 3 files changed, 52 insertions(+), 51 deletions(-) diff --git a/index.js b/index.js index 6832b20..7fa142b 100644 --- a/index.js +++ b/index.js @@ -18,9 +18,12 @@ var __inc__ = 1024 Helper.base64encode = function(str, urlFriendly) { var buf, str64 - if (!Buffer.isBuffer(str)) { + if (Buffer.isBuffer(str)) { + buf = str + } else { buf = Buffer.from(str + '') } + str64 = buf.toString('base64') if (urlFriendly) { @@ -94,11 +97,11 @@ Helper.md5 = function(str, encode) { if (typeof str === 'number') { str += '' } - if (typeof str !== 'string' && !Buffer.isBuffer(str)) { - return str + if (typeof str === 'string' || Buffer.isBuffer(str)) { + return Helper.hash('md5', str, encode) } - return Helper.hash('md5', str, encode) + return str } /** @@ -106,12 +109,11 @@ Helper.md5 = function(str, encode) { * @param {Str} file [文件路径] */ Helper.md5Sign = function(file) { - if (!fs.existsSync(file)) { - return null + if (fs.accessSync(file, fs.constants.R_OK)) { + var buf = fs.readFileSync(file) + return Helper.hash('md5', buf) } - - var buf = fs.readFileSync(file) - return Helper.hash('md5', buf) + return null } /** @@ -123,11 +125,11 @@ Helper.sha1 = function(str, encode) { if (typeof str === 'number') { str += '' } - if (typeof str !== 'string' && !Buffer.isBuffer(str)) { - return str + if (typeof str === 'string' || Buffer.isBuffer(str)) { + return Helper.hash('sha1', str, encode) } - return Helper.hash('sha1', str, encode) + return str } /** @@ -135,12 +137,11 @@ Helper.sha1 = function(str, encode) { * @param {Str} file [文件路径] */ Helper.sha1Sign = function(file) { - if (!fs.existsSync(file)) { - return null + if (fs.accessSync(file, fs.constants.R_OK)) { + var buf = fs.readFileSync(file) + return Helper.hash('sha1', buf) } - - var buf = fs.readFileSync(file) - return Helper.hash('sha1', buf) + return null } /** @@ -152,11 +153,11 @@ Helper.sha256 = function(str, encoding) { if (typeof str === 'number') { str += '' } - if (typeof str !== 'string' && !Buffer.isBuffer(str)) { - return str + if (typeof str === 'string' || Buffer.isBuffer(str)) { + return Helper.hash('sha256', str, encoding) } - return Helper.hash('sha256', str, encoding) + return str } /** @@ -164,12 +165,11 @@ Helper.sha256 = function(str, encoding) { * @param {Str} file [文件路径] */ Helper.sha256Sign = function(file) { - if (!fs.existsSync(file)) { - return null + if (fs.accessSync(file, fs.constants.R_OK)) { + var buf = fs.readFileSync(file) + return Helper.hash('sha256', buf) } - - var buf = fs.readFileSync(file) - return Helper.hash('sha256', buf) + return null } module.exports = Helper diff --git a/index.mjs b/index.mjs index 53c5e74..e593580 100644 --- a/index.mjs +++ b/index.mjs @@ -18,9 +18,12 @@ var __inc__ = 1024 export function base64encode(str, urlFriendly) { var buf, str64 - if (!Buffer.isBuffer(str)) { + if (Buffer.isBuffer(str)) { + buf = str + } else { buf = Buffer.from(str + '') } + str64 = buf.toString('base64') if (urlFriendly) { @@ -94,11 +97,12 @@ export function md5(str, encode) { if (typeof str === 'number') { str += '' } - if (typeof str !== 'string' && !Buffer.isBuffer(str)) { - return str + + if (typeof str === 'string' || Buffer.isBuffer(str)) { + return Helper.hash('md5', str, encode) } - return Helper.hash('md5', str, encode) + return str } /** @@ -106,12 +110,11 @@ export function md5(str, encode) { * @param {Str} file [文件路径] */ export function md5Sign(file) { - if (!fs.existsSync(file)) { - return null + if (fs.accessSync(file, fs.constants.R_OK)) { + var buf = fs.readFileSync(file) + return Helper.hash('md5', buf) } - - var buf = fs.readFileSync(file) - return Helper.hash('md5', buf) + return null } /** @@ -123,11 +126,11 @@ export function sha1(str, encode) { if (typeof str === 'number') { str += '' } - if (typeof str !== 'string' && !Buffer.isBuffer(str)) { - return str + if (typeof str === 'string' || Buffer.isBuffer(str)) { + return Helper.hash('sha1', str, encode) } - return Helper.hash('sha1', str, encode) + return str } /** @@ -135,12 +138,11 @@ export function sha1(str, encode) { * @param {Str} file [文件路径] */ export function sha1Sign(file) { - if (!fs.existsSync(file)) { - return null + if (fs.accessSync(file, fs.constants.R_OK)) { + var buf = fs.readFileSync(file) + return Helper.hash('sha1', buf) } - - var buf = fs.readFileSync(file) - return Helper.hash('sha1', buf) + return null } /** @@ -152,11 +154,11 @@ export function sha256(str, encoding) { if (typeof str === 'number') { str += '' } - if (typeof str !== 'string' && !Buffer.isBuffer(str)) { - return str + if (typeof str === 'string' || Buffer.isBuffer(str)) { + return Helper.hash('sha256', str, encoding) } - return Helper.hash('sha256', str, encoding) + return str } /** @@ -164,12 +166,11 @@ export function sha256(str, encoding) { * @param {Str} file [文件路径] */ export function sha256Sign(file) { - if (!fs.existsSync(file)) { - return null + if (fs.accessSync(file, fs.constants.R_OK)) { + var buf = fs.readFileSync(file) + return Helper.hash('sha256', buf) } - - var buf = fs.readFileSync(file) - return Helper.hash('sha256', buf) + return null } export default Helper diff --git a/package.json b/package.json index 244981d..1141a86 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crypto.js", - "version": "2.0.1", + "version": "2.0.2", "description": "原生crypto加密模块的二次封装,简化常用加密函数的使用", "keywords": [ "md5",