Skip to content

Instantly share code, notes, and snippets.

@shnhrrsn
Created September 30, 2019 23:55
Show Gist options
  • Save shnhrrsn/c55f1e686b4bdf0d78d62b456fc2a3a1 to your computer and use it in GitHub Desktop.
Save shnhrrsn/c55f1e686b4bdf0d78d62b456fc2a3a1 to your computer and use it in GitHub Desktop.
Swift CryptoKit MD5/SHA1 Hash
import Foundation
import CryptoKit
private protocol ByteCountable {
static var byteCount: Int { get }
}
extension Insecure.MD5: ByteCountable { }
extension Insecure.SHA1: ByteCountable { }
public extension String {
func insecureMD5Hash(using encoding: String.Encoding = .utf8) -> String? {
return self.hash(algo: Insecure.MD5.self, using: encoding)
}
func insecureSHA1Hash(using encoding: String.Encoding = .utf8) -> String? {
return self.hash(algo: Insecure.SHA1.self, using: encoding)
}
private func hash<Hash: HashFunction & ByteCountable>(algo: Hash.Type, using encoding: String.Encoding = .utf8) -> String? {
guard let data = self.data(using: encoding) else {
return nil
}
return algo.hash(data: data).prefix(algo.byteCount).map {
String(format: "%02hhx", $0)
}.joined()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment