5

I am currently working on a project (in swift 3/4) where I need to hash a HTTP request that is formatted as so:

    {"request": {"method": "getMyPeople", 
    "params": {"api_key": 00de5089d590e413807343166da22a45,
    "user_id": 8 }, "id": "1"}}

I am trying to hash in SHA-256 with a secret key, something that looks like this:

    6b107c7ebebf056e1c45924d0546d35e

What I need:

  1. I need to be able to hash with sha256 with a secret key.
  2. I need that hashed request to be a string in hex form in order for me to make a request to the database

What I have:

  1. Set up my header bridge file to use objective-c code
  2. Looked into many solutions and none of them worked for me

Solutions I have tried: My most recent attempt is Zaph's post here (https://stackoverflow.com/a/39249920/8093921).

Where my issue occurs: My issue seems to occurring when I try to convert the form of

 hashSHA256: <aabc766b 6b357564 e41f4f91 2d494bcc bfa16924 b574abbd ba9e3e9d a0c8920a>

as seen in Zaph's post, they leave it in this form where I need it in form of a string in hex.

If anyone need any more clarification please let me know. Thank you in advance for the help!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Asher Straubing
  • 83
  • 1
  • 1
  • 7
  • There are >100 Q&A's about `[swift] SHA256` – Which solutions did you try, and how exactly did they not work? – Martin R Jan 23 '18 at 16:06
  • @MartinR, My issue seems to be from getting the returned value after the string in hex form. My recent attempt was with this post from https://stackoverflow.com/questions/39249191/hash-string-with-sha512-in-swift from Zaph. Sorry for not explaining that in post my post. – Asher Straubing Jan 23 '18 at 16:22
  • What "issue"? Please show a [mcve] with input, actual output and expected output. – We cannot *guess* what your problem is. – Martin R Jan 23 '18 at 16:25
  • This [How to convert Data to hex string in swift](https://stackoverflow.com/questions/39075043/how-to-convert-data-to-hex-string-in-swift) or this [How to hash NSString with SHA1 in Swift?](https://stackoverflow.com/questions/25761344/how-to-hash-nsstring-with-sha1-in-swift) might be what you are looking for. – Martin R Jan 23 '18 at 16:28
  • I will look into this, thank you. I apologize for not creating a verifiable example. This is my first post on Stack Overflow. – Asher Straubing Jan 23 '18 at 16:33
  • @MartinR I got my hash to work, thank you for the references. Again, next time I post I will make sure to craft a comprehensive post along with code to show my errors. – Asher Straubing Jan 23 '18 at 17:17

1 Answers1

17

I admit that it can be confusing to get everything together but the final solution is quite simple:

extension String {

    func hmac(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()
    }

}

Example:

let result = "test".hmac(key: "test")

Result:

88cd2108b5347d973cf39cdf9053d7dd42704876d8c9a9bd8e2d168259d3ddf7
sundance
  • 2,930
  • 1
  • 20
  • 25