An easy-to-use category for NSData
and NSString
to calculate digests.
Supports:
- SHA224
- SHA256
- SHA386
- SHA512
- SHA1
- MD2
- MD4
- MD5
CBHDigestKit
extends NSData
and NSString
with a category adding methods for several digests and converting from bytesToHex
.
Compute the SHA256 digest of a NSString
:
NSString *string = @"This is a string.";
NSData *digest = [string sha256UsingEncoding:NSUTF8StringEncoding];
Compute the SHA256 digest of a NSData
:
NSData *data = [NSData dataWithContentsOfFile:@"/etc/hosts"];
NSData *digest = [data sha256];
Compute and print SHA256 digest of a file:
NSData *data = [NSData dataWithContentsOfFile:@"/etc/hosts"];
NSData *digest = [data sha256];
NSLog(@"0x%@", [digest bytesToHex]);
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 bytesToHex]);
CBHDigestKit is available under the ISC license.