Created
June 5, 2019 20:22
-
-
Save algrid/6589deb56a3cecf2811484b4e1a12d7b to your computer and use it in GitHub Desktop.
Calculating digital signature using a key from iOS Secure Enclave
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
| private func sign(algorithm: SecKeyAlgorithm, data: Data) { | |
| guard SecKeyIsAlgorithmSupported(key!, .sign, algorithm) else { | |
| UIAlertController.showSimple(title: "Can't sign", | |
| text: "Algorith not supported", | |
| from: self) | |
| return | |
| } | |
| // SecKeyCreateSignature call is blocking when the used key | |
| // is protected by biometry authentication. If that's not the case, | |
| // dispatching to a background thread isn't necessary. | |
| DispatchQueue.global().async { | |
| var error: Unmanaged<CFError>? | |
| let signature = SecKeyCreateSignature(self.key!, algorithm, | |
| data as CFData, | |
| &error) as Data? | |
| DispatchQueue.main.async { | |
| self.signature = signature | |
| guard signature != nil else { | |
| UIAlertController.showSimple(title: "Can't sign", | |
| text: (error!.takeRetainedValue() as Error).localizedDescription, | |
| from: self) | |
| return | |
| } | |
| // signature is a Data instance containing the digital signature value | |
| // ... | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment