title: HMAC SHA256 in Swift 4
body: <p>I have a string and a key, which i want to generate an HMAC SHA256 from it. Although i'm using 2 libs</p>
  
  <p><a href="https://github.com/iosdevzone/IDZSwiftCommonCrypto" rel="noreferrer">IDZSwiftCommonCrypto</a> and <a href="https://github.com/krzyzanowskim/CryptoSwift#hmac-based-key-derivation-function" rel="noreferrer">CryptoSwift</a></p>
  
  <p>and <a href="https://github.com/krzyzanowskim/CryptoSwift#hmac-based-key-derivation-function" rel="noreferrer">this answer</a></p>
  
  <p>Nothing really worked for me. My source of truth are those 2 websites</p>
  
  <p><a href="https://myeasywww.appspot.com/utility/free/online/Crypt_Decrypt-MD5-AES-HMAC-SHA-DES-RABBIT/en?command=UTILITY&amp;ID=2" rel="noreferrer">https://myeasywww.appspot.com/utility/free/online/Crypt_Decrypt-MD5-AES-HMAC-SHA-DES-RABBIT/en?command=UTILITY&amp;ID=2</a></p>
  
  <p>and </p>
  
  <p><a href="https://www.freeformatter.com/hmac-generator.html#ad-output" rel="noreferrer">https://www.freeformatter.com/hmac-generator.html#ad-output</a></p>
  
  <p>Which they always generate the correct hash key for my case.
  Any idea in what can work here? Some code samples</p>
  
  <p>For IDZSwiftCommonCrypto</p>
  
  <pre><code>func getHMacSHA256(forMessage message: String, key: String) -&gt; String? {
      let hMacVal = HMAC(algorithm: HMAC.Algorithm.sha256, key: key).update(string: message)?.final()
      if let encryptedData = hMacVal {
          let decData = NSData(bytes: encryptedData, length: Int(encryptedData.count))
          let base64String = decData.base64EncodedString(options: .lineLength64Characters)
          print("base64String: \(base64String)")
          return base64String
      } else {
          return nil
      }
  }
  </code></pre>
  
  <p>And for CryptoSwift</p>
  
  <pre><code>    let password: Array&lt;UInt8&gt; = Array(payload.utf8)
      let salt: Array&lt;UInt8&gt; = Array("somekey".utf8)
  
      let signedBody = try? HKDF(password: password, salt: salt, variant: .sha256).calculate()
  </code></pre>
  
  <p>But nothing really works like the sources of truth.Any idea?</p>