parent
46da9f445f
commit
8eb2292a8c
14
Readme.md
14
Readme.md
|
@ -12,11 +12,11 @@
|
|||
|
||||
```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
|
||||
|
||||
#### 1. md5(str`<String>`)
|
||||
> 常规的md5函数, 可对字符串(数字类型也可以), 进行加密。
|
||||
> 常规的md5函数, 可对字符串(数字类型也可以), 进行加密。如果要对一个超级大的文本进行md5求值的话, 建议改成下面的 md5Sum()
|
||||
|
||||
|
||||
#### 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>`)
|
||||
> 将一个`arraybuffer`对象, 转为 `Binary` 符串。
|
||||
> 将一个`arraybuffer`对象, 转为 `Binary`字符串。
|
||||
|
||||
|
||||
#### 8. sha1(str`<String>`)
|
||||
|
@ -66,11 +66,11 @@ import { base64encode, base64decode } from '//jscdn.ink/crypto.web.js/1.0.0/base
|
|||
>> 注意: 该方法返回的不是字符串, 而是一个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。
|
||||
>> 注意: 该方法返回的不是字符串, 而是一个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。
|
||||
>> 注意: 该方法返回的不是字符串, 而是一个Promise对象。
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
{
|
||||
"name": "crypto.web.js",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"files": [
|
||||
"dist/*"
|
||||
],
|
||||
"description": "crypto.js浏览器版本,提供常用的加密封装",
|
||||
"keywords": [
|
||||
"md5",
|
||||
|
|
|
@ -1,10 +1,35 @@
|
|||
import { str2uint } from './helper.js'
|
||||
import { str2uint, getType } from './helper.js'
|
||||
|
||||
const subtle = window.crypto.subtle
|
||||
|
||||
var __stamp__ = 0
|
||||
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 = '-') {
|
||||
var rand = Math.random().toString(16).slice(2) + Math.random().toString(16).slice(2)
|
||||
var now = (~~(Date.now() / 1000)).toString(16)
|
||||
|
@ -44,11 +69,8 @@ export function ab2bin(buf) {
|
|||
/**
|
||||
* @return Promise
|
||||
*/
|
||||
export function hash(type, str) {
|
||||
if (typeof str === 'string') {
|
||||
str = str2uint(str)
|
||||
}
|
||||
return subtle.digest(type, str).then(buf => ab2hex(buf))
|
||||
export function hash(type, data) {
|
||||
return subtle.digest(type, any2uint(data)).then(buf => ab2hex(buf))
|
||||
}
|
||||
|
||||
export function sha1(str) {
|
||||
|
@ -63,7 +85,7 @@ export function sha512(str) {
|
|||
return hash('SHA-512', str)
|
||||
}
|
||||
|
||||
export function hmac(mode, str, key = '', encode) {
|
||||
export function hmac(mode, data = '', key = '', encode) {
|
||||
if (key) {
|
||||
if (typeof key === 'string') {
|
||||
key = str2uint(key)
|
||||
|
@ -73,26 +95,25 @@ export function hmac(mode, str, key = '', encode) {
|
|||
}
|
||||
return subtle
|
||||
.importKey('raw', key, { name: 'HMAC', hash: { name: mode } }, true, ['sign', 'verify'])
|
||||
.then(cKey => {
|
||||
return subtle.sign('HMAC', cKey, str2uint(str)).then(ab => {
|
||||
var output = ab
|
||||
.then(cKey => subtle.sign('HMAC', cKey, any2uint(data)))
|
||||
.then(ab => {
|
||||
var output = ab
|
||||
|
||||
switch (encode) {
|
||||
case 'binary':
|
||||
output = ab2bin(ab)
|
||||
break
|
||||
case 'hex':
|
||||
output = ab2hex(ab)
|
||||
break
|
||||
case 'base64':
|
||||
output = window.btoa(ab2bin(ab))
|
||||
break
|
||||
case 'buffer':
|
||||
output = new Uint8Array(ab)
|
||||
break
|
||||
}
|
||||
switch (encode) {
|
||||
case 'binary':
|
||||
output = ab2bin(ab)
|
||||
break
|
||||
case 'hex':
|
||||
output = ab2hex(ab)
|
||||
break
|
||||
case 'base64':
|
||||
output = window.btoa(ab2bin(ab))
|
||||
break
|
||||
case 'buffer':
|
||||
output = new Uint8Array(ab)
|
||||
break
|
||||
}
|
||||
|
||||
return output
|
||||
})
|
||||
return output
|
||||
})
|
||||
}
|
||||
|
|
|
@ -11,3 +11,7 @@ export function str2uint(txt) {
|
|||
export function buf2str(buf) {
|
||||
return decoder.decode(buf)
|
||||
}
|
||||
|
||||
export function getType(val) {
|
||||
return Object.prototype.toString.call(val).slice(8, -1)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue