From 9f5580edfa01e20de3337241b64fa513b6b87fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Fri, 8 Mar 2019 19:04:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96base64=E8=A7=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 7 +++---- index.js | 16 +++++----------- package.json | 2 +- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/Readme.md b/Readme.md index c8b1c74..737c8f8 100644 --- a/Readme.md +++ b/Readme.md @@ -117,15 +117,14 @@ crypto.base64encode('hello world') //aGVsbG8gd29ybGQ= -### base64decode(str[, urlFriendly][, outEncode]) +### base64decode(str[, urlFriendly]) - str `` - urlFriendly `` 可选 -- outEncode `` 可选,默认ascii, 如果之前的编码的字符串带有中文等字符,请设置为utf8等 -> 与之对应的,便是这个base解码了。同样`urlFriendly`是指要解码的字符串之前是否采用了URL友好处理,默认否。 +> base64解码, 返回Buffer对象。同样`urlFriendly`是指要解码的字符串之前是否采用了URL友好处理,默认否。 ```javascript -crypto.base64decode('aGVsbG8gd29ybGQ=') //hello world +crypto.base64decode('aGVsbG8gd29ybGQ=')// .toString('utf-8') === hello world ``` diff --git a/index.js b/index.js index c6aeabf..7862c62 100644 --- a/index.js +++ b/index.js @@ -211,10 +211,11 @@ module.exports = { * @param {bool} urlFriendly [是否对URL友好,默认否,是则会把+转成-,/转成_] */ base64encode(str, urlFriendly) { + let buf if (!Buffer.isBuffer(str)) { - str = Buffer.from(str + '') + buf = Buffer.from(str + '') } - let encode = str.toString('base64') + let encode = buf.toString('base64') if (urlFriendly) { return encode .replace(/\+/g, '-') @@ -225,10 +226,9 @@ module.exports = { }, /** - * [base64decode base64解密] + * [base64decode base64解密, 返回Buffer对象] * @param {Str} str [要解密的字符串] * @param {bool} urlFriendly [之前是否对结果采用了URL友好处理] - * @param {Str/Buffer} encoding [编码,默认utf-8] */ base64decode(str, urlFriendly, encoding) { if (urlFriendly) { @@ -238,12 +238,6 @@ module.exports = { .replace(/[^A-Za-z0-9\+\/]/g, '') } - let buff = Buffer.from(str, 'base64') - - if (encoding === 'buffer') { - return buff - } - - return buff.toString(encoding || 'ascii') + return Buffer.from(str, 'base64') } } diff --git a/package.json b/package.json index def7048..9d7b3e7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crypto.js", - "version": "1.2.0", + "version": "1.2.1", "description": "原生crypto加密模块的二次封装,简化常用加密函数的使用", "keywords": ["md5", "sha1", "base64", "fivejs", "crypto"], "author": "yutent ",