master
parent
52af19e58e
commit
9f891e75f5
12
Readme.md
12
Readme.md
|
@ -34,6 +34,7 @@ npm install crypto.js
|
||||||
// 1、 传统的 commonJS引入, 所有的方法都在上面
|
// 1、 传统的 commonJS引入, 所有的方法都在上面
|
||||||
var {
|
var {
|
||||||
origin, // 原生crypto对象
|
origin, // 原生crypto对象
|
||||||
|
crypto, // 原生crypto对象
|
||||||
uuid,
|
uuid,
|
||||||
rand,
|
rand,
|
||||||
md5,
|
md5,
|
||||||
|
@ -50,7 +51,8 @@ var {
|
||||||
// 2、 全新的 ESM 方式
|
// 2、 全新的 ESM 方式
|
||||||
import crypto from 'crypto.js'
|
import crypto from 'crypto.js'
|
||||||
import crypto, {
|
import crypto, {
|
||||||
origin,
|
origin, // 原生crypto对象
|
||||||
|
crypto, // 原生crypto对象
|
||||||
uuid,
|
uuid,
|
||||||
rand,
|
rand,
|
||||||
md5,
|
md5,
|
||||||
|
@ -68,7 +70,13 @@ import crypto, {
|
||||||
|
|
||||||
|
|
||||||
## 属性
|
## 属性
|
||||||
> 其实就一个属性,即 `origin`,即为原生的`crypto`对象,方便在封装的方法中无法满足需求时,可以自行调用原生的`crypto`实现。
|
|
||||||
|
### `origin` (Deprecated)
|
||||||
|
> 即为原生的`crypto`对象,方便在封装的方法中无法满足需求时,可以自行调用原生的`crypto`实现。
|
||||||
|
|
||||||
|
### `crypto` (v3.2.0新增)
|
||||||
|
> 即为原生的`crypto`对象,方便在封装的方法中无法满足需求时,可以自行调用原生的`crypto`实现。
|
||||||
|
|
||||||
|
|
||||||
## 常用API方法
|
## 常用API方法
|
||||||
> 对使用频率非常高的几种加密/编码进行更加简便的封装。
|
> 对使用频率非常高的几种加密/编码进行更加简便的封装。
|
||||||
|
|
4
build.js
4
build.js
|
@ -7,7 +7,7 @@
|
||||||
const Es = require('esbuild')
|
const Es = require('esbuild')
|
||||||
|
|
||||||
Es.build({
|
Es.build({
|
||||||
entryPoints: ['src/index.mjs'],
|
entryPoints: ['src/index.js'],
|
||||||
outfile: 'dist/index.mjs',
|
outfile: 'dist/index.mjs',
|
||||||
platform: 'node',
|
platform: 'node',
|
||||||
bundle: true,
|
bundle: true,
|
||||||
|
@ -15,7 +15,7 @@ Es.build({
|
||||||
format: 'esm'
|
format: 'esm'
|
||||||
})
|
})
|
||||||
Es.build({
|
Es.build({
|
||||||
entryPoints: ['src/index.mjs'],
|
entryPoints: ['src/index.js'],
|
||||||
outfile: 'dist/index.js',
|
outfile: 'dist/index.js',
|
||||||
platform: 'node',
|
platform: 'node',
|
||||||
bundle: true,
|
bundle: true,
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
{
|
{
|
||||||
"name": "crypto.js",
|
"name": "crypto.js",
|
||||||
"version": "3.1.2",
|
"type": "module",
|
||||||
|
"version": "3.2.0",
|
||||||
"description": "原生crypto加密模块的二次封装,简化常用加密函数的使用",
|
"description": "原生crypto加密模块的二次封装,简化常用加密函数的使用",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"md5",
|
"md5",
|
||||||
"sha1",
|
"sha1",
|
||||||
"sha256",
|
"sha256",
|
||||||
"hmac",
|
"hmac",
|
||||||
|
"aes",
|
||||||
"base64",
|
"base64",
|
||||||
"fivejs",
|
|
||||||
"crypto",
|
"crypto",
|
||||||
"crypto-js",
|
"crypto-js",
|
||||||
"crypto.js"
|
"crypto.js"
|
||||||
|
@ -16,10 +17,8 @@
|
||||||
"author": "yutent <yutent.io@gmail.com>",
|
"author": "yutent <yutent.io@gmail.com>",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/bytedo/crypto.js.git"
|
"url": "https://git.wkit.fun/bytedo/crypto.js.git"
|
||||||
},
|
},
|
||||||
"dependencies": {},
|
|
||||||
"devDependencies": {},
|
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"files": [
|
"files": [
|
||||||
"dist/*"
|
"dist/*"
|
||||||
|
|
|
@ -12,15 +12,15 @@ const AUTH_MODE = [
|
||||||
'aes-256-ocb'
|
'aes-256-ocb'
|
||||||
]
|
]
|
||||||
const VERSION = +process.versions.node.split('.').slice(0, 2).join('.')
|
const VERSION = +process.versions.node.split('.').slice(0, 2).join('.')
|
||||||
|
const KEY_16 = Buffer.alloc(16)
|
||||||
|
|
||||||
|
function format(buff, encode) {
|
||||||
|
if (encode === void 0 || encode === 'buffer') {
|
||||||
|
return buff
|
||||||
|
}
|
||||||
|
return buff.toString(encode)
|
||||||
|
}
|
||||||
|
|
||||||
// <Buffer d7 2c 87 d0 f0 77 c7 76 6f 29 85 df ab 30 e8 95>
|
|
||||||
const EMPTY_KEY = crypto.scryptSync
|
|
||||||
? crypto.scryptSync('', '', 16)
|
|
||||||
: Buffer.from(
|
|
||||||
'0xd7 0x2c 0x87 0xd0 0xf0 0x77 0xc7 0x76 0x6f 0x29 0x85 0xdf 0xab 0x30 0xe8 0x95'.split(
|
|
||||||
' '
|
|
||||||
)
|
|
||||||
)
|
|
||||||
//
|
//
|
||||||
if (!crypto.randomUUID) {
|
if (!crypto.randomUUID) {
|
||||||
crypto.randomUUID = function () {
|
crypto.randomUUID = function () {
|
||||||
|
@ -49,13 +49,13 @@ if (!crypto.randomInt) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const origin = crypto
|
export { crypto }
|
||||||
|
|
||||||
export const hash = function (mode, data, outEncode) {
|
export const hash = function (mode, data, outEncode) {
|
||||||
let sum = crypto.createHash(mode)
|
let sum = crypto.createHash(mode)
|
||||||
let isBuffer = Buffer.isBuffer(data)
|
let isBuffer = Buffer.isBuffer(data)
|
||||||
|
|
||||||
sum.update(data, isBuffer ? 'binary' : 'utf8')
|
sum.update(data, isBuffer ? 'buffer' : 'utf8')
|
||||||
return sum.digest(outEncode || 'hex')
|
return sum.digest(outEncode || 'hex')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,101 +64,111 @@ export const hmac = function (mode, data, key, outEncode) {
|
||||||
let sum = crypto.createHmac(mode, key)
|
let sum = crypto.createHmac(mode, key)
|
||||||
let isBuffer = Buffer.isBuffer(data)
|
let isBuffer = Buffer.isBuffer(data)
|
||||||
|
|
||||||
sum.update(data, isBuffer ? 'binary' : 'utf8')
|
sum.update(data, isBuffer ? 'buffer' : 'utf8')
|
||||||
return sum.digest(outEncode || 'hex')
|
return sum.digest(outEncode || 'hex')
|
||||||
}
|
}
|
||||||
|
|
||||||
export const cipher = function (
|
export const cipher = function (
|
||||||
mode,
|
mode,
|
||||||
data,
|
data,
|
||||||
key = EMPTY_KEY,
|
key = KEY_16,
|
||||||
inEncode,
|
inEncode = 'utf8',
|
||||||
outEncode
|
outEncode
|
||||||
) {
|
) {
|
||||||
// 10.0.0之后, createCipher方法不推荐使用了
|
// 10.0.0之后, createCipher方法不推荐使用了
|
||||||
if (VERSION >= 10.5) {
|
if (VERSION >= 10.5) {
|
||||||
return cipheriv(mode, data, key, EMPTY_KEY, inEncode, outEncode)
|
return cipheriv(mode, data, key, KEY_16, inEncode, outEncode)
|
||||||
}
|
}
|
||||||
let isBuffer = Buffer.isBuffer(data)
|
let isBuffer = Buffer.isBuffer(data)
|
||||||
inEncode = isBuffer ? 'binary' : inEncode || 'utf8'
|
inEncode = isBuffer ? 'buffer' : inEncode
|
||||||
outEncode = outEncode || 'base64'
|
|
||||||
|
let _cipher = crypto.createCipher(mode, key)
|
||||||
|
let buff = _cipher.update(data, inEncode)
|
||||||
|
buff = Buffer.concat([buff, _cipher.final()])
|
||||||
|
|
||||||
let cipher = crypto.createCipher(mode, key)
|
|
||||||
let enStr = cipher.update(data, inEncode, outEncode)
|
|
||||||
enStr += cipher.final(outEncode)
|
|
||||||
if (AUTH_MODE.indexOf(mode) > -1) {
|
if (AUTH_MODE.indexOf(mode) > -1) {
|
||||||
let authTag = cipher.getAuthTag()
|
buff = Buffer.concat([buff, _cipher.getAuthTag()])
|
||||||
return { enStr, authTag }
|
|
||||||
}
|
}
|
||||||
return enStr
|
return format(buff, outEncode)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const decipher = function (
|
export const decipher = function (
|
||||||
mode,
|
mode,
|
||||||
data,
|
data,
|
||||||
key = EMPTY_KEY,
|
key = KEY_16,
|
||||||
tag,
|
inEncode = 'base64',
|
||||||
inEncode,
|
outEncode = 'utf8'
|
||||||
outEncode
|
|
||||||
) {
|
) {
|
||||||
// 10.0.0之后, createCipher方法不推荐使用了
|
// 10.0.0之后, createCipher方法不推荐使用了
|
||||||
if (VERSION >= 10.5) {
|
if (VERSION >= 10.5) {
|
||||||
return decipheriv(mode, data, key, EMPTY_KEY, tag, inEncode, outEncode)
|
return decipheriv(mode, data, key, KEY_16, inEncode, outEncode)
|
||||||
}
|
}
|
||||||
|
|
||||||
let isBuffer = Buffer.isBuffer(data)
|
let isBuffer = Buffer.isBuffer(data)
|
||||||
inEncode = isBuffer ? 'binary' : inEncode || 'base64'
|
if (!isBuffer) {
|
||||||
outEncode = outEncode || 'utf8'
|
data = Buffer.from(data, inEncode)
|
||||||
|
|
||||||
let cd = crypto.createDecipher(mode, key)
|
|
||||||
if (AUTH_MODE.indexOf(mode) > -1) {
|
|
||||||
cd.setAuthTag(tag)
|
|
||||||
}
|
}
|
||||||
let deStr = cd.update(data, inEncode, outEncode)
|
inEncode = 'buffer'
|
||||||
deStr += cd.final(outEncode)
|
|
||||||
return deStr
|
let _decipher = crypto.createDecipher(mode, key)
|
||||||
|
|
||||||
|
if (AUTH_MODE.indexOf(mode) > -1) {
|
||||||
|
let tag = data.slice(-16)
|
||||||
|
data = data.slice(0, -16)
|
||||||
|
_decipher.setAuthTag(tag)
|
||||||
|
}
|
||||||
|
let buff = _decipher.update(data, inEncode)
|
||||||
|
buff = Buffer.concat([buff, _decipher.final()])
|
||||||
|
return format(buff, outEncode)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const cipheriv = function (
|
export const cipheriv = function (
|
||||||
mode,
|
mode,
|
||||||
data,
|
data,
|
||||||
key = EMPTY_KEY,
|
key = KEY_16,
|
||||||
iv = EMPTY_KEY,
|
iv = KEY_16,
|
||||||
inEncode,
|
inEncode = 'utf8',
|
||||||
outEncode
|
outEncode
|
||||||
) {
|
) {
|
||||||
let isBuffer = Buffer.isBuffer(data)
|
let isBuffer = Buffer.isBuffer(data)
|
||||||
inEncode = isBuffer ? 'binary' : inEncode || 'utf8'
|
inEncode = isBuffer ? 'buffer' : inEncode
|
||||||
outEncode = outEncode || 'base64'
|
|
||||||
|
let _cipher = crypto.createCipheriv(mode, key, iv)
|
||||||
|
let buff = _cipher.update(data, inEncode)
|
||||||
|
|
||||||
|
buff = Buffer.concat([buff, _cipher.final()])
|
||||||
|
|
||||||
let cciv = crypto.createCipheriv(mode, key, iv)
|
|
||||||
let enStr = cciv.update(data, inEncode, outEncode)
|
|
||||||
enStr += cciv.final(outEncode)
|
|
||||||
if (AUTH_MODE.indexOf(mode) > -1) {
|
if (AUTH_MODE.indexOf(mode) > -1) {
|
||||||
let authTag = cciv.getAuthTag()
|
buff = Buffer.concat([buff, _cipher.getAuthTag()])
|
||||||
return { enStr, authTag }
|
|
||||||
}
|
}
|
||||||
return enStr
|
return format(buff, outEncode)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const decipheriv = function (
|
export const decipheriv = function (
|
||||||
mode,
|
mode,
|
||||||
data,
|
data,
|
||||||
key = EMPTY_KEY,
|
key = KEY_16,
|
||||||
iv = EMPTY_KEY,
|
iv = KEY_16,
|
||||||
tag,
|
inEncode = 'base64',
|
||||||
inEncode,
|
outEncode = 'utf8'
|
||||||
outEncode
|
|
||||||
) {
|
) {
|
||||||
let isBuffer = Buffer.isBuffer(data)
|
let isBuffer = Buffer.isBuffer(data)
|
||||||
inEncode = isBuffer ? 'binary' : inEncode || 'base64'
|
|
||||||
outEncode = outEncode || 'utf8'
|
|
||||||
|
|
||||||
let dcpiv = crypto.createDecipheriv(mode, key, iv)
|
if (!isBuffer) {
|
||||||
|
data = Buffer.from(data, inEncode)
|
||||||
|
}
|
||||||
|
inEncode = 'buffer'
|
||||||
|
|
||||||
|
let _decipher = crypto.createDecipheriv(mode, key, iv)
|
||||||
|
|
||||||
if (AUTH_MODE.indexOf(mode) > -1) {
|
if (AUTH_MODE.indexOf(mode) > -1) {
|
||||||
dcpiv.setAuthTag(tag)
|
let tag = data.slice(-16)
|
||||||
|
data = data.slice(0, -16)
|
||||||
|
_decipher.setAuthTag(tag)
|
||||||
}
|
}
|
||||||
let deStr = dcpiv.update(data, inEncode, outEncode)
|
|
||||||
deStr += dcpiv.final(outEncode)
|
let buff = _decipher.update(data, inEncode)
|
||||||
return deStr
|
buff = Buffer.concat([buff, _decipher.final()])
|
||||||
|
|
||||||
|
return format(buff, outEncode)
|
||||||
}
|
}
|
|
@ -7,14 +7,14 @@
|
||||||
import os from 'os'
|
import os from 'os'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import {
|
import {
|
||||||
origin,
|
crypto,
|
||||||
hash,
|
hash,
|
||||||
hmac,
|
hmac,
|
||||||
cipher,
|
cipher,
|
||||||
decipher,
|
decipher,
|
||||||
cipheriv,
|
cipheriv,
|
||||||
decipheriv
|
decipheriv
|
||||||
} from './helper.mjs'
|
} from './helper.js'
|
||||||
|
|
||||||
const MAC = (function (ns) {
|
const MAC = (function (ns) {
|
||||||
for (let k in ns) {
|
for (let k in ns) {
|
||||||
|
@ -79,14 +79,14 @@ export function rand(len, forceNum) {
|
||||||
let max = str.length
|
let max = str.length
|
||||||
let tmp = ''
|
let tmp = ''
|
||||||
for (let i = 0; i < len; i++) {
|
for (let i = 0; i < len; i++) {
|
||||||
tmp += str[origin.randomInt(max)]
|
tmp += str[crypto.randomInt(max)]
|
||||||
}
|
}
|
||||||
return tmp
|
return tmp
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID
|
// 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID
|
||||||
export function uuid(pipe = '-') {
|
export function uuid(pipe = '-') {
|
||||||
var str = origin.randomUUID()
|
var str = crypto.randomUUID()
|
||||||
var now = (~~(Date.now() / 1000)).toString(16)
|
var now = (~~(Date.now() / 1000)).toString(16)
|
||||||
|
|
||||||
if (__inc__ > 65535) {
|
if (__inc__ > 65535) {
|
||||||
|
@ -184,7 +184,16 @@ export function sha256Sign(file) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
export { origin, hash, hmac, cipher, decipher, cipheriv, decipheriv }
|
export {
|
||||||
|
crypto,
|
||||||
|
crypto as origin,
|
||||||
|
hash,
|
||||||
|
hmac,
|
||||||
|
cipher,
|
||||||
|
decipher,
|
||||||
|
cipheriv,
|
||||||
|
decipheriv
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
base64encode,
|
base64encode,
|
||||||
|
@ -197,7 +206,8 @@ export default {
|
||||||
sha1Sign,
|
sha1Sign,
|
||||||
sha256,
|
sha256,
|
||||||
sha256Sign,
|
sha256Sign,
|
||||||
origin,
|
crypto,
|
||||||
|
origin: crypto,
|
||||||
hash,
|
hash,
|
||||||
hmac,
|
hmac,
|
||||||
cipher,
|
cipher,
|
|
@ -0,0 +1,20 @@
|
||||||
|
import { crypto, cipher, cipheriv, decipher, decipheriv } from '../src/index.js'
|
||||||
|
|
||||||
|
let algorithm = 'aes-128-cbc'
|
||||||
|
let data = 'abcd'
|
||||||
|
let key = Buffer.alloc(16)
|
||||||
|
let key2 = Buffer.alloc(32)
|
||||||
|
let iv = Buffer.alloc(16)
|
||||||
|
|
||||||
|
let encode = 'base64'
|
||||||
|
|
||||||
|
// console.log(crypto.getCiphers())
|
||||||
|
|
||||||
|
let output1 = cipher(algorithm, data, key, 'utf8', encode)
|
||||||
|
let output2 = cipheriv(algorithm, data, key, iv)
|
||||||
|
|
||||||
|
console.log(output1)
|
||||||
|
console.log(output2.toString(encode))
|
||||||
|
|
||||||
|
console.log(decipher(algorithm, output1, key))
|
||||||
|
console.log(decipheriv(algorithm, output2, key, iv))
|
Loading…
Reference in New Issue