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",