优化hash(), hmac()的传值

master 1.0.2
yutent 2023-06-25 11:30:36 +08:00
parent 46da9f445f
commit 8eb2292a8c
4 changed files with 63 additions and 34 deletions

View File

@ -12,11 +12,11 @@
```js ```js
import crypto, { md5, base64encode } from '//unpkg.com/crypto.web.js/dist/index.js' import crypto, { md5, base64encode } from '//unpkg.com/crypto.web.js/dist/index.js'
import crypto, { md5, base64encode } from '//jscdn.ink/crypto.web.js/1.0.0/index.js' // 大陆用户可使用此加速地址 import crypto, { md5, base64encode } from '//jscdn.ink/crypto.web.js/latest/index.js' // 大陆用户可使用此加速地址
import { md5, md5Sum } from '//jscdn.ink/crypto.web.js/1.0.0/md5.js' import { md5, md5Sum } from '//jscdn.ink/crypto.web.js/latest/md5.js'
import { base64encode, base64decode } from '//jscdn.ink/crypto.web.js/1.0.0/base64.js' import { base64encode, base64decode } from '//jscdn.ink/crypto.web.js/latest/base64.js'
``` ```
@ -24,7 +24,7 @@ import { base64encode, base64decode } from '//jscdn.ink/crypto.web.js/1.0.0/base
### APIs ### APIs
#### 1. md5(str`<String>`) #### 1. md5(str`<String>`)
> 常规的md5函数, 可对字符串(数字类型也可以), 进行加密。 > 常规的md5函数, 可对字符串(数字类型也可以), 进行加密。如果要对一个超级大的文本进行md5求值的话, 建议改成下面的 md5Sum()
#### 2. md5Sum(ab`<ArrayBuffer>`) #### 2. md5Sum(ab`<ArrayBuffer>`)
@ -48,7 +48,7 @@ import { base64encode, base64decode } from '//jscdn.ink/crypto.web.js/1.0.0/base
#### 7. ab2bin(ab`<ArrayBuffer>`) #### 7. ab2bin(ab`<ArrayBuffer>`)
> 将一个`arraybuffer`对象, 转为 `Binary` 符串。 > 将一个`arraybuffer`对象, 转为 `Binary`符串。
#### 8. sha1(str`<String>`) #### 8. sha1(str`<String>`)
@ -66,11 +66,11 @@ import { base64encode, base64decode } from '//jscdn.ink/crypto.web.js/1.0.0/base
>> 注意: 该方法返回的不是字符串, 而是一个Promise对象。 >> 注意: 该方法返回的不是字符串, 而是一个Promise对象。
#### 11. hash(algorithm`<String>`, data`<String>|<ArrayBuffer>`) #### 11. hash(algorithm`<String>`, data`<String>|<ArrayBuffer>|<Uint8Array>`)
> 散列算法(也称为哈希算法),用来实现一些重要数据的模糊处理,以达到隐藏明文的目的。 上面的sha1、sha256等其实就是基于这个再次封装的结果; algorithm可选值有: SHA-1, SHA-256, SHA-384, SHA-512。 > 散列算法(也称为哈希算法),用来实现一些重要数据的模糊处理,以达到隐藏明文的目的。 上面的sha1、sha256等其实就是基于这个再次封装的结果; algorithm可选值有: SHA-1, SHA-256, SHA-384, SHA-512。
>> 注意: 该方法返回的不是字符串, 而是一个Promise对象。 >> 注意: 该方法返回的不是字符串, 而是一个Promise对象。
#### 12. hmac(algorithm`<String>`, data`<String>|<ArrayBuffer>`, key`<String>`, outEncode`<String>`) #### 12. hmac(algorithm`<String>`, data`<String>|<ArrayBuffer>|<Uint8Array>`, key`<String>|<Uint8Array>`, outEncode`<String>`)
> HMAC算法是在散列算法的基础上与一个密钥结合在一起以阻止对签名完整性的破坏。 与上面的散列算法相比多了一个密钥的参数key。 > HMAC算法是在散列算法的基础上与一个密钥结合在一起以阻止对签名完整性的破坏。 与上面的散列算法相比多了一个密钥的参数key。
>> 注意: 该方法返回的不是字符串, 而是一个Promise对象。 >> 注意: 该方法返回的不是字符串, 而是一个Promise对象。

