Objective-C for iPhone中的Sha256 [英] Sha256 in Objective-C for iPhone
问题描述
当我使用这段代码创建一个字符串sha256时
unsigned char hashedChars [32];
NSString * inputString;
inputString = [NSString stringWithFormat:@hello];
NSData * inputData = [inputString dataUsingEncoding:NSUTF8StringEncoding];
CC_SHA256(inputData.bytes,inputData.length,hashedChars);
它正确返回散列,但我需要插入一个像这样的字符串 \x00 \x25\x53 ,在这种情况下,函数返回空字符串的sha256,因为指定的编码不能用于转换接收器。
现在,我的问题是:如何插入此特殊字符以生成正确的哈希?谢谢
1)获取纯文本输入的散列
- (NSString *)sha256HashFor:(NSString *)输入
{
const char * str = [输入UTF8String];
unsigned char result [CC_SHA256_DIGEST_LENGTH];
CC_SHA256(str,strlen(str),result);
NSMutableString * ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
for(int i = 0; i< CC_SHA256_DIGEST_LENGTH; i ++)
{
[ret appendFormat:@%02x,result [i]];
}
return ret;
$ / code>
2)为 NSData
作为输入
注意:我已经使用 NSData
所以代码如下
- (NSString *)SHA256_HASH {
// if(!self)return nil ;
unsigned char hash [CC_SHA256_DIGEST_LENGTH];
if(CC_SHA256([(NSData *)self bytes],[(NSData *)self length],hash)){
NSData * sha2 = [NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH];
//描述转换为十六进制,但是将<>在它周围并且每隔4个字节就有空格
NSString * hash = [sha2 description];
hash = [hash stringByReplacingOccurrencesOfString:@withString:@];
hash = [hash stringByReplacingOccurrencesOfString:@< withString:@ ];
hash = [hash stringByReplacingOccurrencesOfString:@> withString:@ ];
// hash现在是一个只有40char散列值的字符串
// NSLog(@hash =%@,hash);
//格式化SHA256指纹像
// // 00:00:00:00:00:00:00:00:00
int keyLength = [hash length];
NSString * formattedKey = @;
for(int i = 0; i< keyLength; i + = 2){
NSString * substr = [hash substringWithRange:NSMakeRange(i,2)];
if(i!= keyLength-2)
substr = [substr stringByAppendingString:@:];
formattedKey = [formattedKey stringByAppendingString:substr];
}
返回formattedKey;
}
return nil;
}
When I use this code to create a sha256 of a string
unsigned char hashedChars[32];
NSString *inputString;
inputString = [NSString stringWithFormat:@"hello"];
NSData * inputData = [inputString dataUsingEncoding:NSUTF8StringEncoding];
CC_SHA256(inputData.bytes, inputData.length, hashedChars);
It returns the hash correctly, but I need to insert a string like this \x00\x25\x53 and in this case, the function returns a sha256 of empty string because the specified encoding cannot be used to convert the receiver.
Now, my question is:How to insert this special characters for generate a correct hash? Thanks
Try this, it worked for me
1) To get a hash for plain text input
-(NSString*)sha256HashFor:(NSString*)input
{
const char* str = [input UTF8String];
unsigned char result[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(str, strlen(str), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
{
[ret appendFormat:@"%02x",result[i]];
}
return ret;
}
2) To get hash for NSData
as input
Note:- I have used NSData
category, so the code is as follow
- (NSString *)SHA256_HASH {
//if (!self) return nil;
unsigned char hash[CC_SHA256_DIGEST_LENGTH];
if ( CC_SHA256([(NSData*)self bytes], [(NSData*)self length], hash) ) {
NSData *sha2 = [NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [sha2 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 = %@",hash);
// Format SHA256 fingerprint like
// 00:00:00:00:00:00:00:00:00
int keyLength=[hash length];
NSString *formattedKey = @"";
for (int i=0; i<keyLength; i+=2) {
NSString *substr=[hash substringWithRange:NSMakeRange(i, 2)];
if (i!=keyLength-2)
substr=[substr stringByAppendingString:@":"];
formattedKey = [formattedKey stringByAppendingString:substr];
}
return formattedKey;
}
return nil;
}
这篇关于Objective-C for iPhone中的Sha256的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!