From 1794ab0470c07a68becf27c7b75af92814512e5f Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 12 Dec 2024 16:05:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20`.d.ts`,=20=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E4=B8=BAesm=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 11 +++++-- build.js | 5 ++- package.json | 4 ++- src/index.d.ts | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 50 +++++++++++++++++++++++----- 5 files changed, 146 insertions(+), 13 deletions(-) create mode 100644 src/index.d.ts diff --git a/Readme.md b/Readme.md index 9b2c81f..f2d01f3 100644 --- a/Readme.md +++ b/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 `` 需要的字符长度 -- onlyNumber `` 返回纯数字字符串 [可选] +- forceNum `` 返回纯数字字符串 [可选] > 该方法用于生成指定长度的随机字符串`[a-z-A-z0-9]` diff --git a/build.js b/build.js index 0e01519..dc5b5fb 100644 --- a/build.js +++ b/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') diff --git a/package.json b/package.json index 39a3611..d56d56b 100644 --- a/package.json +++ b/package.json @@ -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/*" ], diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..16efb14 --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,89 @@ +/** + * {} + * @author yutent + * @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 +} diff --git a/src/index.js b/src/index.js index ea46066..bafad9f 100644 --- a/src/index.js +++ b/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,