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.