我有一个与AES加密相关的问题。问题是我需要使用AES加密技术,使用初始化向量,盐,RFC2898迭代来加密字符串,并使用sha1算法生成密钥。
我使用了下面的代码
+(NSString *)stringToSha1:(NSString *)str{
const char *s = [str cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
// This is the destination
uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
// This one function does an unkeyed SHA1 hash of your hash data
CC_SHA1(keyData.bytes, keyData.length, digest);
// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
NSLog(@"Hash is %@ for string %@", hash, str);
return hash;
}用于生成sha1密钥,但它产生的结果与.net和Android中的这项技术完全不同。
Android和.net已经有类和库来做这件事了,我就不管了,所以我怎么用iPhone来做这件事呢?
发布于 2011-10-21 19:20:18
这应该是您需要的
+ (NSData *)sha1HashFromString:(NSString *)stringToHash {
NSData *stringData = [stringToHash dataUsingEncoding:NSASCIIStringEncoding];
uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
CC_SHA1([stringData bytes], [stringData length], digest);
NSData *hashedData = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
return [hashedData autorelease];
}https://stackoverflow.com/questions/7848147
复制相似问题