title: Why is MD5 hashing so hard and in Swift 3?
body: <p>Ok, so every now and then you come across problems that you've solved before using various frameworks and libraries and whatnot found on the internet and your problem is solved relatively quick and easy and you also learn why your problem was a problem in the first place.</p>
  
  <p>However, sometimes you come across problems that make absolute 0 sense, and even worse when the solutions make negative sense.</p>
  
  <p>My problem is that I want to take <code>Data</code> and make an MD5 hash out of it.</p>
  
  <p>I find all kinds of solutions but none of them work.</p>
  
  <p>What's really bugging me out actually is how unnecessarily complicated the solutions seem to be for a trivial task as getting an MD5 hash out of anything.</p>
  
  <p>I am trying to use the <code>Crypto</code> and <code>CommonCrypto</code> frameworks by <code>Soffes</code> and they seem fairly easy, right? Right?</p>
  
  <p>Yes!</p>
  
  <p>But why am I still getting the error <code>fatal error: unexpectedly found nil while unwrapping an Optional value</code>?</p>
  
  <p>From what I understand, the data served by <code>myData.md5</code> in the extension of <code>Crypto</code> by <code>Soffes</code> seem to be "optional". But why?</p>
  
  <p>The code I am trying to execute is:</p>
  
  <pre><code>print(" md5 result: " + String(data: myData.md5, encoding: .utf8)!)
  </code></pre>
  
  <p>where <code>myData</code> has data in it 100% because after the above line of code, I send that data to a server, and the data exists.</p>
  
  <p>On top of that, printing the count of <code>myData.md5.count</code> by <code>print(String(myData.md5.count))</code> works perfectly.</p>
  
  <p>So my question is basically: How do I MD5 hash a Data and print it as a string?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p><strong>What I have tried</strong></p>
  
  <p><strong>That works</strong></p>
  
  <p>MD5:ing the string <code>test</code> in a PHP script gives me <code>098f6bcd4621d373cade4e832627b4f6</code>
  and the Swift code <code>"test".md5()</code> also gives me <code>098f6bcd4621d373cade4e832627b4f6</code></p>
  
  <p><strong>That doesn't work</strong></p>
  
  <p>Converting the <code>UInt8</code> byte array from <code>Data.md5()</code> to a string that represents the correct MD5 value.</p>
  
  <p>The different tests I've done are the following:</p>
  
  <pre><code>var hash = ""
  for byte in myData.data.md5() {
      hash +=  String(format: "%02x", byte)
  }
  print("loop = " + hash) //test 1
  
  print("myData.md5().toHexString() = " +  myData.md5().toHexString()) //test 2
  
  print("CryptoSwift.Digest.md5([UInt8](myData)) = " + CryptoSwift.Digest.md5([UInt8](myData)).toHexString()) //test 3
  </code></pre>
  
  <p>All three tests with the 500 byte test data give me the MD5 value <code>56f6955d148ad6b6abbc9088b4ae334d</code>
  while my PHP script gives me <code>6081d190b3ec6de47a74d34f6316ac6b</code></p>
  
  <p><strong>Test Sample (64 bytes):</strong>
  Raw data:</p>
  
  <pre><code>FFD8FFE0 00104A46 49460001 01010048 00480000 FFE13572 45786966 00004D4D
  002A0000 0008000B 01060003 00000001 00020000 010F0002 00000012 00000092
  </code></pre>
  
  <p>Test 1, 2 and 3 MD5: <code>7f0a012239d9fde5a46071640d2d8c83</code></p>
  
  <p>PHP MD5: <code>06eb0c71d8839a4ac91ee42c129b8ba3</code></p>
  
  <p>PHP Code: <code>echo md5($_FILES["file"]["tmp_name"])</code></p>