优化md5/sha1/sha256,支持对buffer对象求值

master 1.1.5
宇天 2018-10-08 12:22:48 +08:00
parent 15ebc01f93
commit 5af78262e7
2 changed files with 16 additions and 7 deletions

View File

@ -118,11 +118,14 @@ module.exports = {
* @param {Str} encode [hex/base64]
*/
md5(str, encode) {
if (typeof str !== 'string' && typeof str !== 'number') {
if (typeof str === 'number') {
str += ''
}
if (typeof str !== 'string' && !Buffer.isBuffer(str)) {
return str
}
return this.hash('md5', str + '', encode)
return this.hash('md5', str, encode)
},
/**
@ -144,11 +147,14 @@ module.exports = {
* @param {Str} encode [hex/base64]
*/
sha1(str, encode) {
if (typeof str !== 'string' && typeof str !== 'number') {
if (typeof str === 'number') {
str += ''
}
if (typeof str !== 'string' && !Buffer.isBuffer(str)) {
return str
}
return this.hash('sha1', str + '', encode)
return this.hash('sha1', str, encode)
},
/**
@ -170,11 +176,14 @@ module.exports = {
* @param {Str} encoding [hex/base64]
*/
sha256(str, encoding) {
if (typeof str !== 'string' && typeof str !== 'number') {
if (typeof str === 'number') {
str += ''
}
if (typeof str !== 'string' && !Buffer.isBuffer(str)) {
return str
}
return this.hash('sha256', str + '', encoding)
return this.hash('sha256', str, encoding)
},
/**

View File

@ -1,6 +1,6 @@
{
"name": "crypto.js",
"version": "1.1.4",
"version": "1.1.5",
"description": "原生crypto加密模块的二次封装,简化常用加密函数的使用",
"keywords": [
"md5",