title: Hash string with SHA512 in Swift
body: <p>Anyone knows how to reproduce PHP hashing method hash(‘SHA512’, $value, true) in swift ? I tried to use CommonCrypto C library with this code :</p>
  
  <pre><code>extension String {
      func digest(length:Int32, gen:(data: UnsafePointer&lt;Void&gt;, len: CC_LONG, md: UnsafeMutablePointer&lt;UInt8&gt;) -&gt; UnsafeMutablePointer&lt;UInt8&gt;) -&gt; String {
          var cStr = [UInt8](self.utf8)
          var result = [UInt8](count:Int(length), repeatedValue:0)
          gen(data: &amp;cStr, len: CC_LONG(cStr.count), md: &amp;result)
          let output = NSMutableString(capacity:Int(length))
          for r in result {
              output.appendFormat("%02x", r)
          }
          return String(output)
      }
  }
  </code></pre>
  
  <p>and used it like this :</p>
  
  <pre><code>var digest = salted.digest(CC_SHA512_DIGEST_LENGTH, gen: {(data, len, md) in CC_SHA512(data,len,md)})
  </code></pre>
  
  <p>But I don't get the right output</p>
  
  <p><strong>EDIT :</strong></p>
  
  <p>I have some PHP code :</p>
  
  <pre><code>echo base64_encode(hash('sha512', '8yOrBmkd', true)); // output: rlltLWeWaQCrfNTYMa0CcIs0mfLoHGAynrd+d8H65+rGAzHS/BSWsumwSmcxF9aAG9TIzXx+HOjArPyLL3herg==
  </code></pre>
  
  <p>And I want my swift code to output the same. The Base64 encoding is ok :</p>
  
  <pre><code>let utf8str = input.dataUsingEncoding(NSUTF8StringEncoding)!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
                  let base64Encoded = utf8str!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
                  print("Encoded:  \(base64Encoded)")
  </code></pre>
  
  <p>But the hashing part return an output the same value as this PHP code :</p>
  
  <pre><code>echo base64_encode(hash('sha512', '8yOrBmkd', false)); // output: YWU1OTZkMmQ2Nzk2NjkwMGFiN2NkNGQ4MzFhZDAyNzA4YjM0OTlmMmU4MWM2MDMyOWViNzdlNzdjMWZhZTdlYWM2MDMzMWQyZmMxNDk2YjJlOWIwNGE2NzMxMTdkNjgwMWJkNGM4Y2Q3YzdlMWNlOGMwYWNmYzhiMmY3ODVlYWU=
  </code></pre>