简体   繁体   English

SHA-256快速密钥散列

[英]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. 我需要使用SHA-256算法使用密钥将字符串散列。 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. 我还尝试了堆栈溢出和其他一些教程中的多个链接,使用这些链接,我在iOS中收到的输出与我在Android中获得的输出不同。 I have used the same string and secret key on both platforms. 我在两个平台上都使用了相同的字符串和秘密密钥。

Android Code Snippet - Android代码段-

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 " OUTPUT = "99D71664BD5A35E0185C020BACB709DEB24A81555E275CA9328F8CB4E6F186C3

iOS Code snipet - iOS代码片段-

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) 实现->输出= message.generateSHA256(secret_key)

`OUTPUT = "944a37b9768970c5da4f35295008470603391223a05d2b17eed668f1678d447c"' `OUTPUT =“ 944a37b9768970c5da4f35295008470603603391223a05d2b17eed668f1678d447c”'

Please suggest any other method which I can implement in iOS to produces the same output as received in android. 请提出我可以在iOS中实施的任何其他方法,以产生与android接收的相同输出。

Your Android code and your iOS code is not equivalent. 您的Android代码和iOS代码不相同。

Your Android code simply computes the SHA256 digest of your key value concatenation with your message. 您的Android代码仅计算消息中键值串联的SHA256摘要。

You are first calling update with your key and then calling digest with your messsge. 您首先要用密钥调用update ,然后用消息调用digest 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() . 也就是说,此方法首先调用update(input) ,将输入数组传递给update方法,然后调用digest()

Your iOS code, on the other hand, is computing a HMAC of your message using the supplied key. 另一方面,您的iOS代码正在使用提供的密钥来计算消息的HMAC。 This is not the same thing. 这不是同一回事。

You need to compute the SHA256 in the same way that you do on Android; 您需要以与Android相同的方式计算SHA256;

  • Use CC_SHA256_Init 使用CC_SHA256_Init
  • call CC_SHA256_Update with your key and message 使用您的密钥和消息呼叫CC_SHA256_Update
  • Call CC_SHA256_Final to get the hash 调用CC_SHA256_Final获取哈希

Probably easier in Swift is to use SwiftyRSA . 在Swift中可能更容易使用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. 然后,您要做的就是创建一个用连接键和消息初始化的ClearMessage实例,然后在其上调用摘要函数。

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. 不知道Android如何做SHA-256,但是对于iOS,我可以说,您正在执行的代码是完美的,并且结果是正确的。 You should update your question to: "How can I get "SHA-256 for Android same as the iOS" 您应该将问题更新为:“如何获得“与iOS相同的Android SHA-256”

You can check for SHA-256 HashMap online here 您可以在此处在线检查SHA-256 HashMap

Just enter the input and secret key and select SHA-256 from list. 只需输入输入和密钥,然后从列表中选择SHA-256。

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. 您将看到输出,与您从问题中的iOS代码获得的输出相同。

944a37b9768970c5da4f35295008470603391223a05d2b17eed668f1678d447c

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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