Sha256 Hashes in Objective-C
Here’s the translation of the SHA256 Hashes example from Go to Objective-C:
Our first program demonstrates how to compute SHA256 hashes in Objective-C. SHA256 hashes are frequently used to compute short identities for binary or text blobs. For example, TLS/SSL certificates use SHA256 to compute a certificate’s signature.
In Objective-C, we use the CommonCrypto library to compute SHA256 hashes. This library is part of the iOS and macOS SDKs.
We define a helper function sha256
that takes a string input and returns its SHA256 hash as a hexadecimal string.
In the main
function:
- We create an autorelease pool to manage memory.
- We define our input string.
- We call our
sha256
function to compute the hash. - We print both the original string and its hash.
To run the program, save it as a .m
file (e.g., sha256_hashes.m
) and compile it with:
This computes the hash and prints it in a human-readable hex format.
Note that if you need cryptographically secure hashes, you should carefully research hash strength!
Objective-C provides access to other hash functions through the CommonCrypto library. For example, to compute SHA512 hashes, you would use CC_SHA512
instead of CC_SHA256
.