Я пишу функцию, которая берет ключ и URL и генерирует подпись.Я использую hmac-sha1.Но я получил разные подписи между кодом Android и объективом-c:
Objective-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;
}
Android:
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;
}