title: How to pass key in HMAC as HEX Swift iOS
body: <p>So I have this code to generate for HMAC-SHA1</p>
  
  <pre><code>let key = "foo".toSHA1()
  let data = "bar"
  var results = [CUnsignedChar](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
  
  CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1), key, key.count, data, data.count, &amp;results)
  
  let hmacData:NSData = NSData(bytes: results, length: (Int(CC_SHA1_DIGEST_LENGTH)))
  
  var bytes = [UInt8](repeating: 0, count: hmacData.length)
  hmacData.getBytes(&amp;bytes, length: hmacData.length)
  
  var hexString = ""
  for byte in bytes {
      hexString += String(format:"%02hhx", UInt8(byte))
  }
  
  print(hexString)
  </code></pre>
  
  <p>and this code for converting key string to SHA1</p>
  
  <pre><code>func toSHA1() -&gt; String {
          let data = self.data(using: String.Encoding.utf8)!
          var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
          data.withUnsafeBytes {
              _ = CC_SHA1($0, CC_LONG(data.count), &amp;digest)
          }
          let hexBytes = digest.map { String(format: "%02x", $0) }
  
          return hexBytes.joined()
      }
  </code></pre>
  
  <p>and the result is </p>
  
  <blockquote>
    <p>faa3c04b058d38cecf1243421a596742a6cf1188</p>
  </blockquote>
  
  <p>so using this <a href="https://www.liavaag.org/English/SHA-Generator/HMAC/" rel="nofollow noreferrer">onlineHMAC</a> Generator  outputs the same result. But my desired output should be </p>
  
  <blockquote>
    <p>38b24d28d64f2459d42d1ecd1c9fa375ffeb369f</p>
  </blockquote>
  
  <p>and I can achieve this by changing the Key type to <strong>HEX</strong> in the page that I provided.</p>
  
  <p>So my problem now is how do I get the same output in my code? Do I need to convert <strong>key</strong> to hex? </p>