2015-08-11 1 views
2

J'essaie de trouver l'équivalent de cette fonction pour iOS:équivalent de hachage PHP funtion() avec rawoutput pour iOS

hash('sha512', '123', true); 

C'est ma méthode que j'utilise ce qui équivaut à: hash('sha512', '123', false);

- (NSString *)sha512{ 

    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding]; 
    uint8_t digest[CC_SHA512_DIGEST_LENGTH]; 

    CC_SHA512(data.bytes, data.length, digest); 

    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2]; 

    for (int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++) 
    { 
     [output appendFormat:@"%02x", digest[i]]; 
    } 

    return output; 
} 

comment puis-je l'obtenir pour me donner la rawoutput?

+0

définir la "puissance brute". Voulez-vous dire NSData au lieu d'une chaîne? Un exemple aiderait. Le code ressemble à une catégorie sur 'NSString', c'est ce que vous voulez? – zaph

+0

sortie brute est « données binaires brutes » Ouais je l'utilise comme une catégorie sur NSString, pour des raisons pratiques –

+0

@JaredH Pas vraiment un double, l'OP ne veut pas un 'résultat NSString' et il est essentiellement ce que l'OP a fourni dans la question. – zaph

Répondre

2

Si vous voulez sortie NSData (ce qui est un wrapper autour des octets bruts:

// Catégorie sur NSString par demande OP

- (NSData *)sha512 { 
    NSData *dataIn = [self dataUsingEncoding:NSUTF8StringEncoding]; 
    NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA512_DIGEST_LENGTH]; 

    CC_SHA512(dataIn.bytes, 
       (CC_LONG)dataIn.length, 
       macOut.mutableBytes); 

    return macOut; 
} 

Test:

NSData *sha512Data = [@"123" sha512]; 
NSLog(@"sha512Data: %@", sha512Data); 

Sortie (affiché en hexadécimal parce que comment NSData l'affiche de octets:

sha512Data: < 3c9909af ec25354d 551dae21 590bb26e 38d53f21 73b8d3dc 3eee4c04 7e7ab1c1 eb8b8510 3e3be7ba 613b31bb 5c9c3621 4dc9f14a 42fd7a2f db84856b ca5c44c2>

Si un pointeur sur les octets est nécessaire il suffit d'utiliser sha512Data.bytes.

0

Ceci est plus précisément ce que je voulais accomplir, espérons que cela peut aider à toute personne qui rencontre le même problème.

How to use WSSE in iOS with salted sha512 with more than 1 iteration

- (NSString *)hashPassword:(NSString *)password ansSalt:(NSString *)salt { 

    NSString *passwordSalted = [NSString stringWithFormat:@"%@{%@}",password,salt]; 

    NSData *passwordData = [passwordSalted dataUsingEncoding:NSUTF8StringEncoding]; 

    uint8_t hash[CC_SHA512_DIGEST_LENGTH]; 
    CC_SHA512([passwordData bytes], [passwordData length], hash); 

    NSMutableData *allData = [[NSMutableData alloc] init]; 
    [allData appendBytes:hash length:CC_SHA512_DIGEST_LENGTH]; 

    for (NSInteger i = 1; i < 1000; i++) { 

     [allData appendBytes:[passwordData bytes] length:[passwordData length]]; 
     uint8_t hashLoop[CC_SHA512_DIGEST_LENGTH]; 
     CC_SHA512([allData bytes], [allData length], hashLoop); 
     [allData setLength:0]; 
     [allData appendBytes:hashLoop length:CC_SHA512_DIGEST_LENGTH]; 

    } 

    NSData *imageData = [NSData dataWithBytes:[allData bytes] length:[allData length]]; 

    return [imageData base64EncodedStringWithOptions:0]; 

}