search

Home  >  Q&A  >  body text

objective-c - 为什么我们iOS加密出来的和安卓加密出来的不同?

安卓加密出来的是[B@42a6f4d8 我们iOS加密出来的是6132896cb90f07ff8f5313990324016816d54f6b

还有

baseStr = [[NSString alloc] initWithData:[QNUrlSafeBase64 decodeString:HIMAStr] encoding:NSUTF8StringEncoding]; [QNUrlSafeBase64 decodeString:HIMAStr]是有值的 但是返回出来却是nil这是为什么呀?这是我的代码 请大神们指点下!感激不尽!!!

滿天的星座滿天的星座2799 days ago676

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-25 09:05:48

    Luckily, I happened to be using Qiniu recently:

    - (NSString *)genToken
    {
        static NSUInteger expireTime = 0;
        static NSString *cachedSign = nil;
        NSUInteger now = (NSUInteger)[NSDate date].timeIntervalSince1970;
        if (now > expireTime) {
            expireTime = now + 3600 - 60;
            NSDictionary *policy = @{
                                     @"scope": _bucket,
                                     @"deadline": @(expireTime + 60),
                                     @"returnBody": @"{\"key\":$(key),\"name\":$(fname),\"size\":$(fsize),\"w\":$(imageInfo.width),\"h\":$(imageInfo.height),\"hash\":$(etag)}"
                                     };
            NSString *encodedPolicy = [[NSJSONSerialization dataWithJSONObject:policy
                                                                       options:0
                                                                         error:NULL] base64EncodedStringWithOptions:0];
            encodedPolicy = [encodedPolicy stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
            encodedPolicy = [encodedPolicy stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    
            const char *text = [encodedPolicy cStringUsingEncoding:NSASCIIStringEncoding];
            const char *key = [_secretKey cStringUsingEncoding:NSASCIIStringEncoding];
            char cHMAC[CC_SHA1_DIGEST_LENGTH];
            CCHmac(kCCHmacAlgSHA1, key, strlen(key), text, strlen(text), cHMAC);
    
            NSString *sign = [[NSString alloc] initWithData:[NSData dataWithBytes:cHMAC
                                                                           length:sizeof(cHMAC)]
                                                   encoding:NSASCIIStringEncoding];
            NSString *encodedSign = sign.toBase64String;
            encodedSign = [encodedSign stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
            encodedSign = [encodedSign stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    
            cachedSign = [NSString stringWithFormat:@"%@:%@:%@", _accessKey, encodedSign, encodedPolicy];
        }
        return cachedSign;
    }

    reply
    0
  • Cancelreply