View File

@ -1,7 +1,11 @@
{ {
"name": "crypto.web.js", "name": "crypto.web.js",
"version": "1.0.1", "version": "1.0.2",
"type": "module", "type": "module",
"main": "dist/index.js",
"files": [
"dist/*"
],
"description": "crypto.js浏览器版本,提供常用的加密封装", "description": "crypto.js浏览器版本,提供常用的加密封装",
"keywords": [ "keywords": [
"md5", "md5",

View File

@ -1,10 +1,35 @@
import { str2uint } from './helper.js' import { str2uint, getType } from './helper.js'
const subtle = window.crypto.subtle const subtle = window.crypto.subtle
var __stamp__ = 0 var __stamp__ = 0
var __inc__ = 0 //用于生成保证唯一的uuid种子 var __inc__ = 0 //用于生成保证唯一的uuid种子
// 将其他数据类型转为uint8
function any2uint(data) {
let val = data ?? ''
let type = getType(val)
switch (type) {
case 'Uint8Array':
break
case 'ArrayBuffer':
val = new Uint8Array(val)
break
case 'Number':
case 'String':
case 'Boolean':
val = str2uint(val)
break
default:
val = val.toString()
break
}
return val
}
export function uuid(pipe = '-') { export function uuid(pipe = '-') {
var rand = Math.random().toString(16).slice(2) + Math.random().toString(16).slice(2) var rand = Math.random().toString(16).slice(2) + Math.random().toString(16).slice(2)
var now = (~~(Date.now() / 1000)).toString(16) var now = (~~(Date.now() / 1000)).toString(16)
@ -44,11 +69,8 @@ export function ab2bin(buf) {
/** /**
* @return Promise * @return Promise
*/ */
export function hash(type, str) { export function hash(type, data) {
if (typeof str === 'string') { return subtle.digest(type, any2uint(data)).then(buf => ab2hex(buf))
str = str2uint(str)
}
return subtle.digest(type, str).then(buf => ab2hex(buf))
} }
export function sha1(str) { export function sha1(str) {
@ -63,7 +85,7 @@ export function sha512(str) {
return hash('SHA-512', str) return hash('SHA-512', str)
} }
export function hmac(mode, str, key = '', encode) { export function hmac(mode, data = '', key = '', encode) {
if (key) { if (key) {
if (typeof key === 'string') { if (typeof key === 'string') {
key = str2uint(key) key = str2uint(key)
@ -73,26 +95,25 @@ export function hmac(mode, str, key = '', encode) {
} }
return subtle return subtle
.importKey('raw', key, { name: 'HMAC', hash: { name: mode } }, true, ['sign', 'verify']) .importKey('raw', key, { name: 'HMAC', hash: { name: mode } }, true, ['sign', 'verify'])
.then(cKey => { .then(cKey => subtle.sign('HMAC', cKey, any2uint(data)))
return subtle.sign('HMAC', cKey, str2uint(str)).then(ab => { .then(ab => {
var output = ab var output = ab
switch (encode) { switch (encode) {
case 'binary': case 'binary':
output = ab2bin(ab) output = ab2bin(ab)
break break
case 'hex': case 'hex':
output = ab2hex(ab) output = ab2hex(ab)
break break
case 'base64': case 'base64':
output = window.btoa(ab2bin(ab)) output = window.btoa(ab2bin(ab))
break break
case 'buffer': case 'buffer':
output = new Uint8Array(ab) output = new Uint8Array(ab)
break break
} }
return output return output
})
}) })
} }

View File

@ -11,3 +11,7 @@ export function str2uint(txt) {
export function buf2str(buf) { export function buf2str(buf) {
return decoder.decode(buf) return decoder.decode(buf)
} }
export function getType(val) {
return Object.prototype.toString.call(val).slice(8, -1)
}