Skip to content

Commit

Permalink
refactor: 移除 crypto-js,迁移到原生的 crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Oct 30, 2024
1 parent fe48b98 commit 0ba1b0d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/push/dingtalk.ts
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/utils/crypto.ts
Original file line number Diff line number Diff line change
@@ -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')
}

0 comments on commit 0ba1b0d

Please sign in to comment.