为什么我的Android和objective-c代码得到不同的hmac-sha1结果?

为什么我的Android和objective-c代码得到不同的hmac-sha1结果?,android,objective-c,base64,sha1,hmac,Android,Objective C,Base64,Sha1,Hmac,我正在编写一个函数,它接受密钥和url并生成签名。我正在使用hmac-sha1。但我在Android代码和objective-c代码之间得到了不同的签名: 目标C: - (NSString *)hmacsha1:(NSString *)url secretKey:(NSString *)secretKey { const char *cKey = [secretKey cStringUsingEncoding:NSUTF8StringEncoding]; const char

我正在编写一个函数,它接受密钥和url并生成签名。我正在使用hmac-sha1。但我在Android代码和objective-c代码之间得到了不同的签名:

目标C:

- (NSString *)hmacsha1:(NSString *)url secretKey:(NSString *)secretKey
{
    const char *cKey  = [secretKey cStringUsingEncoding:NSUTF8StringEncoding];
    const char *cData = [url cStringUsingEncoding:NSUTF8StringEncoding];

    unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];

    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC
                                          length:sizeof(cHMAC)];

    NSString *hash = [HMAC base64EncodedStringWithOptions:0];


    return hash;
}
安卓:

public static String hmacsha1(String url, String secretKey) throws
        UnsupportedEncodingException, NoSuchAlgorithmException,
        InvalidKeyException
{
    secretKey = secretKey.replace('-', '+');
    secretKey = secretKey.replace('_', '/');

    byte[] key = Base64.decode(secretKey, Base64.DEFAULT);

    SecretKeySpec sha1Key = new SecretKeySpec(key, "HmacSHA1");

    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(sha1Key);

    byte[] sigBytes = mac.doFinal(url.getBytes());

    String signature = Base64.encodeToString(sigBytes, Base64.DEFAULT);

    // convert the signature to 'web safe' base 64
    signature = signature.replace('+', '-');
    signature = signature.replace('/', '_');

    return signature;
}

我使用的方法生成了与android字符串完全相同的hmac-sha1字符串

- (NSData *)hmacForKeyAndData:(NSString *)key data:(NSString *)url
    {
        const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
        const char *cData = [url cStringUsingEncoding:NSASCIIStringEncoding];
        unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];
        CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
        return [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
    }

我在ios和android中也得到了不同的值。这方面的任何更新。有点紧急。你的问题是什么solved@poyo发烧你找到解决上述问题的方法了吗