Skip to content

Instantly share code, notes, and snippets.

@algrid
Created June 5, 2019 20:22
Show Gist options
  • Select an option

  • Save algrid/6589deb56a3cecf2811484b4e1a12d7b to your computer and use it in GitHub Desktop.

Select an option

Save algrid/6589deb56a3cecf2811484b4e1a12d7b to your computer and use it in GitHub Desktop.
Calculating digital signature using a key from iOS Secure Enclave
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