You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将嵌套HMAC的Go代码转换为Node.js实现?

Node.js实现嵌套HMAC(对应Go语言自定义哈希函数场景)

我已掌握普通HMAC的Go到Node.js转换方法,但遇到嵌套HMAC的Go代码场景:Go的hmac.New允许传入自定义哈希函数(此处为内层HMAC),但Node.js的crypto.createHmac仅支持传入字符串类型的哈希算法名称,不知道如何实现相同功能。

Go代码示例

package main

import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/hex"
    "fmt"
    "hash"
)
func main() {
    hmacf := hmac.New(func() hash.Hash {
        return hmac.New(sha256.New, []byte("inner-key1"))
    }, []byte("inner-key2"));

    hmacf.Write([]byte("message"))
    fmt.Println(hex.EncodeToString(hmacf.Sum(nil)))
}

代码逻辑解析

这段Go代码的核心是嵌套HMAC:外层HMAC以「密钥为inner-key1的SHA256 HMAC」作为底层哈希函数,再用密钥inner-key2对消息message计算最终HMAC。

Node.js实现方案

由于Node.js的crypto.createHmac不支持直接传入自定义哈希函数,我们可以手动拆解HMAC的标准计算逻辑(HMAC本质是两次哈希运算的组合),模拟嵌套效果:

代码实现

const crypto = require('crypto');

// 模拟Go的hash.Hash接口,封装内层HMAC逻辑
class NestedHash {
  constructor(innerKey) {
    this.innerKey = innerKey;
    this.innerHmac = crypto.createHmac('sha256', innerKey);
  }

  // 写入数据到内层HMAC
  update(data) {
    this.innerHmac.update(data);
    return this;
  }

  // 获取内层HMAC的计算结果
  sum(digest) {
    return this.innerHmac.digest(digest);
  }

  // 重置内层HMAC状态(HMAC计算需要两次独立的哈希实例)
  reset() {
    this.innerHmac = crypto.createHmac('sha256', this.innerKey);
    return this;
  }
}

// 实现嵌套HMAC的计算,对应Go代码逻辑
function nestedHmac(innerKey, outerKey, message) {
  const blockSize = 64; // SHA256的块大小为64字节
  let outerKeyBuffer = Buffer.from(outerKey);

  // 处理外层密钥:长度超块大小则哈希,不足则补零到块大小
  if (outerKeyBuffer.length > blockSize) {
    outerKeyBuffer = crypto.createHash('sha256').update(outerKeyBuffer).digest();
  } else if (outerKeyBuffer.length < blockSize) {
    outerKeyBuffer = Buffer.concat([outerKeyBuffer, Buffer.alloc(blockSize - outerKeyBuffer.length)]);
  }

  // 生成ipad和opad
  const ipad = Buffer.alloc(blockSize, 0x36);
  const opad = Buffer.alloc(blockSize, 0x5c);
  for (let i = 0; i < blockSize; i++) {
    ipad[i] ^= outerKeyBuffer[i];
    opad[i] ^= outerKeyBuffer[i];
  }

  // 第一步:计算 (K XOR ipad) + message 的内层HMAC结果
  const hash1 = new NestedHash(innerKey);
  hash1.update(ipad).update(message);
  const hash1Result = hash1.sum();

  // 第二步:计算 (K XOR opad) + hash1Result 的内层HMAC结果
  const hash2 = new NestedHash(innerKey);
  hash2.update(opad).update(hash1Result);
  return hash2.sum('hex');
}

// 测试验证
const result = nestedHmac('inner-key1', 'inner-key2', 'message');
console.log(result);

结果验证

运行上述Node.js代码,输出结果与Go代码完全一致,说明实现逻辑正确。

内容的提问来源于stack exchange,提问作者zizifn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 03:05:26