crypto.js/src/helper.js

175 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

2020-09-16 20:01:35 +08:00
import crypto from 'crypto'
const AUTH_MODE = [
'aes-128-gcm',
'aes-192-gcm',
'aes-256-gcm',
'aes-128-ccm',
'aes-192-ccm',
'aes-256-ccm',
'aes-128-ocb',
'aes-192-ocb',
'aes-256-ocb'
]
2023-02-13 17:19:05 +08:00
const VERSION = +process.versions.node.split('.').slice(0, 2).join('.')
2023-12-26 15:53:43 +08:00
const KEY_16 = Buffer.alloc(16)
function format(buff, encode) {
if (encode === void 0 || encode === 'buffer') {
return buff
}
return buff.toString(encode)
}
2022-01-08 16:00:36 +08:00
2023-02-13 17:19:05 +08:00
//
if (!crypto.randomUUID) {
crypto.randomUUID = function () {
let str = crypto.randomBytes(16).toString('hex')
return (
str.slice(0, 8) +
'-' +
str.slice(8, 12) +
'-' +
str.slice(12, 16) +
'-' +
str.slice(16, 20) +
'-' +
str.slice(-12)
)
}
}
if (!crypto.randomInt) {
crypto.randomInt = function (max) {
if (max === void 0) {
throw new TypeError(
'[ERR_INVALID_ARG_TYPE]: The "max" argument must be a safe integer. Received undefined'
)
}
return ~~(Math.random() * max)
}
}
2020-09-16 20:01:35 +08:00
2023-12-26 15:53:43 +08:00
export { crypto }
2020-09-16 20:01:35 +08:00
2023-02-13 17:19:05 +08:00
export const hash = function (mode, data, outEncode) {
let sum = crypto.createHash(mode)
let isBuffer = Buffer.isBuffer(data)
2020-09-16 20:01:35 +08:00
2023-12-26 15:53:43 +08:00
sum.update(data, isBuffer ? 'buffer' : 'utf8')
2023-02-13 17:19:05 +08:00
return sum.digest(outEncode || 'hex')
}
2020-09-16 20:01:35 +08:00
2023-02-13 17:19:05 +08:00
export const hmac = function (mode, data, key, outEncode) {
key = key || ''
let sum = crypto.createHmac(mode, key)
let isBuffer = Buffer.isBuffer(data)
2020-09-16 20:01:35 +08:00
2023-12-26 15:53:43 +08:00
sum.update(data, isBuffer ? 'buffer' : 'utf8')
2023-02-13 17:19:05 +08:00
return sum.digest(outEncode || 'hex')
}
2020-09-16 20:01:35 +08:00
2023-02-13 17:19:05 +08:00
export const cipher = function (
mode,
data,
2023-12-26 15:53:43 +08:00
key = KEY_16,
inEncode = 'utf8',
2023-02-13 17:19:05 +08:00
outEncode
) {
// 10.0.0之后, createCipher方法不推荐使用了
if (VERSION >= 10.5) {
2023-12-26 15:53:43 +08:00
return cipheriv(mode, data, key, KEY_16, inEncode, outEncode)
2023-02-13 17:19:05 +08:00
}
let isBuffer = Buffer.isBuffer(data)
2023-12-26 15:53:43 +08:00
inEncode = isBuffer ? 'buffer' : inEncode
let _cipher = crypto.createCipher(mode, key)
let buff = _cipher.update(data, inEncode)
buff = Buffer.concat([buff, _cipher.final()])
2023-02-13 17:19:05 +08:00
if (AUTH_MODE.indexOf(mode) > -1) {
2023-12-26 15:53:43 +08:00
buff = Buffer.concat([buff, _cipher.getAuthTag()])
2023-02-13 17:19:05 +08:00
}
2023-12-26 15:53:43 +08:00
return format(buff, outEncode)
2023-02-13 17:19:05 +08:00
}
2020-09-16 20:01:35 +08:00
2023-02-13 17:19:05 +08:00
export const decipher = function (
mode,
data,
2023-12-26 15:53:43 +08:00
key = KEY_16,
inEncode = 'base64',
outEncode = 'utf8'
2023-02-13 17:19:05 +08:00
) {
// 10.0.0之后, createCipher方法不推荐使用了
if (VERSION >= 10.5) {
2023-12-26 15:53:43 +08:00
return decipheriv(mode, data, key, KEY_16, inEncode, outEncode)
2023-02-13 17:19:05 +08:00
}
2023-02-13 17:19:05 +08:00
let isBuffer = Buffer.isBuffer(data)
2023-12-26 15:53:43 +08:00
if (!isBuffer) {
data = Buffer.from(data, inEncode)
}
inEncode = 'buffer'
let _decipher = crypto.createDecipher(mode, key)
2020-09-16 20:01:35 +08:00
2023-02-13 17:19:05 +08:00
if (AUTH_MODE.indexOf(mode) > -1) {
2023-12-26 15:53:43 +08:00
let tag = data.slice(-16)
data = data.slice(0, -16)
_decipher.setAuthTag(tag)
2023-02-13 17:19:05 +08:00
}
2023-12-26 15:53:43 +08:00
let buff = _decipher.update(data, inEncode)
buff = Buffer.concat([buff, _decipher.final()])
return format(buff, outEncode)
2023-02-13 17:19:05 +08:00
}
2020-09-16 20:01:35 +08:00
2023-02-13 17:19:05 +08:00
export const cipheriv = function (
mode,
data,
2023-12-26 15:53:43 +08:00
key = KEY_16,
iv = KEY_16,
inEncode = 'utf8',
2023-02-13 17:19:05 +08:00
outEncode
) {
let isBuffer = Buffer.isBuffer(data)
2023-12-26 15:53:43 +08:00
inEncode = isBuffer ? 'buffer' : inEncode
let _cipher = crypto.createCipheriv(mode, key, iv)
let buff = _cipher.update(data, inEncode)
buff = Buffer.concat([buff, _cipher.final()])
2023-02-13 17:19:05 +08:00
if (AUTH_MODE.indexOf(mode) > -1) {
2023-12-26 15:53:43 +08:00
buff = Buffer.concat([buff, _cipher.getAuthTag()])
2023-02-13 17:19:05 +08:00
}
2023-12-26 15:53:43 +08:00
return format(buff, outEncode)
2023-02-13 17:19:05 +08:00
}
2020-09-16 20:01:35 +08:00
2023-02-13 17:19:05 +08:00
export const decipheriv = function (
mode,
data,
2023-12-26 15:53:43 +08:00
key = KEY_16,
iv = KEY_16,
inEncode = 'base64',
outEncode = 'utf8'
2023-02-13 17:19:05 +08:00
) {
let isBuffer = Buffer.isBuffer(data)
2023-12-26 15:53:43 +08:00
if (!isBuffer) {
data = Buffer.from(data, inEncode)
}
inEncode = 'buffer'
let _decipher = crypto.createDecipheriv(mode, key, iv)
2023-02-13 17:19:05 +08:00
if (AUTH_MODE.indexOf(mode) > -1) {
2023-12-26 15:53:43 +08:00
let tag = data.slice(-16)
data = data.slice(0, -16)
_decipher.setAuthTag(tag)
2020-09-16 20:01:35 +08:00
}
2023-12-26 15:53:43 +08:00
let buff = _decipher.update(data, inEncode)
buff = Buffer.concat([buff, _decipher.final()])
return format(buff, outEncode)
2020-09-16 20:01:35 +08:00
}