Last active
June 29, 2020 02:07
-
-
Save cardosoedgar/8600755075395662f7b417d6d9aa90c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let algorithm: SecKeyAlgorithm = .eciesEncryptionStandardX963SHA1AESGCM | |
func decrypt(string: String) -> String? { | |
let attributes: [String: Any] = [ | |
kSecClass as String: kSecClassKey, | |
kSecAttrApplicationTag as String: "com.cardosoedgar.key", | |
kSecReturnRef as String: true] | |
var key: CFTypeRef? | |
let status = SecItemCopyMatching(attributes as CFDictionary, &key) | |
guard status == errSecSuccess else { return nil } | |
var error: Unmanaged<CFError>? | |
guard let data = Data(base64Encoded: string), | |
let decrypted = SecKeyCreateDecryptedData(key as! SecKey, algorithm, data as CFData, &error) else { return nil } | |
return String(data: decrypted as Data, encoding: .utf8) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment