2012-02-25 95 views
8

我在做一個文件上傳工作。我想生成SHA256和CRC32哈希。任何人都可以幫助我如何生成這些散列?我想讓它在iOS上運行。如何在ios中生成SHA256和CRC32

+2

你試過用google搜索嗎? – Dani 2012-02-25 13:08:09

+0

@Dani是的,我做到了。 – NSCry 2012-02-25 16:01:04

回答

32

SHA256可在CommonCrypto。 CRC32不是散列,它是循環冗餘校驗。

示例代碼:

#import <CommonCrypto/CommonDigest.h> 

NSData *dataIn = [@"Now is the time for all good computers to come to the aid of their masters." dataUsingEncoding:NSASCIIStringEncoding]; 
NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH]; 

CC_SHA256(dataIn.bytes, dataIn.length, macOut.mutableBytes); 

NSLog(@"dataIn: %@", dataIn); 
NSLog(@"macOut: %@", macOut); 

的NSLog輸出:
DATAIN:< 4e6f7720 69732074 68652074 696d6520 666f7220 616c6c20 676f6f64 20636f6d 70757465 72732074 6f20636f 6d652074 6f207468 65206169 64206f66 20746865 6972206d 61737465 72732e>

macOut:< 53f89cf6 7ebfbe56 89f1f76a 3843dfd1 09d68c5b a938dcd2 9a12004e 108260cb>

+0

ok.But我想獲得任何數據的CRC32值。有沒有辦法讓它在ios? – NSCry 2012-02-25 15:50:07

+0

我想爲ios工作。ios我在安全框架中找不到 NSCry 2012-02-25 15:55:13

+0

是的,Apple正在轉型,但我只是用iOS 5.0 SDK進行了測試。至於crc32,它可以在幾個庫中使用,例如zlib。 – zaph 2012-02-25 17:16:04

-4

沒有應用程序可以生成IOS

這應該哈希....它爲Mac

http://itunes.apple.com/us/app/digiprint/id473233587?mt=12

+5

很好的嘗試,以幫助Yagnesh,但我認爲原來的海報是要求如何生成SHA256和CRC32號碼編程爲他自己的程序,上傳。 – 2012-02-25 13:22:37

+0

@MichaelDautermann是的你是對的。你有什麼建議? – NSCry 2012-02-25 15:52:52

+0

你[在這個相關的問題/答案可能會得到一些有用的提示](http://stackoverflow.com/questions/1847281/commoncrypto-is-no-longer-part-of-the-iphone-sdk-where-else -can-i-easily-get-a),@ARC。 – 2012-02-25 16:10:57

1

對於這兩種情況,您都可以n使用該要旨:

https://gist.github.com/paul-delange/6808278

和示例

NSString* crc32 = (__bridge NSString*)TGDFileHashCreateWithPath((__bridge CFStringRef)filepath, TGDFileHashDefaultChunkSizeForReadingData, TGDChecksumAlgorithmCRC32); 
+0

太棒了。謝謝。 – Drakes 2015-08-05 09:11:14

0

此方法作爲從文件路徑使用gcloud iOS上會產生CRC32C。如果您希望標準crc32僅取消註釋CRC32_POLYNOMIAL的其他值。

它讀取512KB塊中給出的文件,因此可用於大型文件。

- (NSString*) crc32c:(NSString*)filepath{ 

    /// using crc code from 
    // http://classroomm.com/objective-c/index.php?action=printpage;topic=2891.0 
    // by rgronlie 


    //this is the standard crc32 polynomial 
    //uint32_t CRC32_POLYNOMIAL = 0xEDB88320; 

    //this is the crc32c one 
    uint32_t CRC32_POLYNOMIAL = 0x82F63B78L; 
    uint32_t CRC32C_SEED = 0xFFFFFFFFL; 

    // create and populate a lookup table 
    uint32_t* pCRCTable = malloc(sizeof(uint32_t) * 256); 

    for (uint32_t i = 0; i <= 255; i++) 
    { 
     uint32_t crc32 = i; 
     for (uint32_t j = 8; j > 0; j--) 
     { 
      if ((crc32 & 1) == 1) 
       crc32 = (crc32 >> 1)^CRC32_POLYNOMIAL; 
      else 
       crc32 >>= 1; 
     } 
     pCRCTable[i] = crc32; 
    } 

    // get a handle to the file 
    NSFileHandle *filehandle = [NSFileHandle fileHandleForReadingAtPath:filepath]; 

    if(filehandle == NULL){ 
     NSLog(@"failed to create file handle"); 
     return nil; 
    } 

    // a buffer to read into 
    NSData* databuffer; 

    uint32_t crc = CRC32C_SEED; 

    // read the file in chunks of 512KB 

    while(true){ 
     databuffer = [filehandle readDataOfLength: 512 * 1024]; 

     // if there is nothing left finish 
     if([databuffer length] == 0){ 
      break; 
     } 
     // otherwise run each chunk through the lookup table 
     uint8_t *pBytes = (uint8_t *)[databuffer bytes]; 
     uint32_t length = [databuffer length]; 

     while (length--) 
     { 
      crc = (crc>>8)^pCRCTable[(crc & 0xFF)^*pBytes++]; 
     } 
    } 

    // clean up 
    [filehandle closeFile]; 
    free(pCRCTable); 

    // this is the result 
    uint32_t hash = crc^0xFFFFFFFFL; 

    // reverse it for endianness 
    uint32_t hash_reversed = CFSwapInt32HostToBig(hash); 
    // as raw bytes 
    NSData* hash_data = [NSData dataWithBytes: &hash_reversed length: sizeof(hash_reversed)]; 
    // return base64 encoded 
    return [hash_data base64EncodedStringWithOptions:0]; 
}