import { createHash } from "node:crypto";
import { Buffer } from "node:buffer";
function hashData(data, algorithm = 'sha256') {
console.log(`使用 ${algorithm} 算法处理数据`);
const buffer = typeof data === 'string'
? Buffer.from(data)
: Buffer.from(JSON.stringify(data));
const hash = createHash(algorithm);
hash.update(buffer);
const digest = hash.digest('hex');
console.log(`哈希结果: ${digest}`);
return digest;
}
function encryptData(data, salt = 'default-salt') {
console.log(`加密数据,使用盐值: ${salt}`);
const combined = `${data}:${salt}:${Date.now()}`;
const sha256Hash = hashData(combined, 'sha256');
const md5Hash = hashData(combined, 'md5');
return {
original: data,
salt,
sha256: sha256Hash,
md5: md5Hash,
timestamp: new Date().toISOString()
};
}
export default async function handler(input) {
console.log("开始处理加密任务");
const data = input.data || "这是需要加密的默认数据";
const salt = input.salt || "custom-salt-" + Math.floor(Math.random() * 1000);
const encryptedResult = encryptData(data, salt);
return {
message: '加密处理完成',
result: encryptedResult
};
}