很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,访问帮助中心 /help/reopen-questions 。
我有一个 java 代码示例,用于使用 HMAC-SHA1 算法(RFC 2104)计算摘要,然后使用 Base64 编码(RFC 2045)进行编码。
这是java代码
public static String buildDigest(String key, String idString) throws SignatureException {
try {
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(idString.getBytes(charset))), charset);
} catch (Exception e) {
throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
}
}
我在 Stack Overflow 中找到了答案,所以这里是 C# 代码
private string EncodeHMAC(string input, byte[] key)
{
HMACSHA1 myhmacsha1 = new HMACSHA1(key);
byte[] byteArray = Encoding.UTF8.GetBytes(input);
// MemoryStream stream = new MemoryStream(byteArray);
var hashValue = myhmacsha1.ComputeHash(byteArray);
return hashValue.Aggregate("", (s, e) => s + String.Format("{0:x2}", e), s => s);
}
private string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes = System.Text.UTF8Encoding.UTF8.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
我没有得到正确的结果我正在关注的教程 http://api.summon.serialssolutions.com/help/api/authentication
尝试这个:
// This method will return the base 64 encoded string using the given input and key.
private string EncodeHMAC(string input, byte[] key)
{
HMACSHA1 hmac = new HMACSHA1(key);
byte[] stringBytes = Encoding.UTF8.GetBytes(input);
byte[] hashedValue = hmac.ComputeHash(stringBytes);
return Convert.ToBase64String(hashedValue);
}
我认为您没有正确地将哈希值转换为 Base 64 字符串。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)