时间:2019-03-17 标签:c#vsjavaHMACSHA1
我正在尝试在 C# 中实现这个 Java SHA1。不知道为什么我的 C# 结果不同。
我犹豫是否要发布创建哈希的密钥和数据,尽管填写任何值都会在每个实现中产生不同的结果。
Java:
String key = "key";
String data = "data";
String algorithm = "HmacSHA1";
Charset charset = Charset.forName("utf-8");
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), algorithm);
Mac mac = Mac.getInstance(algorithm);
mac.init(signingKey);
return new String(Base64.encodeBase64(mac.doFinal(data.getBytes(charset))), charset);
产生:g1SZb73Vg6bV7VhFQ2KkCLy98rE=
C#:
String key = "key";
String data = "data";
byte[] aKey = UTF8Encoding.UTF8.GetBytes(key);
byte[] aData = UTF8Encoding.UTF8.GetBytes(data);
HMACSHA1 oEncode = new HMACSHA1(aKey);
byte[] aBytes = oEncode.ComputeHash(aData);
return Convert.ToBase64String(aBytes);
产生:50Hg/Hsq4v+iYrXJYpxxHE5q3RU=
I'm trying to implement this Java SHA1 in C#. Not sure why my results in C# are different.
I'm hesitant to post the key and data which create the hash, though filling in any value should produce differing results in each implementation.
Java:
String key = "key";
String data = "data";
String algorithm = "HmacSHA1";
Charset charset = Charset.forName("utf-8");
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), algorithm);
Mac mac = Mac.getInstance(algorithm);
mac.init(signingKey);
return new String(Base64.encodeBase64(mac.doFinal(data.getBytes(charset))), charset);
produces: g1SZb73Vg6bV7VhFQ2KkCLy98rE=
C#:
String key = "key";
String data = "data";
byte[] aKey = UTF8Encoding.UTF8.GetBytes(key);
byte[] aData = UTF8Encoding.UTF8.GetBytes(data);
HMACSHA1 oEncode = new HMACSHA1(aKey);
byte[] aBytes = oEncode.ComputeHash(aData);
return Convert.ToBase64String(aBytes);
produces: 50Hg/Hsq4v+iYrXJYpxxHE5q3RU=
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论