master
yutent 2023-12-26 15:53:43 +08:00
parent 52af19e58e
commit 9f891e75f5
6 changed files with 122 additions and 75 deletions

View File

@ -34,6 +34,7 @@ npm install crypto.js
// 1、 传统的 commonJS引入, 所有的方法都在上面
var {
origin, // 原生crypto对象
crypto, // 原生crypto对象
uuid,
rand,
md5,
@ -50,7 +51,8 @@ var {
// 2、 全新的 ESM 方式
import crypto from 'crypto.js'
import crypto, {
origin,
origin, // 原生crypto对象
crypto, // 原生crypto对象
uuid,
rand,
md5,
@ -68,7 +70,13 @@ import crypto, {
## 属性
> 其实就一个属性,即 `origin`,即为原生的`crypto`对象,方便在封装的方法中无法满足需求时,可以自行调用原生的`crypto`实现。
### `origin` (Deprecated)
> 即为原生的`crypto`对象,方便在封装的方法中无法满足需求时,可以自行调用原生的`crypto`实现。
### `crypto` (v3.2.0新增)
> 即为原生的`crypto`对象,方便在封装的方法中无法满足需求时,可以自行调用原生的`crypto`实现。
## 常用API方法
> 对使用频率非常高的几种加密/编码进行更加简便的封装。

View File

@ -7,7 +7,7 @@
const Es = require('esbuild')
Es.build({
entryPoints: ['src/index.mjs'],
entryPoints: ['src/index.js'],
outfile: 'dist/index.mjs',
platform: 'node',
bundle: true,
@ -15,7 +15,7 @@ Es.build({
format: 'esm'
})
Es.build({
entryPoints: ['src/index.mjs'],
entryPoints: ['src/index.js'],
outfile: 'dist/index.js',
platform: 'node',
bundle: true,

View File

@ -1,14 +1,15 @@
{
"name": "crypto.js",
"version": "3.1.2",
"type": "module",
"version": "3.2.0",
"description": "原生crypto加密模块的二次封装,简化常用加密函数的使用",
"keywords": [
"md5",
"sha1",
"sha256",
"hmac",
"aes",
"base64",
"fivejs",
"crypto",
"crypto-js",
"crypto.js"
@ -16,10 +17,8 @@
"author": "yutent <yutent.io@gmail.com>",
"repository": {
"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",
"files": [
"dist/*"

View File

@ -12,15 +12,15 @@ const AUTH_MODE = [
'aes-256-ocb'
]
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) {
crypto.randomUUID = function () {
@ -49,13 +49,13 @@ if (!crypto.randomInt) {
}
}
export const origin = crypto
export { crypto }
export const hash = function (mode, data, outEncode) {
let sum = crypto.createHash(mode)
let isBuffer = Buffer.isBuffer(data)
sum.update(data, isBuffer ? 'binary' : 'utf8')
sum.update(data, isBuffer ? 'buffer' : 'utf8')
return sum.digest(outEncode || 'hex')
}
@ -64,101 +64,111 @@ export const hmac = function (mode, data, key, outEncode) {
let sum = crypto.createHmac(mode, key)
let isBuffer = Buffer.isBuffer(data)
sum.update(data, isBuffer ? 'binary' : 'utf8')
sum.update(data, isBuffer ? 'buffer' : 'utf8')
return sum.digest(outEncode || 'hex')
}
export const cipher = function (
mode,
data,
key = EMPTY_KEY,
inEncode,
key = KEY_16,
inEncode = 'utf8',
outEncode
) {
// 10.0.0之后, createCipher方法不推荐使用了
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)
inEncode = isBuffer ? 'binary' : inEncode || 'utf8'
outEncode = outEncode || 'base64'
inEncode = isBuffer ? 'buffer' : inEncode
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) {
let authTag = cipher.getAuthTag()
return { enStr, authTag }
buff = Buffer.concat([buff, _cipher.getAuthTag()])
}
return enStr
return format(buff, outEncode)
}
export const decipher = function (
mode,
data,
key = EMPTY_KEY,
tag,
inEncode,
outEncode
key = KEY_16,
inEncode = 'base64',
outEncode = 'utf8'
) {
// 10.0.0之后, createCipher方法不推荐使用了
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)
inEncode = isBuffer ? 'binary' : inEncode || 'base64'
outEncode = outEncode || 'utf8'
let cd = crypto.createDecipher(mode, key)
if (AUTH_MODE.indexOf(mode) > -1) {
cd.setAuthTag(tag)
if (!isBuffer) {
data = Buffer.from(data, inEncode)
}
let deStr = cd.update(data, inEncode, outEncode)
deStr += cd.final(outEncode)
return deStr
inEncode = 'buffer'
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 (
mode,
data,
key = EMPTY_KEY,
iv = EMPTY_KEY,
inEncode,
key = KEY_16,
iv = KEY_16,
inEncode = 'utf8',
outEncode
) {
let isBuffer = Buffer.isBuffer(data)
inEncode = isBuffer ? 'binary' : inEncode || 'utf8'
outEncode = outEncode || 'base64'
inEncode = isBuffer ? 'buffer' : inEncode
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) {
let authTag = cciv.getAuthTag()
return { enStr, authTag }
buff = Buffer.concat([buff, _cipher.getAuthTag()])
}
return enStr
return format(buff, outEncode)
}
export const decipheriv = function (
mode,
data,
key = EMPTY_KEY,
iv = EMPTY_KEY,
tag,
inEncode,
outEncode
key = KEY_16,
iv = KEY_16,
inEncode = 'base64',
outEncode = 'utf8'
) {
let isBuffer = Buffer.isBuffer(data)
inEncode = isBuffer ? 'binary' : inEncode || 'base64'
outEncode = outEncode || 'utf8'
let dcpiv = crypto.createDecipheriv(mode, key, iv)
if (AUTH_MODE.indexOf(mode) > -1) {
dcpiv.setAuthTag(tag)
if (!isBuffer) {
data = Buffer.from(data, inEncode)
}
let deStr = dcpiv.update(data, inEncode, outEncode)
deStr += dcpiv.final(outEncode)
return deStr
inEncode = 'buffer'
let _decipher = crypto.createDecipheriv(mode, key, iv)
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)
}

View File

@ -7,14 +7,14 @@
import os from 'os'
import fs from 'fs'
import {
origin,
crypto,
hash,
hmac,
cipher,
decipher,
cipheriv,
decipheriv
} from './helper.mjs'
} from './helper.js'
const MAC = (function (ns) {
for (let k in ns) {
@ -79,14 +79,14 @@ export function rand(len, forceNum) {
let max = str.length
let tmp = ''
for (let i = 0; i < len; i++) {
tmp += str[origin.randomInt(max)]
tmp += str[crypto.randomInt(max)]
}
return tmp
}
// 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID
export function uuid(pipe = '-') {
var str = origin.randomUUID()
var str = crypto.randomUUID()
var now = (~~(Date.now() / 1000)).toString(16)
if (__inc__ > 65535) {
@ -184,7 +184,16 @@ export function sha256Sign(file) {
return null
}
export { origin, hash, hmac, cipher, decipher, cipheriv, decipheriv }
export {
crypto,
crypto as origin,
hash,
hmac,
cipher,
decipher,
cipheriv,
decipheriv
}
export default {
base64encode,
@ -197,7 +206,8 @@ export default {
sha1Sign,
sha256,
sha256Sign,
origin,
crypto,
origin: crypto,
hash,
hmac,
cipher,

20
test/test.js Normal file
View File

@ -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))