美文网首页iOS
iOS AES HMACSHA1之后 URL 返回Nil

iOS AES HMACSHA1之后 URL 返回Nil

作者: NieFeng1024 | 来源:发表于2016-11-18 16:51 被阅读66次

    首先感谢YYKit

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

    bug.png 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

        本文链接:https://www.haomeiwen.com/subject/mvcspttx.html