Posts

Post marked as solved
7 Replies
4.3k Views
I am exited about CryptoKit and I tried to replace CommonCrypto with CryptoKit for a simple hash I was doing but the results are different. Can anyone tell me why I get different output from CommonCrypto and CryptoKit in the following code? func execute() { let password: String = "Apple!Crypt#Test123" print("password: \(password)") let commonCryptoHashPassword = hashPasswordCommonCrypto(password) print("CommonCrypto: \(commonCryptoHashPassword!)") let cryptoKitHashPassword = hashPasswordCryptoKit(password) print("CryptoKit: \(cryptoKitHashPassword!)") } // CommonCrypto func hashPasswordCommonCrypto(_ password: String) -> String? { // Create the password hash var digest = [UInt8](repeating: 1, count: Int(CC_SHA512_DIGEST_LENGTH)) CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA512), password, password.count, password, password.count, &digest) let passwordHashData = Data(bytes: digest) return formatPassword(passwordHashData) } // CrytoKit func hashPasswordCryptoKit(_ password: String) -> String? { // Create the hash let passwordData = Data(password.utf8) let passwordHashDigest = SHA512.hash(data: passwordData) return formatPassword(passwordHashDigest) } //Format password into readable string func formatPassword(_ password: Data) -> String { var passwordString : String = password.map { String(format: "%02hhx", $0) }.joined() // Add a dash after every 8 characters var index = 8 repeat { passwordString.insert("-", at: String.Index(encodedOffset: index)) index = index + 9 } while index < passwordString.count return passwordString } func formatPassword(_ password: SHA512Digest) -> String { var passwordString : String = password.map { String(format: "%02hhx", $0) }.joined() // Add a dash after every 8 characters var index = 8 repeat { passwordString.insert("-", at: String.Index(encodedOffset: index)) index = index + 9 } while index < passwordString.count return passwordString }Here is the output that I get.CommonCrypto:90cc8e53-49ad79dc-a01de5a0-718744e4-a1180758-fb8a9976-970d1dcb-513ecfdf-abdd8aa7-d152bd5c-17e0c924-e3bf9885-102383a2-2f471eb0-add0fd58-7cc21dd9CryptoKit:6650240f-d532b1b4-a40a1b96-25e2e64c-6d90d5d1-29a14243-0673e408-d3d3c6ea-41adf755-baccd663-a2be7c66-f221d75b-de45d5fe-0f920c9e-8f7d4a2f-af532b3b
Posted
by zach.m.
Last updated
.