123456789101112131415161718192021222324252627282930313233343536 |
- const SM3 = require("../miniprogram_npm/sm-crypto/index.js");
-
- /**
- * SM3 哈希函数,支持HMAC模式
- * @param {string} message - 需要哈希的消息
- * @param {string} key - 可选的密钥,用于HMAC模式
- * @returns {string} 返回大写的哈希值
- */
- function sm3(message, key) {
- try {
- console.log('SM3 哈希开始,消息:', message);
- console.log('SM3 密钥:', key);
-
- // 在小程序环境中直接使用字符串,不使用Buffer
- if (key) {
- // HMAC-SM3 模式
- const hmac = SM3.sm3(message, {
- key: key
- });
- console.log('HMAC-SM3 结果:', hmac);
- return hmac?.toUpperCase();
- } else {
- // 普通SM3哈希
- const hash = SM3.sm3(message);
- console.log('SM3 哈希结果:', hash);
- return hash?.toUpperCase();
- }
- } catch (error) {
- console.error('SM3 哈希失败:', error);
- throw error;
- }
- }
-
- module.exports = {
- sm3: sm3
- }
|