Skip to content

Instantly share code, notes, and snippets.

@BrandonRoehl
Forked from bleft/Data+Extension.swift
Last active December 6, 2023 01:20
Show Gist options
  • Select an option

  • Save BrandonRoehl/5233deb8b84bbac90d23a6e3dce2660f to your computer and use it in GitHub Desktop.

Select an option

Save BrandonRoehl/5233deb8b84bbac90d23a6e3dce2660f to your computer and use it in GitHub Desktop.
get md5 hash from Data in swift
import Foundation
import CommonCrypto
extension Data {
var md5 : String {
var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
_ = self.withUnsafeBytes { bytes in
CC_MD5(bytes, CC_LONG(self.count), &digest)
}
var digestHex = ""
for index in 0..<Int(CC_MD5_DIGEST_LENGTH) {
digestHex += String(format: "%02x", digest[index])
}
return digestHex
}
}
@BrandonRoehl
Copy link
Author

Just forked because I always have to remember the imports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment