根据this site mysql使用:
$message[0]
生成密码()哈希。
...
我已从另一个StackOverflow post复制此功能:
<?php
$lastDate = NULL;
$chat = file("members/cdn/1/chats/9188.txt");
foreach($chat as $line) :
$name = strchr($line,":",true);
$message = explode(' ', substr(strchr($line,":"),1), 3);
if($lastDate != $message[0]) :
$lastDate = $message[0];
?>
<div><?=$message[0];?></div>
<?php
endif;
?>
<div class="container">
<div class="arrow">
<div class="outer"></div>
<div class="inner"></div>
</div>
<div class="message-body">
<p><?=$message[2];?></p>
<p class="message_time"><?=date("g:i a", strtotime($message[1]));?></p>
</div>
</div>
<div class="spacer"></div>
<?php
endforeach;
?>
生成sh1哈希值。但正如第一个链接所说:
第二个SHA1()应用于第一个SHA1()返回的二进制数据,而不是其十六进制表示。因此,在SQL中,我必须在应用第二个SHA1之前使用UNHEX()。
所以我必须这样做:
SHA1(UNHEX(SHA1("this_is_a_random_string")))
但无论我尝试什么,我都无法让它发挥作用。
最简单的方法是什么?
谢谢!
答案 0 :(得分:1)
- (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