Makes calculating digests easy.
Supports:
- SHA-224
- SHA-256
- SHA-386
- SHA-512
- SHA-1
- MD2
- MD4
- MD5
CBHDigestKit
extends NSData
and NSString
with a category adding methods for several digests and converting from bytesToHex
.
Compute the SHA-256 digest of a NSString
:
NSData *digest = [@"This is a string." sha256UsingEncoding:NSUTF8StringEncoding];
Compute the SHA-256 digest of a NSData
:
NSData *digest = [[NSData dataWithContentsOfFile:@"/etc/hosts"] sha256];
Efficiently compute the SHA-256 digest of a file:
NSInputStream *fileStream = [NSInputStream inputStreamWithFileAtPath:@"/etc/hosts"];
NSData *digest = [CBHDigester digestStream:fileStream usingAlgorithm:CBHDigestAlgorithm_SHA256];
Compute and print a digest using an arbitrary algorithm:
CBHDigestAlgorithm algorithm = ...
NSData *data = [NSData dataWithContentsOfFile:@"/etc/hosts"];
NSData *digest = [data digestUsingAlgorithm:algorithm];
NSLog(@"0x%@", [digest encodeAsHexadecimal]);
CBHDigestKit is available under the ISC license.