From 0ba1b0d5e0da68d130b45f79f5b573ec5b871c4b Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Wed, 30 Oct 2024 16:07:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=20crypto-js?= =?UTF-8?q?=EF=BC=8C=E8=BF=81=E7=A7=BB=E5=88=B0=E5=8E=9F=E7=94=9F=E7=9A=84?= =?UTF-8?q?=20crypto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/push/dingtalk.ts | 5 +++-- src/utils/crypto.ts | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 src/utils/crypto.ts diff --git a/src/push/dingtalk.ts b/src/push/dingtalk.ts index ece04cc..61bacd4 100644 --- a/src/push/dingtalk.ts +++ b/src/push/dingtalk.ts @@ -1,12 +1,13 @@ import { AxiosResponse } from 'axios' import debug from 'debug' -import CryptoJS from 'crypto-js' + import { MessageTemplateAbs } from './dingtalk/template' import { Text } from './dingtalk/Text' import { Markdown } from './dingtalk/Markdown' import { Send } from '@/interfaces/send' import { warn } from '@/utils/helper' import { ajax } from '@/utils/ajax' +import { base64Encode, hmacSha256Encode } from '@/utils/crypto' const Debugger = debug('push:dingtalk') @@ -44,7 +45,7 @@ export class Dingtalk implements Send { private getSign(timeStamp: number): string { let signStr = '' if (this.SECRET) { - signStr = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(`${timeStamp}\n${this.SECRET}`, this.SECRET)) + signStr = base64Encode(hmacSha256Encode(`${timeStamp}\n${this.SECRET}`, this.SECRET)) Debugger('Sign string is %s, result is %s', `${timeStamp}\n${this.SECRET}`, signStr) } return signStr diff --git a/src/utils/crypto.ts b/src/utils/crypto.ts new file mode 100644 index 0000000..1b30e48 --- /dev/null +++ b/src/utils/crypto.ts @@ -0,0 +1,11 @@ +import crypto from 'crypto' + +// Base64 编码 +export function base64Encode(str: string): string { + return Buffer.from(str).toString('base64') +} + +// HmacSHA256 加密 +export function hmacSha256Encode(str: string, secret: string): string { + return crypto.createHmac('sha256', secret).update(str).digest('hex') +}