节点HmacSHA1种子 [英] Node HmacSHA1 Seed

查看:79
本文介绍了节点HmacSHA1种子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Node发送SOAP请求,并与受WSS保护的服务进行通信.

I'm trying to send a SOAP request via Node, talking to a service which is secured with WSS.

我需要使用SignedInfo元素对XML响应进行签名,这要求我将生成的Nonce Binary机密与从初始令牌请求返回的Nonce二进制机密(PSHA1格式)结合起来.

I need to sign the XML response with a SignedInfo element which requires me combining a Nonce Binary secret I generated, with a Nonce binary secret returned from the initial token request - PSHA1 format.

通过使用以下类,我已经能够使用Java进行验证(其中秘密是我的客户现时,种子是服务器现时):

I've been able to validate this using Java, by utilising the following class (Where the secret is my client nonce and the seed is the server nonce):

使用以下Java代码:

With the following Java code:

Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec key = new SecretKeySpec(getSharedKey(), "HmacSHA1");
mac.init(key);

String bytesToSign = "<XML_TO_SIGN_GOES_HERE>";

String signature = Base64.encodeBytes(mac.doFinal(bytesToSign.getBytes())); 

尽管我需要在Node项目中执行此操作,但我已经研究了Crypto API和许多插件,但无法生成相同的签名.

I need to do this in a Node project though, I've looked at the Crypto API and numerous plugins but I'm unable to generate the same signature.

如何使用节点为HmacSHA1指定种子?

How do I specify a seed for a HmacSHA1 using node?

推荐答案

最后,我设法到达那里,有一个名为psha1(

I managed to get there in the end, there's an NPM module called psha1 (https://www.npmjs.com/package/psha1).

使用该库,我创建了以下generateSignature模块,其外观如下:

Using that library I created the following a generateSignature module which looks as follows:

const crypto = require('crypto');
const psha1 = require('psha1');

export const generateSignatureValue = ({
  clientSecret,
  serverSecret,
  messageToSign,
}) => {

  const secretKey =
    psha1(clientSecret, serverSecret, 256);

  const hash =
    crypto
      .createHmac('sha1', Buffer.from(secretKey, 'base64'))
      .update(messageToSign)
      .digest('binary');

  return Buffer
    .from(hash, 'binary')
    .toString('base64');
};

export default generateSignatureValue;

这给了我想要的输出:)

This gives me the desired output :)

这篇关于节点HmacSHA1种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