Objective-C:按照 mysql password() 的方式生成哈希
Objective-C: Generate hash the way mysql password() does
根据this sitemysql使用:
SHA1(UNHEX(SHA1("this_is_a_random_string")))
生成其 password() 哈希。
...
我从另一个Whosebug 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;
}
生成 sh1 哈希。但正如第一个 link 所说:
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.
所以我必须做类似的事情:
NSString * a = [self sha1:@"abc"];
// convert a into binary data.
NSString * b = [self:sha1:a];
但无论我尝试什么,我都无法让它工作。
最简单的方法是什么?
- (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] = {'[=10=]','[=10=]','[=10=]'};
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
根据this sitemysql使用:
SHA1(UNHEX(SHA1("this_is_a_random_string")))
生成其 password() 哈希。
...
我从另一个Whosebug 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;
}
生成 sh1 哈希。但正如第一个 link 所说:
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.
所以我必须做类似的事情:
NSString * a = [self sha1:@"abc"];
// convert a into binary data.
NSString * b = [self:sha1:a];
但无论我尝试什么,我都无法让它工作。
最简单的方法是什么?
- (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] = {'[=10=]','[=10=]','[=10=]'};
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