Code Example for Swift’s MessageDigest 256 Using Swift Messaging

It is recommended to modify your inquiry to inquire about obtaining SHA-256 for Android that is equivalent to that of iOS. You may inspect the SHA-256 HashMap available on the internet by providing the input and secret key, and choosing SHA-256 from the options. My objective is to utilize the SHA-256 algorithm to hash a string with a secret key.


As a beginner in encryption and hashing algorithms, I aim to hash a string with a secret key through the
algorithm. Despite following various links from Stack Overflow and other tutorials, the output I obtained in iOS differs from what I received in Android, despite using the same string and secret key for both platforms.

Android Code Snippet

MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] channelKeyLong = digest.digest(message.getBytes("utf-8"));


secret_key = "35285324354d562c245b031232115124372e5242394f51301f62224e1e432910"
message = "Guest"

OUTPUT = "99D71664BD5A35E0185C020BACB709DEB24A81555E275CA9328F8CB4E6F186C3

iOS Code snipet –

extension String {
  func generateSHA256(key: String) -> String {
        var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
        CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA256), key, key.count, self, self.count, &digest)
        let data = Data(bytes: digest)
        return { String(format: "%02hhx", $0) }.joined()


secret_key = "35285324354d562c245b031232115124372e5242394f51301f62224e1e432910"
message = "Guest"

The generation of the SHA256 output is carried out by calling the ‘message.generateSHA256’ function with the secret key passed as an argument.

The resulting value is a string of characters consisting of “944a37b9768970c5da4f35295008470603391223a05d2b17eed668f1678d447c”.

Kindly recommend an alternate approach that can be used on iOS to achieve the identical results obtained on Android.

Solution 1:

The code for your Android and iOS platforms are not the same.

Your Android code calculates the SHA256 hash of the concatenation of your message and key value.

Initially, you invoke


by providing your key, followed by invoking


with your message. The documentation specifies:

This method executes the final update on the digest by utilizing the given byte array and then finishes the computation of the digest. In other words, it invokes


initially, which takes the input array as an argument for the update method. Afterward, it calls



On one side, your iOS code is generating an HMAC of your message with the provided key, which is distinct from the other process.

The method for computing SHA256 on Android should be replicated.

  • Use

  • Connect to


    using your key and message.
  • Retrieve the hash by dialing



In Swift, using SwiftyRSA is a simpler option. You can create an instance of


with your
concatenation key
and the message, then execute the digest function.

Solution 2:

Regarding SHA-256 on Android, I cannot speak to its performance. However, I can confirm that the code you have developed for iOS is flawless and produces accurate results. It may be beneficial to revise your inquiry to inquire about obtaining identical SHA-256 functionality on both Android and iOS platforms.

Access an online resource to verify the SHA-256 HashMap.

Simply input your values for the input and secret keys, then choose SHA-256 from the available options.

secret_key = "35285324354d562c245b031232115124372e5242394f51301f62224e1e432910"
message = "Guest"

The output you receive will be identical to the one obtained from the iOS Code mentioned in your inquiry.


Hope it helps!

Frequently Asked Questions

Posted in Ios