Hmac Sha256不正确的结果值AWS Java

Hmac Sha256不正确的结果值AWS Java,java,amazon-web-services,hmac,sha256,Java,Amazon Web Services,Hmac,Sha256,因此,我试图用sha256为AWS计算这个hmac,但我没有得到预期的结果,即使这个例子取自AWS官方文档:唯一改变的是算法,它没有破坏程序,所以它应该可以工作,但它没有 我得到的结果是:k1T/qvVoXgEvmdFhTEh71vLDznqEVCyKcslA5RRSB6s= 我期望的结果是:M/y0+EAFFGaUAp4bWv/WEuXYah99pVsxvqtAuC8YN7I= 有人知道怎么回事吗?这可能与新行字符的解释有关\n可以是cr、lf或cr lf,具体取决于您的操作系统。AWS使用两

因此,我试图用sha256为AWS计算这个hmac,但我没有得到预期的结果,即使这个例子取自AWS官方文档:唯一改变的是算法,它没有破坏程序,所以它应该可以工作,但它没有

我得到的结果是:k1T/qvVoXgEvmdFhTEh71vLDznqEVCyKcslA5RRSB6s= 我期望的结果是:M/y0+EAFFGaUAp4bWv/WEuXYah99pVsxvqtAuC8YN7I=


有人知道怎么回事吗?

这可能与新行字符的解释有关\n可以是cr、lf或cr lf,具体取决于您的操作系统。

AWS使用两个不同的HMAC函数,第一个返回字符串表示,另一个返回二进制表示。这是我用OpenSSL实现的C++,希望有帮助:

public static void main(String[] args) throws SignatureException {
    String data = "GET"+"\n"+"webservices.amazon.com"+"\n"+"/onca/xml"+"\n"+"AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&ItemId=0679722769&Operation=ItemLookup&ResponeGroup=ItemAttributes%2COffers%2CImages%2CReviews&Service=AWSECommerceService&Timestamp=2009-01-01T12%3A00%3A00Z&Version=2009-01-06";
    String key = "1234567890";
    String result = calculateRFC2104HMAC(data, key);
    System.out.println(result);

}

private static final String HMAC_SHA_ALGORITHM = "HmacSHA256";


public static String calculateRFC2104HMAC(String data, String key)throws java.security.SignatureException{
    String result;
    try {

    // get an hmac_sha256 key from the raw key bytes
    SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"), HMAC_SHA_ALGORITHM);

    // get an hmac_sha256 Mac instance and initialize with the signing key
    Mac mac = Mac.getInstance(HMAC_SHA_ALGORITHM);
    mac.init(signingKey);

    // compute the hmac256 on input data bytes
    byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));

    // base64-encode the hmac256
    result = Base64.encodeBase64String(rawHmac);

    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }
    return result;
    }
string-hmacHex(字符串键,字符串消息)
{
无符号字符散列[32];
HMAC_CTX HMAC;
HMAC_CTX_init(&HMAC);
HMAC_Init_ex(&HMAC,&key[0],key.length(),EVP_sha256(),NULL);
HMAC_Update(&HMAC,(unsigned char*)&msg[0],msg.length());
无符号整数len=32;
HMAC_Final(HMAC、hash和len);
HMAC_CTX_清理(&HMAC);
std::stringstream-ss;
党卫军
string hmacHex(string key, string msg)
{
    unsigned char hash[32];

    HMAC_CTX hmac;
    HMAC_CTX_init(&hmac);
    HMAC_Init_ex(&hmac, &key[0], key.length(), EVP_sha256(), NULL);
    HMAC_Update(&hmac, (unsigned char*)&msg[0], msg.length());
    unsigned int len = 32;
    HMAC_Final(&hmac, hash, &len);
    HMAC_CTX_cleanup(&hmac);

    std::stringstream ss;
    ss << std::hex << std::setfill('0');
    for (int i = 0; i < len; i++)
    {   
        ss << std::hex << std::setw(2)  << (unsigned int)hash[i];
    }

    return (ss.str());
}
string hmac(string key, string msg)
{
    unsigned char hash[32];

    HMAC_CTX hmac;
    HMAC_CTX_init(&hmac);
    HMAC_Init_ex(&hmac, &key[0], key.length(), EVP_sha256(), NULL);
    HMAC_Update(&hmac, ( unsigned char* )&msg[0], msg.length());
    unsigned int len = 32;
    HMAC_Final(&hmac, hash, &len);
    HMAC_CTX_cleanup(&hmac);

    std::stringstream ss;
    ss << std::setfill('0');
    for (int i = 0; i < len; i++)
    {
        ss  << hash[i];
    }

    return (ss.str());
}