首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iPhone中使用IV、Salt、RFC2898迭代进行加密,使用SHA1算法生成密钥

在iPhone中使用IV、Salt、RFC2898迭代进行加密,使用SHA1算法生成密钥
EN

Stack Overflow用户
提问于 2011-10-21 18:29:13
回答 1查看 1.3K关注 0票数 2

我有一个与AES加密相关的问题。问题是我需要使用AES加密技术,使用初始化向量,盐,RFC2898迭代来加密字符串,并使用sha1算法生成密钥。

我使用了下面的代码

代码语言:javascript
复制
+(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来做这件事呢?

EN

回答 1

Stack Overflow用户

发布于 2011-10-21 19:20:18

这应该是您需要的

代码语言:javascript
复制
+ (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];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7848147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档