Skip to content

Instantly share code, notes, and snippets.

@tsif
Last active December 16, 2020 20:56
Show Gist options
  • Save tsif/96b57da2195e4ae2dc9b1c039639f42a to your computer and use it in GitHub Desktop.
Save tsif/96b57da2195e4ae2dc9b1c039639f42a to your computer and use it in GitHub Desktop.
let salt = "Salt".data(using: .utf8)!
let firstPrivateKey = P256.KeyAgreement.PrivateKey()
let firstPublicKey = firstPrivateKey.publicKey
let secondPrivateKey = P256.KeyAgreement.PrivateKey()
let secondPublicKey = secondPrivateKey.publicKey
let firstSharedSecret = try! firstPrivateKey.sharedSecretFromKeyAgreement(with: secondPublicKey)
let firstSymmetricKey = firstSharedSecret.hkdfDerivedSymmetricKey(using: SHA256.self,
salt: salt,
sharedInfo: Data(),
outputByteCount: 32)
let secondSharedSecret = try! secondPrivateKey.sharedSecretFromKeyAgreement(with: firstPublicKey)
let secondSymmetricKey = secondSharedSecret.hkdfDerivedSymmetricKey(using: SHA256.self,
salt: salt,
sharedInfo: Data(),
outputByteCount: 32)
if firstSymmetricKey == secondSymmetricKey {
print("First and second have the same key to do as they please")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment