parent
f26091f81c
commit
1794ab0470
11
Readme.md
11
Readme.md
|
@ -3,6 +3,9 @@
|
|||
|
||||
## 更新日志
|
||||
|
||||
+ v3.3.0
|
||||
- 增加 `.d.ts`, 默认为esm加载
|
||||
|
||||
+ v3.2.0
|
||||
- 调整`cipher,decipher,cipheriv,decipheriv`的传参和返回结果, `aes-gcm`等算法,`tag`会拼接在密文后面。
|
||||
- 增加`crypto`属性返回, 该属性为`原生crypto对象`
|
||||
|
@ -49,6 +52,8 @@ var {
|
|||
sha1Sign,
|
||||
sha256,
|
||||
sha256Sign,
|
||||
sha512,
|
||||
sha512Sign,
|
||||
base64encode,
|
||||
base64decode
|
||||
} = require('crypto.js')
|
||||
|
@ -67,6 +72,8 @@ import crypto, {
|
|||
sha1Sign,
|
||||
sha256,
|
||||
sha256Sign,
|
||||
sha512,
|
||||
sha512Sign,
|
||||
base64encode,
|
||||
base64decode,
|
||||
} from 'crypto.js'
|
||||
|
@ -87,9 +94,9 @@ import crypto, {
|
|||
## 常用API方法
|
||||
> 对使用频率非常高的几种加密/编码进行更加简便的封装。
|
||||
|
||||
### rand(len[, onlyNumber])
|
||||
### rand(len[, forceNum])
|
||||
- len `<Number>` 需要的字符长度
|
||||
- onlyNumber `<Boolean>` 返回纯数字字符串 [可选]
|
||||
- forceNum `<Boolean>` 返回纯数字字符串 [可选]
|
||||
|
||||
> 该方法用于生成指定长度的随机字符串`[a-z-A-z0-9]`
|
||||
|
||||
|
|
5
build.js
5
build.js
|
@ -4,7 +4,8 @@
|
|||
* @date 2021/08/09 11:59:41
|
||||
*/
|
||||
|
||||
const Es = require('esbuild')
|
||||
import Es from 'esbuild'
|
||||
import fs from 'iofs'
|
||||
|
||||
Es.build({
|
||||
entryPoints: ['src/index.js'],
|
||||
|
@ -21,3 +22,5 @@ Es.build({
|
|||
bundle: true,
|
||||
minify: true
|
||||
})
|
||||
|
||||
fs.cp('src/index.d.ts', 'dist/index.d.ts')
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
{
|
||||
"name": "crypto.js",
|
||||
"version": "3.2.2",
|
||||
"type": "module",
|
||||
"version": "3.3.0",
|
||||
"description": "原生crypto加密模块的二次封装,简化常用加密函数的使用",
|
||||
"keywords": [
|
||||
"md5",
|
||||
|
@ -19,6 +20,7 @@
|
|||
"url": "https://git.wkit.fun/bytedo/crypto.js.git"
|
||||
},
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"files": [
|
||||
"dist/*"
|
||||
],
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
/**
|
||||
* {}
|
||||
* @author yutent<yutent.io@gmail.com>
|
||||
* @date 2024/12/12 15:41:25
|
||||
*/
|
||||
declare module 'crypto.js' {
|
||||
// 随机数生成
|
||||
export function rand(len: number, forceNum: boolean): string
|
||||
|
||||
// 生成唯一 uuid
|
||||
export function uuid(pipe: string): string
|
||||
|
||||
export function base64encode(str: string, urlFriendly: boolean): string
|
||||
|
||||
export function base64decode(str: string, urlFriendly: boolean): string
|
||||
|
||||
export function md5(data: string, encode: string): string | Buffer
|
||||
|
||||
export function md5Sign(path: string, encode: string): string | Buffer
|
||||
|
||||
export function sha1(data: string, encode: string): string | Buffer
|
||||
|
||||
export function sha1Sign(path: string, encode: string): string | Buffer
|
||||
|
||||
export function sha256(data: string, encode: string): string | Buffer
|
||||
|
||||
export function sha256Sign(path: string, encode: string): string | Buffer
|
||||
|
||||
export function sha1(data: string, encode: string): string | Buffer
|
||||
|
||||
export function sha1Sign(path: string, encode: string): string | Buffer
|
||||
|
||||
export function sha512(data: string, encode: string): string | Buffer
|
||||
|
||||
export function sha512Sign(path: string, encode: string): string | Buffer
|
||||
|
||||
//
|
||||
export function hash(
|
||||
mode: string,
|
||||
data: string | Buffer,
|
||||
outEncode: string
|
||||
): string | Buffer
|
||||
|
||||
//
|
||||
export function hmac(
|
||||
mode: string,
|
||||
data: string | Buffer,
|
||||
key: string | Buffer,
|
||||
outEncode: string
|
||||
): string | Buffer
|
||||
|
||||
//
|
||||
export function cipher(
|
||||
mode: string,
|
||||
data: string | Buffer,
|
||||
key: string | Buffer,
|
||||
inEncode: string,
|
||||
outEncode: string
|
||||
): string | Buffer
|
||||
|
||||
//
|
||||
export function decipher(
|
||||
mode: string,
|
||||
data: string | Buffer,
|
||||
key: string | Buffer,
|
||||
inEncode: string,
|
||||
outEncode: string
|
||||
): string | Buffer
|
||||
|
||||
//
|
||||
export function cipheriv(
|
||||
mode: string,
|
||||
data: string | Buffer,
|
||||
key: string | Buffer,
|
||||
iv: string | Buffer,
|
||||
inEncode: string,
|
||||
outEncode: string
|
||||
): string | Buffer
|
||||
|
||||
//
|
||||
export function decipheriv(
|
||||
mode: string,
|
||||
data: string | Buffer,
|
||||
key: string | Buffer,
|
||||
iv: string | Buffer,
|
||||
inEncode: string,
|
||||
outEncode: string
|
||||
): string | Buffer
|
||||
}
|
50
src/index.js
50
src/index.js
|
@ -119,11 +119,12 @@ export function md5(str, encode) {
|
|||
/**
|
||||
* [md5Sign 获取文件的md5签名]
|
||||
* @param {Str} file [文件路径]
|
||||
* @param {Str} encode [hex/base64]
|
||||
*/
|
||||
export function md5Sign(file) {
|
||||
export function md5Sign(file, encode) {
|
||||
try {
|
||||
let buf = fs.readFileSync(file)
|
||||
return hash('md5', buf)
|
||||
return hash('md5', buf, encode)
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
|
@ -149,10 +150,10 @@ export function sha1(str, encode) {
|
|||
* [sha1Sign 获取文件的sha1签名]
|
||||
* @param {Str} file [文件路径]
|
||||
*/
|
||||
export function sha1Sign(file) {
|
||||
export function sha1Sign(file, encode) {
|
||||
try {
|
||||
let buf = fs.readFileSync(file)
|
||||
return hash('sha1', buf)
|
||||
return hash('sha1', buf, encode)
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
|
@ -161,14 +162,14 @@ export function sha1Sign(file) {
|
|||
/**
|
||||
* [sha256 sha256加密]
|
||||
* @param {Str/Num} str [要加密的字符串]
|
||||
* @param {Str} encoding [hex/base64]
|
||||
* @param {Str} encode [hex/base64]
|
||||
*/
|
||||
export function sha256(str, encoding) {
|
||||
export function sha256(str, encode) {
|
||||
if (typeof str === 'number') {
|
||||
str += ''
|
||||
}
|
||||
if (typeof str === 'string' || Buffer.isBuffer(str)) {
|
||||
return hash('sha256', str, encoding)
|
||||
return hash('sha256', str, encode)
|
||||
}
|
||||
|
||||
return str
|
||||
|
@ -178,10 +179,39 @@ export function sha256(str, encoding) {
|
|||
* [sha256Sign 获取文件的sha256签名]
|
||||
* @param {Str} file [文件路径]
|
||||
*/
|
||||
export function sha256Sign(file) {
|
||||
export function sha256Sign(file, encode) {
|
||||
try {
|
||||
let buf = fs.readFileSync(file)
|
||||
return hash('sha256', buf)
|
||||
return hash('sha256', buf, encode)
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* [sha512 sha512加密]
|
||||
* @param {Str/Num} str [要加密的字符串]
|
||||
* @param {Str} encode [hex/base64]
|
||||
*/
|
||||
export function sha512(str, encode) {
|
||||
if (typeof str === 'number') {
|
||||
str += ''
|
||||
}
|
||||
if (typeof str === 'string' || Buffer.isBuffer(str)) {
|
||||
return hash('sha512', str, encode)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
/**
|
||||
* [sha512Sign 获取文件的sha512签名]
|
||||
* @param {Str} file [文件路径]
|
||||
*/
|
||||
export function sha512Sign(file, encode) {
|
||||
try {
|
||||
let buf = fs.readFileSync(file)
|
||||
return hash('sha512', buf, encode)
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
|
@ -209,6 +239,8 @@ export default {
|
|||
sha1Sign,
|
||||
sha256,
|
||||
sha256Sign,
|
||||
sha512,
|
||||
sha512Sign,
|
||||
crypto,
|
||||
origin: crypto,
|
||||
hash,
|
||||
|
|
Loading…
Reference in New Issue