Hi
I'm trying to use SecKeyCreateSignature to sign a string but seem to have missed something. Here is my code snippet:
public static func sign(using name: String, value: String, base64EncodingOptions: Data.Base64EncodingOptions = []) -> String?
{
// Get private key by name using SecItemCopyMatching
guard let privateKey: SecKey = getPrivateKey(name) else
{
return nil
}
let data = value.data(using: .utf8)!
var error: Unmanaged<CFError>?
guard let signedData = SecKeyCreateSignature(privateKey,
.rsaSignatureDigestPKCS1v15SHA512,
data as CFData,
&error) as Data? else
{
return nil
}
return signedData.base64EncodedString(options: base64EncodingOptions)
}
And to call the method:
let result = sign("MyKey", "hello world")
print(result)
Using the public key generated on iOS and exported to Java, the validation fails. I'm think it might be something to do with value.data(using: .utf8) but I'm not sure. Using the older style SecKeyRawSign you would hash the "hello world" value before invoking SecKeyRawSign
Any ideas or a simple example would be great 🙂