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