简体   繁体   English

如何计算 iOS 中的 SHA-2(最好是 SHA 256 或 SHA 512)hash?

[英]How can I compute a SHA-2 (ideally SHA 256 or SHA 512) hash in iOS?

The Security services API doesn't appear to allow me to compute a hash directly.安全服务 API 似乎不允许我直接计算 hash。 There are plenty of public domain and liberally licensed versions available, but I'd rather use a system library implementation if possible.有很多公共领域和自由许可的版本可用,但如果可能的话,我宁愿使用系统库实现。

The data is accessible via NSData, or plain pointers.数据可通过 NSData 或普通指针访问。

The cryptographic strength of the hash is important to me. hash 的加密强度对我来说很重要。 SHA-256 is the minimum acceptable hash size. SHA-256 是可接受的最小 hash 大小。

This is what I'm using for SHA1:这是我用于 SHA1 的:

#import <CommonCrypto/CommonDigest.h>

+ (NSData *)sha1:(NSData *)data {
    unsigned char hash[CC_SHA1_DIGEST_LENGTH];
    if ( CC_SHA1([data bytes], [data length], hash) ) {
        NSData *sha1 = [NSData dataWithBytes:hash length:CC_SHA1_DIGEST_LENGTH];        
        return sha1;
    }
return nil;
}

Replace CC_SHA1 with CC_SHA256 (or whichever you need), as well as CC_SHA1_DIGEST_LENGTH with CC_SHA256_DIGEST_LENGTH .CC_SHA1替换为CC_SHA256 (或任何您需要的),以及将CC_SHA1_DIGEST_LENGTH替换为CC_SHA256_DIGEST_LENGTH

Here's a pretty similar one based on NSString这是一个非常相似的基于 NSString

+ (NSString *)hashed_string:(NSString *)input
{
    const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:input.length];
    uint8_t digest[CC_SHA256_DIGEST_LENGTH];

    // This is an iOS5-specific method.
    // It takes in the data, how much data, and then output format, which in this case is an int array.
    CC_SHA256(data.bytes, data.length, digest);

    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];

    // Parse through the CC_SHA256 results (stored inside of digest[]).
    for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }

    return output;
}

(Credits go to http://www.raywenderlich.com/6475/basic-security-in-ios-5-tutorial-part-1 ) (学分 go 至http://www.raywenderlich.com/6475/basic-security-in-ios-5-tutorial-part-1

This is what worked for me这对我有用

func sha256(securityString : String) -> String {
    let data = securityString.dataUsingEncoding(NSUTF8StringEncoding)!
    var hash = [UInt8](count: Int(CC_SHA256_DIGEST_LENGTH), repeatedValue: 0)
    CC_SHA256(data.bytes, CC_LONG(data.length), &hash)
    let output = NSMutableString(capacity: Int(CC_SHA1_DIGEST_LENGTH))
    for byte in hash {
        output.appendFormat("%02x", byte)
    }
    return output as String
}

Below link i used for creating document hash value and Its very simple and easy to calculate hash value specially for large files.下面的链接我用于创建文档 hash 值,它非常简单,易于计算 hash 值,特别适用于大文件。

Link: http://www.joel.lopes-da-silva.com/2010/09/07/compute-md5-or-sha-hash-of-large-file-efficiently-on-ios-and-mac-os-x/comment-page-1/#comment-18533链接: http://www.joel.lopes-da-silva.com/2010/09/07/compute-md5-or-sha-hash-of-large-file-efficiently-on-ios-and-mac- os-x/comment-page-1/#comment-18533

+ (NSData *)sha256DataFromData:(NSData *)data {
    unsigned char result[CC_SHA256_DIGEST_LENGTH];
    CC_SHA256([data bytes], (int)[data length], result);
    return [NSData dataWithBytes:result length:CC_SHA256_DIGEST_LENGTH];
}

I cleaned up https://stackoverflow.com/a/13199111/1254812 a bit and structured it as an NSString extension我清理了一点 https://stackoverflow.com/a/13199111/1254812并将其构建为NSString扩展

@interface NSString (SHA2HEX)

/*
 Get the SHA2 (256 bit) digest as a hex string.
 */
@property (nonatomic, readonly) NSString* sha2hex;
@end

@implementation NSString (SHA2HEX)

- (NSString*)sha2hex
{
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];

    if (data.length > UINT32_MAX)
        return nil;

    uint8_t digest[CC_SHA256_DIGEST_LENGTH];
    CC_SHA256(data.bytes, (CC_LONG)data.length, digest);

    const int hexlen = CC_SHA256_DIGEST_LENGTH * 2;
    NSMutableString *hexstr = [NSMutableString stringWithCapacity:hexlen];

    for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
        [hexstr appendFormat:@"%02x", digest[i]];
    }

    return hexstr;
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM