diff options
author | Roopesh Chander <roop@roopc.net> | 2018-10-24 14:11:34 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2018-10-27 15:13:01 +0530 |
commit | 571e9b582647ecca9cb679ed30333c2e74959e54 (patch) | |
tree | a6270ef8792278331a1d2627a11481288d792cb1 /WireGuard/WireGuard/UI/TunnelViewModel.swift | |
parent | Tunnel view model: Populate interface's public key (diff) | |
download | wireguard-apple-571e9b582647ecca9cb679ed30333c2e74959e54.tar.xz wireguard-apple-571e9b582647ecca9cb679ed30333c2e74959e54.zip |
Tunnel creation: Compute public key from private key as the text is being edited
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/UI/TunnelViewModel.swift')
-rw-r--r-- | WireGuard/WireGuard/UI/TunnelViewModel.swift | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/UI/TunnelViewModel.swift b/WireGuard/WireGuard/UI/TunnelViewModel.swift index b28abeb..e7ab12d 100644 --- a/WireGuard/WireGuard/UI/TunnelViewModel.swift +++ b/WireGuard/WireGuard/UI/TunnelViewModel.swift @@ -27,6 +27,8 @@ class TunnelViewModel { case deletePeer = "Delete peer" } + static let keyLengthInBase64 = 44 + class InterfaceData { var scratchpad: [InterfaceField: String] = [:] var fieldsWithError: Set<InterfaceField> = [] @@ -53,6 +55,16 @@ class TunnelViewModel { } else { scratchpad[field] = stringValue } + if (field == .privateKey) { + if (stringValue.count == TunnelViewModel.keyLengthInBase64), + let privateKey = Data(base64Encoded: stringValue), + privateKey.count == 32 { + let publicKey = Curve25519.generatePublicKey(fromPrivateKey: privateKey) + scratchpad[.publicKey] = publicKey.base64EncodedString() + } else { + scratchpad.removeValue(forKey: .publicKey) + } + } } } |