Last active
August 8, 2023 18:49
-
-
Save InfiniteFalltrough/41ba81de1a9a5643480c5bb57a5f5c52 to your computer and use it in GitHub Desktop.
CryptoKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import CryptoKit | |
let data = "KEK! LOL!" | |
func generatePrivateKey() -> P256.KeyAgreement.PrivateKey { | |
let privateKey = P256.KeyAgreement.PrivateKey() | |
return privateKey | |
} | |
let privateKey = generatePrivateKey() | |
print(privateKey) | |
let publicKey = privateKey.publicKey | |
print(publicKey) | |
func exportPrivateKey(_ privateKey: P256.KeyAgreement.PrivateKey) -> String { | |
let rawPrivateKey = privateKey.rawRepresentation | |
let privateKeyBase64 = rawPrivateKey.base64EncodedString() | |
let percentEncodedPrivateKey = privateKeyBase64.addingPercentEncoding(withAllowedCharacters: .alphanumerics)! | |
return percentEncodedPrivateKey | |
} | |
let exportedPK = exportPrivateKey(privateKey) | |
print(exportedPK) | |
func importPrivateKey(_ privateKey: String) throws -> P256.KeyAgreement.PrivateKey { | |
let privateKeyBase64 = privateKey.removingPercentEncoding! | |
let rawPrivateKey = Data(base64Encoded: privateKeyBase64)! | |
return try P256.KeyAgreement.PrivateKey(rawRepresentation: rawPrivateKey) | |
} | |
let importedPK = try! importPrivateKey(exportedPK) | |
print(importedPK) | |
func deriveSymmetricKey(privateKey: P256.KeyAgreement.PrivateKey, publicKey: P256.KeyAgreement.PublicKey) throws -> SymmetricKey { | |
let sharedSecret = try privateKey.sharedSecretFromKeyAgreement(with: publicKey) | |
let symmetricKey = sharedSecret.hkdfDerivedSymmetricKey( | |
using: SHA256.self, | |
salt: "Salt".data(using: .utf8)!, | |
sharedInfo: Data(), | |
outputByteCount: 32 | |
) | |
return symmetricKey | |
} | |
let symmetricKey = try! deriveSymmetricKey(privateKey: privateKey, publicKey: publicKey) | |
print(symmetricKey) | |
// text encrypting example | |
func encrypt(text: String, symmetricKey: SymmetricKey) throws -> String { | |
let textData = text.data(using: .utf8)! | |
let encrypted = try AES.GCM.seal(textData, using: symmetricKey) | |
return encrypted.combined!.base64EncodedString() | |
} | |
let encryptedData = try! encrypt(text: data, symmetricKey: symmetricKey) | |
print(encryptedData) | |
// text decrypting example | |
func decrypt(text: String, symmetricKey: SymmetricKey) -> String { | |
do { | |
guard let data = Data(base64Encoded: text) else { | |
return "Could not decode text: \(text)" | |
} | |
let sealedBox = try AES.GCM.SealedBox(combined: data) | |
let decryptedData = try AES.GCM.open(sealedBox, using: symmetricKey) | |
guard let text = String(data: decryptedData, encoding: .utf8) else { | |
return "Could not decode data: \(decryptedData)" | |
} | |
return text | |
} catch let error { | |
return "Error decrypting message: \(error.localizedDescription)" | |
} | |
} | |
let decryptedData = decrypt(text: encryptedData, symmetricKey: symmetricKey) | |
print(decryptedData) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment