2011-01-17 14 views
11

Come creare uno SHA256 di una stringa in iphone/Objective C ...Generazione di SHA256 in iphone/Objective C ...?

Sha256 in Objective-C for iPhone

Ho letto this..but non sono in grado di capire questo ..

voglio creare un output simile a php funcation come segue: -

$hash = hash_hmac("sha256", implode(';', $hash_parameters), $api_key); 

cui parametri hash è la matrice di argomenti ...

Puoi scrivere questo come metodo che prenderà la stringa di input ...?

E quale sarà l'output del metodo NSData o NSString .. ??

Devo creare una richiesta con questo .. ??

Così l'oggetto della richiesta ..

[theRequest setHTTPBody:requestBody]; 

quello che dovrebbe essere il tipo di requestBody ??

+0

Questo è stato chiaramente risposto qui-> http://stackoverflow.com/questions/3709204/sha256-in-objective-c-for- iphone/11426583 # 11426583 – Jagadeeshwar

risposta

17

Non sono sicuro di comprendere appieno le tue domande, ma se stai cercando di creare una stringa con hash, puoi passare i tuoi parametri come argomenti a una funzione di hash.

-(void)generateHashedString { 

    NSString *key = @"Some random string"; 
    //enter your objects you want to encode in the data object 
    NSString *data = [NSString stringWithFormat:@"%@%@%@", @"sha256", hash_parameters, api_key]; 

    const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding]; 
    const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding]; 

    unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH]; 

    CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC); 

    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC 
              length:sizeof(cHMAC)]; 

    NSString *hash = [HMAC base64Encoding]; 

} 

Questo vi darà un NSString di hash che è possibile utilizzare per rendere le vostre richieste. NSLog(@"%@",hash); Per vedere cosa hai generato!

Assicurati di #import <CommonCrypto/CommonHMAC.h> troppo

+0

Ho provato questo, ma l'uscita è diversa dalla funzione php – Maysam

+0

Questo è codificato in base64.Puoi omettere la codifica base64 se non ne hai bisogno. – TALLBOY

+0

come posso convertire il tipo hmac in nsstring? – Maysam

2

ho trascorrere una giornata buco, cercando di convertire l'hash generato (byte) in dati leggibili. Ho usato la base64 codificata come la risposta sopra e non ha funzionato affatto per me (b.t.w. hai bisogno e un esterno .h per essere in grado di usare la codifica base64, che avevo).

Quindi quello che ho fatto è stato questo (che funziona perfettamente senza un .h esterna):

CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC); 

// Now convert to NSData structure to make it usable again 
NSData *out = [NSData dataWithBytes:cHMAC length:CC_SHA256_DIGEST_LENGTH]; 

// description converts to hex but puts <> around it and spaces every 4 bytes 
NSString *hash = [out description]; 
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""]; 
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""]; 
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""]; 
// hash is now a string with just the 40char hash value in it 
NSLog(@"%@",hash); 
3

non ho confrontare il seguente codice per l'uscita funzione PHP ma funziona per me:

+(NSString *)signWithKey:(NSString *)key usingData:(NSString *)data 
{ 
    const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding]; 
    const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding]; 

    unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH]; 

    CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC); 

    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)]; 

    return [[HMAC.description stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""]; 
} 

Fatemi sapere se era utile ...

1

Per questo un riferimento di hashing HMAC funziona su PHP.

- (NSString *)getToken:(NSString *)queryString 
{ 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; 
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
    NSString *dateString = [formatter stringFromDate:[NSDate date]]; 
    NSDate *dateTodayUTC = [formatter dateFromString:dateString]; 
    NSString *nowTimestamp = [NSString stringWithFormat:@"%.f", [dateTodayUTC timeIntervalSince1970]]; 

    NSString *hashCombinations = [[NSString alloc] initWithFormat:@"%@%@%.f", queryString, public_api_key, [dateTodayUTC timeIntervalSince1970]]; 

    const char *privateKey = [private_api_key cStringUsingEncoding:NSUTF8StringEncoding]; 
    const char *requestData = [hashCombinations cStringUsingEncoding:NSUTF8StringEncoding]; 
    unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH]; 

    //HmacSHA256 
    CCHmac(kCCHmacAlgSHA256, // algorithm 
      privateKey, strlen(privateKey), // privateKey 
      requestData, strlen(requestData), // requestData 
      cHMAC); // length 

    NSString *hash; 
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2]; 
    for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) 
     [output appendFormat:@"%02x", cHMAC[i]]; 
    hash = output; 

    NSString *base64HashString = [self base64String:hash]; 
    self.tokenLabel.text = hash; 

    NSLog(@"generated hash = %@", hash); 
    NSLog(@"base64 hash = %@", base64HashString); 
    NSLog(@"timestamp = %@ nsdate utc = %@", nowTimestamp, dateString); 
    NSLog(@"combinations %@", hashCombinations); 
    return [base64HashString urlencode]; 
} 

È possibile utilizzare questo metodo base64.

- (NSString *)base64String:(NSString *)str 
{ 
    NSData *theData = [str dataUsingEncoding: NSASCIIStringEncoding]; 
    const uint8_t* input = (const uint8_t*)[theData bytes]; 
    NSInteger length = [theData length]; 

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/="; 

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2)/3) * 4]; 
    uint8_t* output = (uint8_t*)data.mutableBytes; 

    NSInteger i; 
    for (i=0; i < length; i += 3) { 
     NSInteger value = 0; 
     NSInteger j; 
     for (j = i; j < (i + 3); j++) { 
      value <<= 8; 

      if (j < length) { 
       value |= (0xFF & input[j]); 
      } 
     } 

     NSInteger theIndex = (i/3) * 4; 
     output[theIndex + 0] =     table[(value >> 18) & 0x3F]; 
     output[theIndex + 1] =     table[(value >> 12) & 0x3F]; 
     output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '='; 
     output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '='; 
    } 

    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
} 
0

penso che sia più soluzione compatta:

#import <CommonCrypto/CommonCrypto.h> 

... 

-(NSData*)Sha256WithKey:(NSData*)key andData:(NSData*)data{ 
    NSMutableData* result = [NSMutableData 
           dataWithCapacity:CC_SHA256_DIGEST_LENGTH]; 

    CCHmac(kCCHmacAlgSHA256, [key bytes], [key length], 
      [data bytes], [data length], result.mutableBytes); 

    return result; 
} 
....