import CryptoSwift import CommonCrypto #if canImport(CryptoKit) import CryptoKit #endif public final class PublicKeyPinner { ... /// Creates a hash from the received data using the `sha256` algorithm. /// `Returns` the `base64` encoded representation of the hash. /// /// To replicate the output of the `openssl dgst -sha256` command, an array of specific bytes need to be appended to /// the beginning of the data to be hashed. /// - Parameter data: The data to be hashed. private func hash(data: Data) -> String { // Add the missing ASN1 header for public keys to re-create the subject public key info var keyWithHeader = Data(rsa2048Asn1Header) keyWithHeader.append(data) // Check if iOS 13 is available, and use CryptoKit's hasher if #available(iOS 13, *) { return Data(SHA256.hash(data: keyWithHeader)).base64EncodedString() } else { ... } } }