Skip to content

Instantly share code, notes, and snippets.

@zonble
Created June 19, 2019 08:57
Show Gist options
  • Select an option

  • Save zonble/abb59b110d7c8960c3e0734f9a96b919 to your computer and use it in GitHub Desktop.

Select an option

Save zonble/abb59b110d7c8960c3e0734f9a96b919 to your computer and use it in GitHub Desktop.
import UIKit
import CryptoKit
import CommonCrypto
let str = "Boom boom boom boom boom boom"
// CryptoKit
func MD5CryptoKit(string: String) -> Data {
string.data(using:.utf8)!.withUnsafeBytes { ptr in
Data(Insecure.MD5.hash(bufferPointer: ptr).map { $0 }[0..<Insecure.MD5.byteCount])
}
}
MD5CryptoKit(string: str).base64EncodedString()
// Common Crypto
func MD5CommonCrypto(string: String) -> Data {
let length = Int(CC_MD5_DIGEST_LENGTH)
let messageData = string.data(using:.utf8)!
var digestData = Data(count: length)
_ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
messageData.withUnsafeBytes { messageBytes -> UInt8 in
if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
let messageLength = CC_LONG(messageData.count)
CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
}
return 0
}
}
return digestData
}
MD5CommonCrypto(string: str).base64EncodedString()
@HIROK1
Copy link

HIROK1 commented Oct 20, 2021

👍

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