douwen8118 2018-01-20 03:28
浏览 145
已采纳

无法使HMACsha256产生预期的结果

I'm trying to hook into the Binance API and have discovered that my implementation of HMACsha256 isn't producing the results that the sample documentation (and doing it in the command line) can. I'm trying to improve my knowledge of Go, but can't seem to crack this one.

Here's the documentation (Relevant Sample at "SIGNED Endpoint Examples for POST /api/v1/order"): https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md

Without looking at the doc, here is the gist, my key is the following:

NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j

my value is the following:

symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559

When I use the terminal with the following command:

echo -n "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559" | openssl dgst -sha256 -hmac "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"

I receive the result

c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71

Which is what the documentation suggests. But when I use the following go program:

package main

import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/base64"
    "fmt"
)

func main() {
    docSecret := "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"
    docQuery := "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559"
    result := hmacify(docQuery, docSecret)
    fmt.Println(result)
}

func hmacify(message string, secret string) string {
    key := []byte(secret)
    h := hmac.New(sha256.New, key)
    h.Write([]byte(message))
    return base64.StdEncoding.EncodeToString(h.Sum(nil))
}

produces the result:

yNtWglrnHW15RHhJ5hcRX0qSD6Ks3KsrBTxLKDi9a3E=

What am I misunderstanding that's causing such a massive difference from the sample and command line?

  • 写回答

2条回答 默认 最新

  • donglian5309 2018-01-20 04:21
    关注

    You got the same result in both cases. It's just encoded differently. The openssl command encodes it as hex and your function uses base64.StdEncoding.EncodeToString() to encode it as base64.

    Here's a small Python 2 snippet showing this:

    >>> x = "c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71"
    >>> y = "yNtWglrnHW15RHhJ5hcRX0qSD6Ks3KsrBTxLKDi9a3E=".decode('base64').encode('hex')
    >>> y
    'c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71'
    >>> x == y
    True
    

    To get the exact same result as the command line use hex.EncodeToString() as @Anuruddha suggested.

    import "encoding/hex"
    
    func hmacify(message string, secret string) string {
        key := []byte(secret)
        h := hmac.New(sha256.New, key)
        h.Write([]byte(message))
        return hex.EncodeToString(h.Sum(nil))  // <--- change is HERE
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作