clear environment variable, update download-docx(sample) to check latest version before query database.

This commit is contained in:
oom 2026-11-02 10:00:12 +07:00
parent bc61f0fd2e
commit 45ff0001e6
7 changed files with 103 additions and 51 deletions

View file

@ -3,8 +3,17 @@ import { createHmac, createPrivateKey, createPublicKey, timingSafeEqual, verify,
/**
* คำนวณ HMAC-SHA256 จาก string หรือ JSON Object
*/
/**
* ฟังก์ชันสำหรับลงนามดิจิทัล (Sign/hash) ข้อมูลด้วย HMAC-SHA256 secret
* @param data ข้อความดิบที่ต้องการเซ็น (string หรือ object)
* @param secretKey คีย์ลับในรูปแบบ string
* @returns ลายเซ็น (Signature/hash) ในรูปแบบ Hex String (ความยาว 64 ตัวอักษร)
*/
export function generateHmacSha256(data: string | object, secretKey: string): string {
const payload = typeof data === 'string' ? data : JSON.stringify(data);
const payload = typeof data === 'string' ? data:JSON.stringify(sortObjectKeys(data as Record<string, unknown>));
return createHmac('sha256', secretKey)
.update(payload, 'utf8')
.digest('hex');
@ -13,6 +22,16 @@ export function generateHmacSha256(data: string | object, secretKey: string): st
/**
* ตรวจสอบความถูกต้องของ HMAC (ใช้ timingSafeEqual เพื่อป้องกัน Timing Attacks)
*/
/**
* ฟังก์ชันสำหรับตรวจสอบลายเซ็น (Verify) ด้วย Ed25519 Public Key (Hex)
* @param data ข้อความดิบที่ใช้ในการเซ็น (string หรือ Buffer)
* @param secretKey secret ที่นำมาใช้ตรวจสอบ
* @param expectedHmac ลายเซ็นในรูปแบบ Hex String (ความยาว 64 ตัวอักษร)
* @returns ผลลัพธ์ความถูกต้อง (true = ผ่าน, false = ไม่ผ่าน)
*/
export function verifyHmacSha256(data: string | object, secretKey: string, expectedHmac: string): boolean {
const calculatedHmac = generateHmacSha256(data, secretKey);
const a = Buffer.from(calculatedHmac, 'hex');