Hash F#将hmac python转换为F#

Hash F#将hmac python转换为F#,hash,f#,sha,Hash,F#,Sha,如何将此Python代码转换为F#” message=nonce+client\u id+api\u key signature=hmac.new(API_SECRET,msg=message,digestmod=hashlib.sha256).hexdigest().upper() 到目前为止,我的代码实现了这一点: let message =string(nonce)+ client_id + api_key let signature = HMACSHA256.Create(API_se

如何将此Python代码转换为F#”

message=nonce+client\u id+api\u key
signature=hmac.new(API_SECRET,msg=message,digestmod=hashlib.sha256).hexdigest().upper()
到目前为止,我的代码实现了这一点:

let message =string(nonce)+ client_id + api_key
let signature = HMACSHA256.Create(API_secret+message)
简单的问题是我找不到表示F#中的
hexdigest()
函数的内容


另外,我是否像我所做的那样将API_Secret与消息结合起来,还是必须将它们分开?

我想你需要这样的想法:

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

StringBuilder的部分是python中的
hexdigest
所做的(如果我正确理解了
hexdigest

我认为您需要这样的想法:

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

StringBuilder的部分是python中的
hexdigest
所做的(如果我得到
hexdigest
正确的话)

消息必须是字节数组吗?对于此方法,是的-您必须自己转换它-对于文本,最简单的方法是System.text.Encoding中的方法/类-例如,此方法使用UTF8编码字符串:
让utf8EncodedBytes=System.text.Encoding.UTF8.GetBytes myString
根据HMAC创建应为:
use hmac=新系统。安全。加密。HMACSHA256(秘密)
true-你会得到一个警告-这更像是一个惯例,但我会修正它-谢谢消息必须是一个字节数组吗?对于这个方法是的-你必须自己转换它-对于文本,最简单的方法是System.text.Encoding中的方法/类-例如,这个方法使用UTF8对字符串进行编码:
让utf8EncodedBytes=System.Text.Encoding.UTF8.GetBytes myString
根据HMAC的创建应该是:
使用HMAC=new System.Security.Cryptography.HMACSHA256(secret)
true-您会得到一个警告-这更像是一个约定,但我会修复它-谢谢