简体   繁体   中英

SHA-256 Hashing with secret key swift

I am a newbie to encryption and hashing algorithms. I need to Hash a string with a secret key using the SHA-256 algorithm. I tried multiple links from stack overflow and some other tutorials as well, Using those links, the output I received in iOS is different from the output I am getting in Android. I have used the same string and secret key on both platforms.

Android Code Snippet -

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

INPUT -

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 data.map { String(format: "%02hhx", $0) }.joined()
    }
}

INPUT -

secret_key = "35285324354d562c245b031232115124372e5242394f51301f62224e1e432910"
message = "Guest"

IMPLEMENTATION -> OUTPUT = message.generateSHA256(secret_key)

`OUTPUT = "944a37b9768970c5da4f35295008470603391223a05d2b17eed668f1678d447c"'

Please suggest any other method which I can implement in iOS to produces the same output as received in android.

Your Android code and your iOS code is not equivalent.

Your Android code simply computes the SHA256 digest of your key value concatenation with your message.

You are first calling update with your key and then calling digest with your messsge. The documentation states:

Performs a final update on the digest using the specified array of bytes, then completes the digest computation. That is, this method first calls update(input) , passing the input array to the update method, then calls digest() .

Your iOS code, on the other hand, is computing a HMAC of your message using the supplied key. This is not the same thing.

You need to compute the SHA256 in the same way that you do on Android;

  • Use CC_SHA256_Init
  • call CC_SHA256_Update with your key and message
  • Call CC_SHA256_Final to get the hash

Probably easier in Swift is to use SwiftyRSA . All you need to do then is create an instance of ClearMessage initialised with your concatenation key and message and then call the digest function on it.

Don't know about how the android doing SHA-256 but for the iOS I can say, the code you are doing is perfect and the results is right. You should update your question to: "How can I get "SHA-256 for Android same as the iOS"

You can check for SHA-256 HashMap online here

Just enter the input and secret key and select SHA-256 from list.

secret_key = "35285324354d562c245b031232115124372e5242394f51301f62224e1e432910"
message = "Guest"

You will see the output, which is same as the one you getting from the iOS Code in your question.

944a37b9768970c5da4f35295008470603391223a05d2b17eed668f1678d447c

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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