当我试图签署一项交易时,我收到了这样的信息:
Transaction payment = new Transaction();
BitcoinSecret PaymentSecret = new BitcoinSecret("1sXCvdpXz...UqkXW9mvT");
...
payment.Sign(Container.PaymentSecret, false);我深入研究了开源NBitcoin API并计算出了这些行,给出了错误消息。我能做什么?(https://github.com/NicolasDorier/NBitcoin/blob/master/NBitcoin/Crypto/DeterministicECDSA.cs)
try
{
hmac = MacUtilities.GetMac(macName);
}
catch(SecurityUtilityException nsae)
{
throw new InvalidOperationException(nsae.Message, nsae);
}发布于 2015-04-29 02:36:47
问题就在这里:
NBitcoin/NBitcoin.BouncyCastle/security/MacUtilities.cs
public static IMac GetMac(string algorithm)
{
...
if(mechanism.StartsWith("HMAC"))
{
...
}
...
}机制字符串是"HMACSHA256“,if语句对我来说永远不算真,因为我的语言是匈牙利语,"CS”是匈牙利语中的一个字母。因此,根据StartsWith函数,"HMACSHA256“不以匈牙利语中的"HMAC”开头。
尼古拉斯·多里尔( Nicolas )在NBitcoin API中修正了这个问题,将StringComparison.OrdinalIgnoreCase设置添加到StartWith函数中。
如果有人想测试,这里有一封来自Nicolas的电子邮件:
好吧,为了历史。
Thread.CurrentThread.CurrentCulture = new CultureInfo("hu");
string mechanism = "HMACSHA256";
var v1 = mechanism.StartsWith("HMAC");
mechanism = "HMAC-SHA256";
var v2 = mechanism.StartsWith("HMAC");在匈牙利语中,v1是假的,v2是真的。
发布于 2015-04-28 16:47:19
如果有人想确切地知道到底发生了什么,下面是引起bug的代码片段:
string mechanism = "HMACSHA256";
if (mechanism.StartsWith("HMAC"))
{
Console.WriteLine("good");
}
else
{
Console.WriteLine("bad");
}
Console.ReadLine();如果您设置了机制=“HMAC- set 256”,那么错误就不会发生。如果您使用mechanism.StartsWith("HMAC",StringComparison.InvariantCulture),那么bug就不会发生。
我还修复了github中的bug,并为github创建了一个对NBitcoin API的拉请求,因此希望将来不会发生在其他人身上。
发布于 2015-04-26 06:40:31
在HMAC之后有一个连字符。HMAC-SHA256 256
https://stackoverflow.com/questions/29874122
复制相似问题