最近,Apple 在 iOS 13 中引入了 CryptoKit,它有一个方法 sharedSecretFromKeyAgreement,它根据公钥和私钥之间的密钥协议生成共享密钥。这如何在 iOS 12 及更低版本中实现?
iOS 13
import CryptoKit
let alicePrivateKey = P256.KeyAgreement.PrivateKey()
let alicePublicKey = alicePrivateKey.publicKey
let eileenPrivateKey = P256.KeyAgreement.PrivateKey()
let eileenPublicKey = eileenPrivateKey.publicKey
let shared1 = try alicePrivateKey.sharedSecretFromKeyAgreement(with: eileenPublicKey)
let shared2 = try eileenPrivateKey.sharedSecretFromKeyAgreement(with: alicePublicKey)
if shared1 == shared2 {
print("shared keys are equal")
}