2022-05-01 14:07:43 +02:00
|
|
|
import SwiftUI
|
2022-05-01 18:30:30 +02:00
|
|
|
import CryptoKit
|
2022-05-01 14:07:43 +02:00
|
|
|
|
|
|
|
struct SingleKeyView: View {
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var needRefresh = false
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var keyManager: KeyManagement
|
2023-08-07 15:47:40 +02:00
|
|
|
|
|
|
|
@State
|
|
|
|
private var showEditWindow = false
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var keyText = ""
|
2022-05-01 14:07:43 +02:00
|
|
|
|
|
|
|
let type: KeyManagement.KeyType
|
|
|
|
|
|
|
|
private var generateText: String {
|
2022-05-01 18:30:30 +02:00
|
|
|
hasKey ? "Regenerate" : "Generate"
|
2022-05-01 14:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var hasKey: Bool {
|
|
|
|
keyManager.has(type)
|
|
|
|
}
|
|
|
|
|
|
|
|
var content: String {
|
|
|
|
keyManager.get(type)?.displayString ?? "-"
|
|
|
|
}
|
|
|
|
|
2022-05-01 18:30:30 +02:00
|
|
|
var copyText: String {
|
|
|
|
guard let key = keyManager.get(type)?.data else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
guard type.usesHashing else {
|
|
|
|
return key.hexEncoded
|
|
|
|
}
|
|
|
|
return SHA256.hash(data: key).hexEncoded
|
|
|
|
}
|
|
|
|
|
2022-05-01 14:07:43 +02:00
|
|
|
var body: some View {
|
|
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
|
|
Text(type.displayName)
|
|
|
|
.bold()
|
|
|
|
Text(needRefresh ? content : content)
|
|
|
|
.font(.system(.body, design: .monospaced))
|
2022-05-01 18:30:30 +02:00
|
|
|
.foregroundColor(.secondary)
|
2022-05-01 14:07:43 +02:00
|
|
|
HStack() {
|
|
|
|
Button(generateText) {
|
|
|
|
keyManager.generate(type)
|
|
|
|
needRefresh.toggle()
|
|
|
|
}
|
2022-05-02 23:24:48 +02:00
|
|
|
.padding([.horizontal, .bottom])
|
|
|
|
.padding(.top, 4)
|
2022-05-01 18:30:30 +02:00
|
|
|
|
|
|
|
Button(type.usesHashing ? "Copy hash" : "Copy") {
|
|
|
|
UIPasteboard.general.string = copyText
|
2022-05-01 14:07:43 +02:00
|
|
|
}
|
|
|
|
.disabled(!hasKey)
|
2022-05-02 23:24:48 +02:00
|
|
|
.padding([.horizontal, .bottom])
|
|
|
|
.padding(.top, 4)
|
2023-08-07 15:47:40 +02:00
|
|
|
Button("Edit") {
|
|
|
|
keyText = keyManager.get(type)?.displayString ?? ""
|
|
|
|
print("Set key text to '\(keyText)'")
|
|
|
|
showEditWindow = true
|
|
|
|
}
|
|
|
|
.padding([.horizontal, .bottom])
|
|
|
|
.padding(.top, 4)
|
2022-05-01 14:07:43 +02:00
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
2023-08-07 15:47:40 +02:00
|
|
|
.alert("Update key", isPresented: $showEditWindow, actions: {
|
|
|
|
TextField("Key data", text: $keyText)
|
|
|
|
.lineLimit(4)
|
|
|
|
.font(.system(.body, design: .monospaced))
|
|
|
|
.foregroundColor(.black)
|
|
|
|
Button("Save", action: saveKey)
|
|
|
|
Button("Cancel", role: .cancel, action: {})
|
|
|
|
}, message: {
|
|
|
|
Text("Enter the hex encoded key")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private func saveKey() {
|
|
|
|
let cleanText = keyText.replacingOccurrences(of: " ", with: "")
|
|
|
|
guard let keyData = Data(fromHexEncodedString: cleanText) else {
|
|
|
|
print("Invalid key string")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let keyLength = type.keyLength.bitCount
|
|
|
|
guard keyData.count * 8 == keyLength else {
|
|
|
|
print("Invalid key length \(keyData.count * 8) bits, expected \(keyLength)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
keyManager.save(type, data: keyData)
|
|
|
|
print("Key \(type) saved")
|
2022-05-01 14:07:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SingleKeyView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
SingleKeyView(
|
|
|
|
keyManager: .constant(KeyManagement()),
|
|
|
|
type: .deviceKey)
|
2023-08-07 15:47:40 +02:00
|
|
|
.previewLayout(.fixed(width: 350, height: 100))
|
2022-05-01 14:07:43 +02:00
|
|
|
}
|
|
|
|
}
|