优化base64解码

master 1.2.1
宇天 2019-03-08 19:04:42 +08:00
parent 1f1fe657f8
commit 9f5580edfa
3 changed files with 9 additions and 16 deletions

View File

@ -117,15 +117,14 @@ crypto.base64encode('hello world') //aGVsbG8gd29ybGQ=
### base64decode(str[, urlFriendly][, outEncode])
### base64decode(str[, urlFriendly])
- str `<String>`
- urlFriendly `<Boolean>` 可选
- outEncode `<String>` 可选默认ascii, 如果之前的编码的字符串带有中文等字符,请设置为utf8等
> 与之对应的便是这个base解码了。同样`urlFriendly`是指要解码的字符串之前是否采用了URL友好处理默认否。
> base64解码, 返回Buffer对象。同样`urlFriendly`是指要解码的字符串之前是否采用了URL友好处理默认否。
```javascript
crypto.base64decode('aGVsbG8gd29ybGQ=') //hello world
crypto.base64decode('aGVsbG8gd29ybGQ=')// .toString('utf-8') === hello world
```

View File

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

View File

@ -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 <yutent@doui.cc>",