Compare commits
No commits in common. "master" and "1.0.2" have entirely different histories.
|
@ -1,4 +1,3 @@
|
||||||
src/
|
src/
|
||||||
test/
|
|
||||||
build.js
|
build.js
|
||||||
.prettierrc.yaml
|
.prettierrc.yaml
|
12
Readme.md
12
Readme.md
|
@ -1,18 +1,18 @@
|
||||||
## crypto.js浏览器版本,提供常用的加密封装
|
## crypto.js浏览器版本,提供常用的加密封装
|
||||||
|
|
||||||
[English Readme](./Readme_EN.md)
|
|
||||||
|
|
||||||
感谢下面的开源库:
|
感谢以下2个优秀的开源库:
|
||||||
|
|
||||||
1. [spark-md5](https://github.com/satazor/js-spark-md5), 这个经本人测试, 确实是纯js版中性能最高的一个, 本仓库引入进来, 调整为ESM方式使用。
|
1. [spark-md5](https://github.com/satazor/js-spark-md5), 这个经本人测试, 确实是纯js版中性能最高的一个, 本仓库引入进来, 调整为ESM方式使用。
|
||||||
|
2. [base64](https://github.com/beatgammit/base64-js), 完整的base64库, 相比原生的 btoa()方法, 这个支持对中文进行编码。
|
||||||
|
|
||||||
|
|
||||||
以上这个库, 在index.js中有引入。对体积有要求的, 可以单独使用。
|
以上这2个库, 在index.js中有引入。对体积有要求的, 可以单独使用。
|
||||||
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// 多合一
|
import crypto, { md5, base64encode } from '//unpkg.com/crypto.web.js/dist/index.js'
|
||||||
import { sha1, hmac, md5, base64encode } from '//jscdn.ink/crypto.web.js/latest/index.js' // 大陆用户可使用此加速地址
|
import crypto, { md5, base64encode } from '//jscdn.ink/crypto.web.js/latest/index.js' // 大陆用户可使用此加速地址
|
||||||
|
|
||||||
import { md5, md5Sum } from '//jscdn.ink/crypto.web.js/latest/md5.js'
|
import { md5, md5Sum } from '//jscdn.ink/crypto.web.js/latest/md5.js'
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ import { base64encode, base64decode } from '//jscdn.ink/crypto.web.js/latest/bas
|
||||||
|
|
||||||
### APIs
|
### APIs
|
||||||
|
|
||||||
#### 1. md5(str`<String>|<Number>`)
|
#### 1. md5(str`<String>`)
|
||||||
> 常规的md5函数, 可对字符串(数字类型也可以), 进行加密。如果要对一个超级大的文本进行md5求值的话, 建议改成下面的 md5Sum()
|
> 常规的md5函数, 可对字符串(数字类型也可以), 进行加密。如果要对一个超级大的文本进行md5求值的话, 建议改成下面的 md5Sum()
|
||||||
|
|
||||||
|
|
||||||
|
|
79
Readme_EN.md
79
Readme_EN.md
|
@ -1,79 +0,0 @@
|
||||||
## crypto.js browser edition, which provides regular using
|
|
||||||
|
|
||||||
[中文 Readme](./Readme.md)
|
|
||||||
|
|
||||||
Thanks this library:
|
|
||||||
|
|
||||||
1. [spark-md5](https://github.com/satazor/js-spark-md5), After some testing, this is indeed the highest performing version in the pure JS version. It has been introduced into this warehouse and adjusted to be used in ESM mode.
|
|
||||||
|
|
||||||
|
|
||||||
This library is included in index.js. If you have specific requirements on size, you can use them separately.
|
|
||||||
|
|
||||||
|
|
||||||
```js
|
|
||||||
// all in one
|
|
||||||
import { sha1, hmac, md5, base64encode } from '//unpkg.com/crypto.web.js/dist/index.js'
|
|
||||||
|
|
||||||
// md5
|
|
||||||
import { md5, md5Sum } from '//unpkg.com/crypto.web.js/dist/md5.js'
|
|
||||||
|
|
||||||
// base64
|
|
||||||
import { base64encode, base64decode } from '//unpkg.com/crypto.web.js/dist/base64.js'
|
|
||||||
|
|
||||||
import { sha1, hmac } from '//unpkg.com/crypto.web.js/dist/crypto.js'
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
### APIs
|
|
||||||
|
|
||||||
#### 1. md5(str`<String>|<Number>`)
|
|
||||||
> regular md5 function, you can use to calculate a small string(<100MB).
|
|
||||||
|
|
||||||
|
|
||||||
#### 2. md5Sum(ab`<ArrayBuffer>`)
|
|
||||||
> you can use this to calculate a large data. Need an `ArrayBuffer` object which can get the value through `FileReader` , and the `Blob` object of the new browser also has an `arraybuffer` prototype method。
|
|
||||||
|
|
||||||
|
|
||||||
#### 3. base64encode(str`<String>`)
|
|
||||||
> Base64 encoding, supports Chinese.
|
|
||||||
|
|
||||||
|
|
||||||
#### 4. base64decode(str`<String>`)
|
|
||||||
> Base64 decoding.
|
|
||||||
|
|
||||||
|
|
||||||
#### 5. uuid()
|
|
||||||
> Generate a unique 24-bit random string. Only guarantees uniqueness on a single machine.
|
|
||||||
|
|
||||||
|
|
||||||
#### 6. ab2hex(ab`<ArrayBuffer>`)
|
|
||||||
> Convert an `arraybuffer` object to a hexadecimal string.
|
|
||||||
|
|
||||||
|
|
||||||
#### 7. ab2bin(ab`<ArrayBuffer>`)
|
|
||||||
> Convert an `arraybuffer` object to a Binary string.
|
|
||||||
|
|
||||||
|
|
||||||
#### 8. sha1(str`<String>`)
|
|
||||||
> Calculate the `sha1` value of the specified string.
|
|
||||||
>> Note: The return value of this method is not a string, but a `Promise` object.
|
|
||||||
|
|
||||||
|
|
||||||
#### 9. sha256(str`<String>`)
|
|
||||||
> Calculate the `sha256` value of the specified string.
|
|
||||||
>> Note: The return value of this method is not a string, but a `Promise` object.
|
|
||||||
|
|
||||||
|
|
||||||
#### 10. sha512(str`<String>`)
|
|
||||||
> Calculate the `sha512` value of the specified string.
|
|
||||||
>> Note: The return value of this method is not a string, but a `Promise` object.
|
|
||||||
|
|
||||||
|
|
||||||
#### 11. hash(algorithm`<String>`, data`<String>|<ArrayBuffer>|<Uint8Array>`)
|
|
||||||
> Hash algorithm, used to implement fuzzy processing of some important data, to achieve the purpose of hiding plaintext. The above sha1, sha256, etc., are actually based on this re-encapsulation result; algorithm, optional values are: SHA-1, SHA-256, SHA-384, SHA-512.
|
|
||||||
>> Note: The return value of this method is not a string, but a Promise object.
|
|
||||||
|
|
||||||
|
|
||||||
#### 12. hmac(algorithm`<String>`, data`<String>|<ArrayBuffer>|<Uint8Array>`, key`<String>|<Uint8Array>`, outEncode`<String>`)
|
|
||||||
> HMAC algorithm, which is a combination of a hash algorithm and a key to prevent the destruction of signature integrity. Compared with the above hash algorithm, it has an additional key parameter.
|
|
||||||
>> Note: The return value of this method is not a string, but a Promise object.
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "crypto.web.js",
|
"name": "crypto.web.js",
|
||||||
"version": "1.0.7",
|
"version": "1.0.2",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"files": [
|
"files": [
|
||||||
|
@ -24,7 +24,7 @@
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://git.wkit.fun/bytedo/crypto.web.js"
|
"url": "git+https://github.com/bytedo/crypto.web.js.git"
|
||||||
},
|
},
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
|
145
src/base64.js
145
src/base64.js
|
@ -1,28 +1,141 @@
|
||||||
import { encode, decode } from './helper.js'
|
import { str2uint, buf2str } from './helper.js'
|
||||||
|
|
||||||
// string 转 binary
|
var lookup = []
|
||||||
function str2bin(str) {
|
var revLookup = []
|
||||||
let bin = ''
|
var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
|
||||||
let u8 = encode(str) // 转成Uint8Array
|
|
||||||
for (let i = 0; i < u8.length; i++) {
|
var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
||||||
bin += String.fromCharCode(u8[i])
|
for (var i = 0, len = code.length; i < len; ++i) {
|
||||||
}
|
lookup[i] = code[i]
|
||||||
return bin
|
revLookup[code.charCodeAt(i)] = i
|
||||||
}
|
}
|
||||||
|
|
||||||
function bin2str(b) {
|
// Support decoding URL-safe base64 strings, as Node.js does.
|
||||||
let u8 = new Uint8Array(b.length)
|
// See: https://en.wikipedia.org/wiki/Base64#URL_applications
|
||||||
for (let i = 0; i < u8.length; i++) {
|
revLookup['-'.charCodeAt(0)] = 62
|
||||||
u8[i] = b[i].charCodeAt(0)
|
revLookup['_'.charCodeAt(0)] = 63
|
||||||
|
|
||||||
|
function getLens(b64) {
|
||||||
|
var len = b64.length
|
||||||
|
|
||||||
|
// 补足 =
|
||||||
|
while (len % 4 > 0) {
|
||||||
|
b64 += '='
|
||||||
|
len++
|
||||||
}
|
}
|
||||||
return decode(u8)
|
|
||||||
|
// Trim off extra bytes after placeholder bytes are found
|
||||||
|
// See: https://github.com/beatgammit/base64-js/issues/42
|
||||||
|
var validLen = b64.indexOf('=')
|
||||||
|
if (validLen === -1) validLen = len
|
||||||
|
|
||||||
|
var placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4)
|
||||||
|
|
||||||
|
return [validLen, placeHoldersLen]
|
||||||
|
}
|
||||||
|
|
||||||
|
// base64 is 4/3 + up to two characters of the original data
|
||||||
|
function byteLength(b64) {
|
||||||
|
var lens = getLens(b64)
|
||||||
|
var validLen = lens[0]
|
||||||
|
var placeHoldersLen = lens[1]
|
||||||
|
return ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen
|
||||||
|
}
|
||||||
|
|
||||||
|
function _byteLength(b64, validLen, placeHoldersLen) {
|
||||||
|
return ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen
|
||||||
|
}
|
||||||
|
|
||||||
|
function toByteArray(b64) {
|
||||||
|
var tmp
|
||||||
|
var lens = getLens(b64)
|
||||||
|
var validLen = lens[0]
|
||||||
|
var placeHoldersLen = lens[1]
|
||||||
|
|
||||||
|
var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen))
|
||||||
|
|
||||||
|
var curByte = 0
|
||||||
|
|
||||||
|
// if there are placeholders, only get up to the last complete 4 chars
|
||||||
|
var len = placeHoldersLen > 0 ? validLen - 4 : validLen
|
||||||
|
|
||||||
|
var i
|
||||||
|
for (i = 0; i < len; i += 4) {
|
||||||
|
tmp =
|
||||||
|
(revLookup[b64.charCodeAt(i)] << 18) |
|
||||||
|
(revLookup[b64.charCodeAt(i + 1)] << 12) |
|
||||||
|
(revLookup[b64.charCodeAt(i + 2)] << 6) |
|
||||||
|
revLookup[b64.charCodeAt(i + 3)]
|
||||||
|
arr[curByte++] = (tmp >> 16) & 0xff
|
||||||
|
arr[curByte++] = (tmp >> 8) & 0xff
|
||||||
|
arr[curByte++] = tmp & 0xff
|
||||||
|
}
|
||||||
|
|
||||||
|
if (placeHoldersLen === 2) {
|
||||||
|
tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4)
|
||||||
|
arr[curByte++] = tmp & 0xff
|
||||||
|
}
|
||||||
|
|
||||||
|
if (placeHoldersLen === 1) {
|
||||||
|
tmp =
|
||||||
|
(revLookup[b64.charCodeAt(i)] << 10) |
|
||||||
|
(revLookup[b64.charCodeAt(i + 1)] << 4) |
|
||||||
|
(revLookup[b64.charCodeAt(i + 2)] >> 2)
|
||||||
|
arr[curByte++] = (tmp >> 8) & 0xff
|
||||||
|
arr[curByte++] = tmp & 0xff
|
||||||
|
}
|
||||||
|
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
|
function tripletToBase64(num) {
|
||||||
|
return (
|
||||||
|
lookup[(num >> 18) & 0x3f] +
|
||||||
|
lookup[(num >> 12) & 0x3f] +
|
||||||
|
lookup[(num >> 6) & 0x3f] +
|
||||||
|
lookup[num & 0x3f]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function encodeChunk(uint8, start, end) {
|
||||||
|
var tmp
|
||||||
|
var output = []
|
||||||
|
for (var i = start; i < end; i += 3) {
|
||||||
|
tmp = ((uint8[i] << 16) & 0xff0000) + ((uint8[i + 1] << 8) & 0xff00) + (uint8[i + 2] & 0xff)
|
||||||
|
output.push(tripletToBase64(tmp))
|
||||||
|
}
|
||||||
|
return output.join('')
|
||||||
|
}
|
||||||
|
|
||||||
|
function fromByteArray(uint8) {
|
||||||
|
var tmp
|
||||||
|
var len = uint8.length
|
||||||
|
var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
|
||||||
|
var parts = []
|
||||||
|
var maxChunkLength = 16383 // must be multiple of 3
|
||||||
|
|
||||||
|
// go through the array every three bytes, we'll deal with trailing stuff later
|
||||||
|
for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
|
||||||
|
parts.push(encodeChunk(uint8, i, i + maxChunkLength > len2 ? len2 : i + maxChunkLength))
|
||||||
|
}
|
||||||
|
|
||||||
|
// pad the end with zeros, but make sure to not forget the extra bytes
|
||||||
|
if (extraBytes === 1) {
|
||||||
|
tmp = uint8[len - 1]
|
||||||
|
parts.push(lookup[tmp >> 2] + lookup[(tmp << 4) & 0x3f] + '==')
|
||||||
|
} else if (extraBytes === 2) {
|
||||||
|
tmp = (uint8[len - 2] << 8) + uint8[len - 1]
|
||||||
|
parts.push(lookup[tmp >> 10] + lookup[(tmp >> 4) & 0x3f] + lookup[(tmp << 2) & 0x3f] + '=')
|
||||||
|
}
|
||||||
|
|
||||||
|
return parts.join('')
|
||||||
}
|
}
|
||||||
|
|
||||||
// 支持对中文的base64编码
|
// 支持对中文的base64编码
|
||||||
export function base64encode(str) {
|
export function base64encode(str) {
|
||||||
return btoa(str2bin(str))
|
return fromByteArray(str2uint(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function base64decode(str) {
|
export function base64decode(str) {
|
||||||
return bin2str(atob(base64))
|
return buf2str(toByteArray(str))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { encode, getType } from './helper.js'
|
import { str2uint, getType } from './helper.js'
|
||||||
|
|
||||||
const subtle = window.crypto.subtle
|
const subtle = window.crypto.subtle
|
||||||
|
|
||||||
|
@ -20,11 +20,11 @@ function any2uint(data) {
|
||||||
case 'Number':
|
case 'Number':
|
||||||
case 'String':
|
case 'String':
|
||||||
case 'Boolean':
|
case 'Boolean':
|
||||||
val = encode(val)
|
val = str2uint(val)
|
||||||
break
|
break
|
||||||
|
|
||||||
default:
|
default:
|
||||||
val = encode(val.toString())
|
val = val.toString()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return val
|
return val
|
||||||
|
@ -85,10 +85,10 @@ export function sha512(str) {
|
||||||
return hash('SHA-512', str)
|
return hash('SHA-512', str)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function hmac(mode, data = '', key = '', encoding) {
|
export function hmac(mode, data = '', key = '', encode) {
|
||||||
if (key) {
|
if (key) {
|
||||||
if (typeof key === 'string') {
|
if (typeof key === 'string') {
|
||||||
key = encode(key)
|
key = str2uint(key)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
key = new Uint8Array(16)
|
key = new Uint8Array(16)
|
||||||
|
@ -99,7 +99,7 @@ export function hmac(mode, data = '', key = '', encoding) {
|
||||||
.then(ab => {
|
.then(ab => {
|
||||||
var output = ab
|
var output = ab
|
||||||
|
|
||||||
switch (encoding) {
|
switch (encode) {
|
||||||
case 'binary':
|
case 'binary':
|
||||||
output = ab2bin(ab)
|
output = ab2bin(ab)
|
||||||
break
|
break
|
||||||
|
|
|
@ -4,14 +4,11 @@ const decoder = new TextDecoder()
|
||||||
/**
|
/**
|
||||||
* String 转 Uint8Array
|
* String 转 Uint8Array
|
||||||
*/
|
*/
|
||||||
export function encode(txt) {
|
export function str2uint(txt) {
|
||||||
return encoder.encode(txt)
|
return encoder.encode(txt)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export function buf2str(buf) {
|
||||||
* Uint8Array 转 String
|
|
||||||
*/
|
|
||||||
export function decode(buf) {
|
|
||||||
return decoder.decode(buf)
|
return decoder.decode(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue