A.D
A.D

Reputation: 2270

Objective-C: Generate hash the way mysql password() does

According to this site mysql uses:

SHA1(UNHEX(SHA1("this_is_a_random_string")))

To generate its password() hash.

...

I have copied this function, from another StackOverflow post:

- (NSString *)sha1:(NSString *)str {
    const char *cStr = [str UTF8String];
    unsigned char result[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1(cStr, strlen(cStr), result);
    NSString *s = [NSString  stringWithFormat:
                  @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                  result[0], result[1], result[2], result[3], result[4],
                  result[5], result[6], result[7],
                  result[8], result[9], result[10], result[11], result[12],
                  result[13], result[14], result[15],
                  result[16], result[17], result[18], result[19]
                  ];

    return s;
}

To generate sh1 hashes. But as the first link says:

the second SHA1() is applied to the binary data returned by the first SHA1() and not to its hex representation. Therefore in SQL I have to UNHEX() it before applying the second SHA1.

So I have to do something like:

NSString * a = [self sha1:@"abc"];
// convert a into binary data.
NSString * b = [self:sha1:a];

But whatever I try I cannot get it to work.

What is the simplest way to do this?

Upvotes: 1

Views: 188

Answers (1)

VAO
VAO

Reputation: 1126

- (NSString *)mysqlHashFromString:(NSString *)password{
    NSString *sha1String = [self sha1:password];
    NSData *sha1StringBinaryRepresentation = [self dataWithString:sha1String];
    NSString *finalHash = [self hexStringValue:[self SHA1Digest:sha1StringBinaryRepresentation ]];
    return finalHash;
} 

- (NSData *)dataWithString:(NSString *)string {
   NSString *command = [command stringByReplacingOccurrencesOfString:@" " withString:@""];
   NSMutableData *commandToSend = [NSMutableData new];
   unsigned char whole_byte;
   char byte_chars[3] = {'\0','\0','\0'};
   int i;
   for (i=0; i < [command length]/2; i++) {
       byte_chars[0] = [command characterAtIndex:i*2];
       byte_chars[1] = [command characterAtIndex:i*2+1];
       whole_byte = strtol(byte_chars, NULL, 16);
       [commandToSend appendBytes:&whole_byte length:1]; 
   }
   return commandToSend;
}

- (NSString *)sha1:(NSString *)plainTextPasssword{
    NSData *data = [plainTextPasssword dataUsingEncoding:NSUTF8StringEncoding];
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1(data.bytes, data.length, digest);
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}

- (NSData *)SHA1Digest:(NSData *)data {
    unsigned char result[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1([data bytes], [data length], result);
    return [NSData dataWithBytes:result length:CC_SHA1_DIGEST_LENGTH];
}

- (NSString *)hexStringValue:(NSData *)data {
    NSMutableString *stringBuffer = [NSMutableString stringWithCapacity:([data length] * 2)];
    const unsigned char *dataBuffer = [data bytes];
    for (int i = 0; i < [data length]; ++i) {
        [stringBuffer appendFormat:@"%02x", (unsigned long)dataBuffer[i]];
    }

    return [stringBuffer copy];
}

Creating SHA1 Hash from NSString
Converting HEX NSString To NSData
https://github.com/ali-rantakari/Tagger/blob/master/NSData%2BSHA1.m

Upvotes: 1

Related Questions