Skip to content

Instantly share code, notes, and snippets.

@mkaraoz
Created March 15, 2021 23:23
Show Gist options
  • Save mkaraoz/2e22f17f5c0a5366938f3c1e7a5d92ed to your computer and use it in GitHub Desktop.
Save mkaraoz/2e22f17f5c0a5366938f3c1e7a5d92ed to your computer and use it in GitHub Desktop.
func sign(text2sign : String) throws -> Data {
var error: Unmanaged<CFError>?
let privateKey = try! retrievePrivateKey()
let algorithm: SecKeyAlgorithm = .ecdsaSignatureMessageX962SHA512
guard SecKeyIsAlgorithmSupported(privateKey, .sign, algorithm) else {
throw error!.takeRetainedValue() as Error
}
let data2sign = text2sign.data(using: .utf8)!
// create the signature
guard let signature = SecKeyCreateSignature(privateKey, algorithm, data2sign as CFData, &error) as Data? else {
throw error!.takeRetainedValue() as Error
}
print("Signature: " + signature.base64EncodedString())
return signature;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment