2012-02-25 17 views
8

Sto facendo un processo di caricamento file. Voglio generare gli hash SHA256 e CRC32. Qualcuno può aiutarmi come posso generare quegli hash? Voglio farlo funzionare per iOS.Come generare SHA256 e CRC32 in ios

+2

Hai provato a cercarlo su google? – Dani

+0

@Dani si l'ho fatto. – NSCry

risposta

32

SHA256 è disponibile in CommonCrypto. CRC32 non è un hash, è un controllo di ridondanza ciclico.

codice

Esempio:

#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 uscita:
DataIn: < 4e6f7720 69.732.074 68.652.074 696d6520 666f7220 616c6c20 676f6f64 20636f6d 70.757.465 72.732.074 6f20636f 6d652074 6f207468 65.206.169 64206f66 20.746.865 6972206d 61.737.465 72732e>

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

+0

ok. Ma voglio ottenere il valore CRC32 di qualsiasi dato. C'è un modo per ottenerlo in iOS? – NSCry

+0

Voglio lavorarlo per iOS. In iOS non trovo nel framework securty – NSCry

+0

Sì, Apple si sta muovendo verso le trasformazioni ma l'ho appena testato con l'SDK di iOS 5.0. Per quanto riguarda crc32, è disponibile in diverse librerie come zlib. – zaph

-4

non ci sono applicazioni in grado di generare hash per iOS

Questo dovrebbe funzionare .... proprio per Mac

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

+5

Bel tentativo di essere d'aiuto Yagnesh, ma penso che il poster originale chieda come generare numeri SHA256 e CRC32 a livello di programmazione per il proprio programma che esegue il caricamento. –

+0

@MichaelDautermann sì hai ragione. hai qualche suggerimento ?? – NSCry

+0

tu [potresti avere alcuni suggerimenti utili qui in questa domanda/risposta correlata] (http://stackoverflow.com/questions/1847281/commoncrypto-is-no-longer-part-of-the-iphone-sdk-where-else -can-i-easily-get-a), @ARC. –

1

Per entrambi, si ca n utilizzare questa sostanza:

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

E un esempio

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

Meraviglioso. Grazie. – Drakes

0

Questo metodo genererà crc32c usati per gcloud su iOS da un filepath. Se si desidera che lo standard crc32 annulli semplicemente l'altro valore per CRC32_POLYNOMIAL.

Legge il file indicato in blocchi da 512 KB, quindi può essere utilizzato su file di grandi dimensioni.

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