搜尋

首頁  >  問答  >  主體

objective-c - 為什麼我們iOS加密出來的和安卓加密出來的不同?

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

還有

baseStr = [[NSString alloc] initWithData:[QNUrlSafeBase64 decodeString:HIMAStr] encoding:NSUTF8StringEncoding]; [QNUrlSafeBase64 decodeString:HIMAStr]是有值的呀回來卻是nil 但是為什麼這到底是為什麼?這是我的程式碼 請大神們指著!感激不盡! ! !

滿天的星座滿天的星座2799 天前677

全部回覆(1)我來回復

  • PHPz

    PHPz2017-04-25 09:05:48

    運氣好,剛好最近也用到七牛:

    - (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;
    }

    回覆
    0
  • 取消回覆