简体   繁体   English

F#将hmac python转换为f#

[英]F# converting hmac python into f#

How would I convert this Python code into F# " 我如何将这段Python代码转换为F#”

message = nonce + client_id + api_key
signature = hmac.new(API_SECRET, msg=message, digestmod=hashlib.sha256).hexdigest().upper()

My code so far to achieve this: 到目前为止,我的代码实现了这一点:

let message =string(nonce)+ client_id + api_key
let signature = HMACSHA256.Create(API_secret+message)

Simple issue is I cannot find what represents the hexdigest() function in F#. 一个简单的问题是我找不到F#中代表hexdigest()函数的内容。

Also, do I combine the API_Secret with the message as I have done or must they be separated? 另外,是否将API_Secret与消息结合起来,还是必须将它们分开?

I think you need somethink like this: 我认为您需要这样的想法:

let hexdigest (bytes : byte[]) =
   let sb = System.Text.StringBuilder()
   bytes |> Array.iter (fun b -> b.ToString("X2") |> sb.Append |> ignore)
   string sb

let signature =
   use hmac = new HMACSHA256.Create(API_secret)
   hmac.ComputeHash(message)
   |> hexdigest

The part with the StringBuilder is what hexdigest does in python (if I get hexdigest right) StringBuilder的部分是hexdigest在python中的作用(如果我正确使用了hexdigest

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

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