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
risposta
SHA256 è disponibile in CommonCrypto. CRC32 non è un hash, è un controllo di ridondanza ciclico.
codiceEsempio:
#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>
ok. Ma voglio ottenere il valore CRC32 di qualsiasi dato. C'è un modo per ottenerlo in iOS? – NSCry
Voglio lavorarlo per iOS. In iOS non trovo
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
non ci sono applicazioni in grado di generare hash per iOS
Questo dovrebbe funzionare .... proprio per Mac
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. –
@MichaelDautermann sì hai ragione. hai qualche suggerimento ?? – NSCry
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. –
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);
Meraviglioso. Grazie. – Drakes
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];
}
- 1. Il round trip SHA256
- 2. Come aggiornare Homebrew SHA256?
- 3. HMAC-SHA256 in Delphi
- 4. Inversione CRC32
- 5. Come usare sha256 in php5.3.0
- 6. Digest :: CRC32 with Zlib
- 7. CRC32 vs CRC32C?
- 8. Come calcolare sha256 checksum file in Go
- 9. SHA256 in stored procedure T-sql
- 10. SHA256 in Objective-C per iPhone
- 11. Calcola hash SHA256 in Android/Java e C#
- 12. HMAC SHA256 digest esadecimale di una stringa in Erlang, come?
- 13. SHA256 hash in Python 2.4
- 14. SHA256 preferisce numeri interi?
- 15. Genera sha256 con OpenSSL e C++
- 16. Modulo di crittografia Erlang - ECDSA, RSA, SHA256 e così via
- 17. Implementazione Crc32 C - non funziona
- 18. Come calcolare CRC32 con Python per abbinare i risultati online?
- 19. funzione non definita sha256()
- 20. asincrono SHA256 hashing
- 21. Oggetti hash sha256 persistenti?
- 22. SHA256 in Go e PHP che danno risultati diversi
- 23. Qual è la differenza tra crc32 e crc32b?
- 24. L'implementazione di base di CRC32 di Wikipedia differisce dallo standard CRC32 visto online
- 25. Quanto dura l'hash SHA256?
- 26. Come utilizzare le funzioni SHA256 di OpenSSL
- 27. Generazione di SHA256 in iphone/Objective C ...?
- 28. Calcolo Ethernet CRC32 - software vs risultato algoritmico
- 29. Come ottenere l'identificazione personale del certificato SHA256?
- 30. Ottimizzazione delle prestazioni SHA256 in C
Hai provato a cercarlo su google? – Dani
@Dani si l'ho fatto. – NSCry