iOS AES HMACSHA1之后 URL 返回Nil

首先感谢YYKit

原因很简单
NSURL *url = [NSURL URLWithString:stringURL];
url = nil;
导致bug 的原因是 加密算法有问题字符串之间有空格 和非法字符

iOS AES HMACSHA1之后 URL 返回Nil_第1张图片
bug.png
iOS AES HMACSHA1之后 URL 返回Nil_第2张图片
HMACSHA1.png

正确HMACSHA1加密算法 摘自YYKit

- (NSString *)HMACSHA1EncryptString:(NSString *)string {
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    size_t size;
    switch (kCCHmacAlgSHA1) {
        case kCCHmacAlgMD5: size = CC_MD5_DIGEST_LENGTH; break;
        case kCCHmacAlgSHA1: size = CC_SHA1_DIGEST_LENGTH; break;
        case kCCHmacAlgSHA224: size = CC_SHA224_DIGEST_LENGTH; break;
        case kCCHmacAlgSHA256: size = CC_SHA256_DIGEST_LENGTH; break;
        case kCCHmacAlgSHA384: size = CC_SHA384_DIGEST_LENGTH; break;
        case kCCHmacAlgSHA512: size = CC_SHA512_DIGEST_LENGTH; break;
        default: return nil;
    }
    unsigned char result[size];
    const char *cKey = [API_UPLOAD_KEY_HMAC_SHA1 cStringUsingEncoding:NSUTF8StringEncoding];
    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), data.bytes, data.length, result);
    NSMutableString *hash = [NSMutableString stringWithCapacity:size * 2];
    for (int i = 0; i < size; i++) {
        [hash appendFormat:@"%02x", result[i]];
    }
    return hash;
}

你可能感兴趣的:(iOS AES HMACSHA1之后 URL 返回Nil)