简体   繁体   English

如何将这种签名方法从crypto(node)转换为crypto-js(browser)?

[英]How to convert this signature method from crypto (node) to crypto-js (browser)?

I have a signature method that is meant to be used in Node.js but I'd like to implement it client-side with crypto-js. 我有一个打算在Node.js中使用的签名方法,但是我想在客户端使用crypto-js实现它。 It should work in latest Chrome versions. 它应该可以在最新的Chrome版本中使用。

I have tried to follow some answers like this one: Decode a Base64 String using CryptoJS 我试图遵循这样的一些答案: 使用CryptoJS解码Base64字符串

But I either get errors such as "Error: Malformed UTF-8 data", or a different result than the expected hmacDigest. 但是我要么收到诸如“错误:UTF-8格式错误的数据”之类的错误,要么得到与预期的hmacDigest不同的结果。

I am not sure how I could find an alternative to the "binary" digest although I found this question: How to get digest representation of CryptoJS.HmacSHA256 in JS 我不确定如何找到“二进制”摘要的替代方法,尽管我发现了以下问题: 如何在JS中获取CryptoJS.HmacSHA256的摘要表示形式

The method is supposed to answer the following: 该方法应回答以下问题:

"Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key" “使用(URI路径+ SHA256(即席+ POST数据))的HMAC-SHA512和base64解码的API密钥进行消息签名”

This is the Nodejs version (with crypto): 这是Nodejs版本(带有加密):

const crypto = require('crypto')

function sign(path, params, secret) {
  const message = querystring.stringify(params)
  const secretBase64 = Buffer.from(secret, 'base64')
  const hash = crypto.createHash('sha256')
  const hmac = crypto.createHmac('sha512', secretBase64)

  const hashDigest = hash.update(params.nonce + message).digest('binary')
  const hmacDigest = hmac.update(path + hashDigest, 'binary').digest('base64')

  return hmacDigest
}

note: querystring is just an helper module that can also run in browsers: https://nodejs.org/api/querystring.html 注意:querystring只是一个辅助模块,也可以在浏览器中运行: https : //nodejs.org/api/querystring.html

This is my attempt (wip) at implementing with crypto-js: 这是我尝试(crypt)用crypto-js实现:

import cryptojs from 'crypto-js')

function sign (path, params, secret) {
  const message = querystring.stringify(params)
  const secretParsed = cryptojs.enc.Base64.parse(secret)
  const secretDecoded = cryptojs.enc.Utf8.stringify(secretParsed) // -> this throws an error as "Error: Malformed UTF-8 data"

  const hash = cryptojs.SHA256(params.nonce + message).toString(cryptojs.enc.hex)
  const hmac = cryptojs.HmacSHA512(path + hash, secretDecoded).toString(cryptojs.enc.hex)
  return hmac
}

Try this ! 尝试这个 ! I think this is what you looking for ! 我认为这就是您要寻找的!

const crypto = require("crypto")

const sign = (path, params, secret) => {
    const message = querystring.stringify(params)
    const secret = Buffer.from(secret, 'base64')
    let sign = params.nonce + message
    let hash = crypto.createHmac('sha256', secret)
                     .update(sign)
                     .digest("base64").toString()

    let encoded = encodeURIComponent(hash)
    return encoded
}

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

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