Compare commits
No commits in common. "master" and "3.2.0" have entirely different histories.
20
Readme.md
20
Readme.md
|
@ -252,7 +252,7 @@ crypto.hmac('md5', '123456', 'sdfvkjfhd')
|
|||
#### cipher(mode, data[, key, inEncode, outEncode])
|
||||
- mode `<String>`
|
||||
- data `<String>` | `<Buffer>`
|
||||
- key `<String>` 可选, 默认为 `<Buffer 0 0 0 ... 0>`, 即 `Buffer.alloc(16)` 的结果
|
||||
- key `<String>` 可选, 默认为 `<Buffer d7 2c 87 d0 f0 77 c7 76 6f 29 85 df ab 30 e8 95>`, 即 `crypto.scryptSync('', '', 16)` 的结果
|
||||
- inEncode '<String>' 可选
|
||||
- outEncode '<String>' 可选,默认返回Buffer对象
|
||||
|
||||
|
@ -260,7 +260,7 @@ crypto.hmac('md5', '123456', 'sdfvkjfhd')
|
|||
|
||||
> `mode`为算法类型,常见的有`aes-128-cbc、aes-128-gcm`等等地,很多,具体有哪些可以通过 `this.crypto.getCiphers()` 来查看。
|
||||
> 其他的参数与上面的HMAC算法相似; `inEncode`即声明要加密的数据是什么编码的,默认根据要加密的数据进行判断。
|
||||
|
||||
>> 需要注意的是, 算法类型为`aes-***-gcm`时, 返回的不是一个字符串, 而是一个对象{ enStr, authTag }, 解密时, 需要提供这个 authTag方可解密
|
||||
|
||||
```javascript
|
||||
// 这里给出一个AES-128-CBC的加密例子
|
||||
|
@ -271,12 +271,16 @@ crypto.cipher('aes-128-cbc', '123456', 'abcdefg')
|
|||
crypto.cipher('aes-128-cbc', '123456', 'abcdefg', 'utf8', 'hex')
|
||||
// 9aa03d64f87d555f9fc0a95fa6270656
|
||||
|
||||
|
||||
// 要注意gcm算法的结果
|
||||
crypto.cipher('aes-128-gcm', '123456', 'abcdefg')
|
||||
// { enStr: 'qmo1a4Jz',
|
||||
// authTag: <Buffer c4 a0 3e ab e5 34 a0 ea 25 02 f0 91 06 f7 3b dd>
|
||||
// }
|
||||
|
||||
// v3.x 之后, decipher()同理
|
||||
crypto.cipher('aes-128-cbc', '123456', {key})
|
||||
// 等价于
|
||||
crypto.cipheriv('aes-128-cbc', '123456', {key}, EMPTY_IV) // 其中 EMPTY_IV = Buffer.alloc(16)
|
||||
crypto.cipheriv('aes-128-cbc', '123456', {key}, EMPTY_IV) // 其中 EMPTY_IV = crypto.scryptSync('', '', 16)
|
||||
```
|
||||
|
||||
|
||||
|
@ -304,8 +308,14 @@ crypto.decipher('aes-128-cbc', '9aa03d64f87d555f9fc0a95fa6270656', 'abcdefg', 'h
|
|||
|
||||
|
||||
|
||||
// 要注意gcm算法的结果
|
||||
// authTag: <Buffer c4 a0 3e ab e5 34 a0 ea 25 02 f0 91 06 f7 3b dd>
|
||||
crypto.decipher('aes-128-gcm', 'qmo1a4Jz', 'abcdefg', authTag)
|
||||
// 123456
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
> 至于另外的`cipheriv/decipheriv`这2个方法,这里就不细讲了,和上面的这2个是同样的用法,只是要多1个参数`向量(iv)`
|
||||
>> **`特别要注意的一点是,选择128位的加密算法,那key的长度就必须是16位,256则是32位,依此类推`,具体的请看相关文档**
|
||||
>> **`特别要注意的一点是,选择128位的加密算法,那key的长度就必须是16位,256则是32位,依此类推; 算法类型为gcm时,返回的是对象,解密时需要提供authTag `,具体的请看相关文档**
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "crypto.js",
|
||||
"version": "3.2.2",
|
||||
"version": "3.2.0",
|
||||
"description": "原生crypto加密模块的二次封装,简化常用加密函数的使用",
|
||||
"keywords": [
|
||||
"md5",
|
||||
|
|
29
src/index.js
29
src/index.js
|
@ -27,7 +27,7 @@ const MAC = (function (ns) {
|
|||
return Math.random().toString(16).slice(-4)
|
||||
})(os.networkInterfaces())
|
||||
|
||||
let __inc__ = 4096
|
||||
var __inc__ = 4096
|
||||
|
||||
/**
|
||||
* [base64encode base64编码]
|
||||
|
@ -35,7 +35,7 @@ let __inc__ = 4096
|
|||
* @param {bool} urlFriendly [是否对URL友好, 默认否, 是则会把+转成-, /转成_]
|
||||
*/
|
||||
export function base64encode(str, urlFriendly) {
|
||||
let buf, str64
|
||||
var buf, str64
|
||||
|
||||
if (Buffer.isBuffer(str)) {
|
||||
buf = str
|
||||
|
@ -86,8 +86,8 @@ export function rand(len, forceNum) {
|
|||
|
||||
// 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID
|
||||
export function uuid(pipe = '-') {
|
||||
let str = crypto.randomUUID()
|
||||
let now = (~~(Date.now() / 1000)).toString(16)
|
||||
var str = crypto.randomUUID()
|
||||
var now = (~~(Date.now() / 1000)).toString(16)
|
||||
|
||||
if (__inc__ > 65535) {
|
||||
__inc__ = 4096
|
||||
|
@ -121,12 +121,11 @@ export function md5(str, encode) {
|
|||
* @param {Str} file [文件路径]
|
||||
*/
|
||||
export function md5Sign(file) {
|
||||
try {
|
||||
let buf = fs.readFileSync(file)
|
||||
if (fs.accessSync(file, fs.constants.R_OK)) {
|
||||
var buf = fs.readFileSync(file)
|
||||
return hash('md5', buf)
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -150,12 +149,11 @@ export function sha1(str, encode) {
|
|||
* @param {Str} file [文件路径]
|
||||
*/
|
||||
export function sha1Sign(file) {
|
||||
try {
|
||||
let buf = fs.readFileSync(file)
|
||||
if (fs.accessSync(file, fs.constants.R_OK)) {
|
||||
var buf = fs.readFileSync(file)
|
||||
return hash('sha1', buf)
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -179,12 +177,11 @@ export function sha256(str, encoding) {
|
|||
* @param {Str} file [文件路径]
|
||||
*/
|
||||
export function sha256Sign(file) {
|
||||
try {
|
||||
let buf = fs.readFileSync(file)
|
||||
if (fs.accessSync(file, fs.constants.R_OK)) {
|
||||
var buf = fs.readFileSync(file)
|
||||
return hash('sha256', buf)
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export {
|
||||
|
|
Loading…
Reference in New Issue