You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SM3 copy.js 933B

123456789101112131415161718192021222324252627282930313233343536
  1. const SM3 = require("../miniprogram_npm/sm-crypto/index.js");
  2. /**
  3. * SM3 哈希函数,支持HMAC模式
  4. * @param {string} message - 需要哈希的消息
  5. * @param {string} key - 可选的密钥,用于HMAC模式
  6. * @returns {string} 返回大写的哈希值
  7. */
  8. function sm3(message, key) {
  9. try {
  10. console.log('SM3 哈希开始,消息:', message);
  11. console.log('SM3 密钥:', key);
  12. // 在小程序环境中直接使用字符串,不使用Buffer
  13. if (key) {
  14. // HMAC-SM3 模式
  15. const hmac = SM3.sm3(message, {
  16. key: key
  17. });
  18. console.log('HMAC-SM3 结果:', hmac);
  19. return hmac?.toUpperCase();
  20. } else {
  21. // 普通SM3哈希
  22. const hash = SM3.sm3(message);
  23. console.log('SM3 哈希结果:', hash);
  24. return hash?.toUpperCase();
  25. }
  26. } catch (error) {
  27. console.error('SM3 哈希失败:', error);
  28. throw error;
  29. }
  30. }
  31. module.exports = {
  32. sm3: sm3
  33. }