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.
 
 
 
 
 
 

14 line
562 B

  1. import 'dart:convert';
  2. import 'package:crypto/crypto.dart';
  3. class XunfeiUtils {
  4. // 使用SHA-256算法计算HMAC
  5. static String hmacSha256(String key, String message) {
  6. var keyBytes = utf8.encode(key); // 将密钥转为字节数组
  7. var messageBytes = utf8.encode(message); // 将消息转为字节数组
  8. var hmac = Hmac(sha256, keyBytes); // 创建 HMAC 对象,指定哈希算法和密钥
  9. var digest = hmac.convert(messageBytes); // 计算消息的哈希
  10. return base64.encode(digest.bytes); // 返回 base64 编码的哈希值
  11. }
  12. }