简体   繁体   English

在 Node.js 中使用 Base-64 PKCS-8 进行签名/解码

[英]Signing/Decoding with Base-64 PKCS-8 in Node.js

Currently working to try to get connected with the Walmart Partners API, and having some issues with their cryptography, and I have not found any documented assistance for this in Javascript/Node.目前正在努力尝试与 Walmart Partners API 建立联系,并且他们的密码学存在一些问题,我在 Javascript/Node.js 中没有找到任何文档化的帮助。

The documentation states:文档指出:

Sign the byte array representation of this data by:通过以下方式签署此数据的字节数组表示:

Decoding the Base-64, PKCS-8 representation of your Private Key.解码您的私钥的 Base-64、PKCS-8 表示。 Note that the key is encoded using PKCS-8.请注意,密钥是使用 PKCS-8 编码的。 Libraries in various languages offer the ability to specify that the key is in this format and not in other conflicting formats such as PKCS-1.各种语言的库提供了指定密钥采用这种格式而不是其他冲突格式(如 PKCS-1)的能力。

Use this byte representation of your key to sign the data using SHA-256 with RSA.使用您的密钥的此字节表示使用 SHA-256 和 RSA 对数据进行签名。

Encode the resulting signature using Base-64.使用 Base-64 对生成的签名进行编码。

I have the private key, and found the "chilkat" package that is nicely documented.我有私钥,并找到了很好记录的“chilkat”包。 Only issue is our development system is Windows, and there's an issue with it on Windows 10 64bit.唯一的问题是我们的开发系统是 Windows,在 Windows 10 64 位上存在问题。 If I use the 64bit, it does not identify as a 64bit system, so I am not able to even get the package installed.如果我使用 64 位,它不会识别为 64 位系统,因此我什至无法安装该软件包。

Also, it seems the chilkat package requires at least Node version 4, we attempted upgrading to Node 4 to use this, and the project is currently not able to use that version without some bugs as a result of some other packages we use.此外,似乎 chilkat 包至少需要 Node 版本 4,我们尝试升级到 Node 4 以使用它,并且由于我们使用了一些其他包,该项目目前无法在没有一些错误的情况下使用该版本。 So at least until those bugs get resolved, Node 4 is not an option, which seems that Chilkat is out of the picture...所以至少在这些错误得到解决之前,节点 4 不是一个选择,这似乎奇尔卡特不在画面中......

So I found anotehr NPM package to assist with this: https://github.com/rzcoder/node-rsa所以我找到了 anotehr NPM 包来帮助解决这个问题: https : //github.com/rzcoder/node-rsa

Using that package, I'm having issues getting the decoding/signing to work... I'm finding the documentation quite confusing, and the only assistance I have found with similar questions was in C#...使用那个包,我在解码/签名工作时遇到问题......我发现文档很混乱,我发现类似问题的唯一帮助是在 C# 中......

Any advice on how I can achieve what Walmart is asking for with the node-rsa package?关于如何使用 node-rsa 包实现沃尔玛要求的任何建议?

Using node-rsa , I was able to generate a valid Walmart API signature in two steps:使用node-rsa ,我能够分两步生成有效的 Walmart API 签名:

const NodeRSA = require('node-rsa');

// 1. Decode the private key with base64 then pkcs8
const key = new NodeRSA();
key.importKey(new Buffer(encodedPrivateKey, 'base64'), 'pkcs8-private-der');
const privateKey = key.exportKey();

// 2. Sign the data with the decoded private key and sha256 then encode it with base64
const data = `${consumerId}\n${url}\n${method}\n${timestamp}\n`;
const signature = new NodeRSA(privateKey, {signingScheme: 'sha256'}).sign(data).toString('base64');

Using node-rsa , solution for decrypt.使用node-rsa ,解密解决方案。

const NodeRSA = require('node-rsa')
var key = NodeRSA(privateKey, 'pkcs8-private-pem', {'environment': 'node', 'encryptionScheme': 'pkcs1', 'signingScheme': 'sha256'}); 
var data = key.decrypt(encrypted, 'utf-8')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM