iPhone 中的 SHA256 密钥生成器

发布于 2024-09-06 10:39:43 字数 421 浏览 5 评论 0原文

我想使用 SHA256 进行 N 次迭代生成密钥。

他们输入的应该是我的“密码” + “随机数”

我已经看到了苹果提供的加密示例,但它似乎没有提供我的要求(或者可能是可能我没有正确理解)。

我也浏览了下面的链接,但没有使用 SHA256 生成密钥的方法 http://iphonedevelopment.blogspot.com/ 2009/02/strong-encryption-for-cocoa-cocoa-touch.html

等待一些提示。

问候

I want to generate a key using SHA256 with N number of iterations.

They input should be my "password" + "random number"

I have seen the Crypto sample provided by apple but it seems it doesn't provide my requirement(or might be possible I didnt get it properly).

I have gone through below link as well but is doesnt have method to generate a key using SHA256
http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html

Waiting for some hint.

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的好友蓝忘机已上羡 2024-09-13 10:39:43

试试这个,它对我有用

1)获取平面文本输入的哈希值

-(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)获取 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]; 

    // 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;

}

Try this, it worked for me

1) To get a hash for plane 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;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文