From 8a2a7fc2b44073498e4abea54f62497a0e06926e Mon Sep 17 00:00:00 2001 From: Fin Date: Mon, 9 Dec 2024 15:57:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=81=E8=AE=B8=E4=BD=BF=E7=94=A8GCM=20Mode?= =?UTF-8?q?=20close:=20#262?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bark/Localizable.xcstrings | 23 +++++++++++++++++++++++ Controller/CryptoSettingController.swift | 23 ++++++++++++++++++++--- Controller/CryptoSettingViewModel.swift | 19 ++++++++++--------- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/Bark/Localizable.xcstrings b/Bark/Localizable.xcstrings index 8c301c09..d2e3d776 100644 --- a/Bark/Localizable.xcstrings +++ b/Bark/Localizable.xcstrings @@ -1403,6 +1403,29 @@ } } }, + "gcmNotSupported" : { + "extractionState" : "manual", + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "GCM mode does not support copying script example." + } + }, + "tr" : { + "stringUnit" : { + "state" : "translated", + "value" : "GCM modu, script örneklerini kopyalamayı desteklemez." + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "GCM Mode 不支持复制脚本示例。" + } + } + } + }, "group" : { "extractionState" : "manual", "localizations" : { diff --git a/Controller/CryptoSettingController.swift b/Controller/CryptoSettingController.swift index 2b900e13..a534ef00 100644 --- a/Controller/CryptoSettingController.swift +++ b/Controller/CryptoSettingController.swift @@ -26,7 +26,6 @@ class CryptoSettingController: BaseViewController { let textField = BorderTextField(title: "IV") textField.font = UIFont.preferredFont(ofSize: 14) textField.adjustsFontForContentSizeCategory = true - textField.placeholder = String(format: NSLocalizedString("enterIv"), 16) // todo: update iv length return textField }() @@ -50,7 +49,7 @@ class CryptoSettingController: BaseViewController { btn.applyGradient( withColours: [ UIColor(r255: 36, g255: 51, b255: 236), - UIColor(r255: 70, g255: 44, b255: 233), + UIColor(r255: 70, g255: 44, b255: 233) ], gradientOrientation: .horizontal ) @@ -174,7 +173,6 @@ class CryptoSettingController: BaseViewController { } override func bindViewModel() { - func getFieldValues() -> CryptoSettingFields { return CryptoSettingFields( algorithm: self.algorithmFeild.currentValue!, @@ -216,6 +214,7 @@ class CryptoSettingController: BaseViewController { self?.keyTextField.text = fields.key self?.ivTextField.text = fields.iv } + self?.setIvLengthPlaceholder(mode: self?.modeFeild.currentValue) }).disposed(by: rx.disposeBag) output.modeListChanged @@ -229,6 +228,13 @@ class CryptoSettingController: BaseViewController { output.keyLengthChanged.drive(onNext: { [weak self] keyLength in self?.keyTextField.placeholder = String(format: NSLocalizedString("enterKey"), keyLength) }).disposed(by: rx.disposeBag) + + self.modeFeild + .rx + .currentValueChanged + .subscribe(onNext: { [weak self] val in + self?.setIvLengthPlaceholder(mode: val) + }).disposed(by: rx.disposeBag) output.showSnackbar.drive(onNext: { text in HUDError(text) @@ -243,4 +249,15 @@ class CryptoSettingController: BaseViewController { HUDSuccess(NSLocalizedString("Copy")) }).disposed(by: rx.disposeBag) } + + private func setIvLengthPlaceholder(mode: String?) { + guard let mode else { + return + } + if let length = ["CBC": 16, "GCM": 12][mode] { + self.ivTextField.placeholder = String(format: NSLocalizedString("enterIv"), length) + } else { + self.ivTextField.placeholder = "" + } + } } diff --git a/Controller/CryptoSettingViewModel.swift b/Controller/CryptoSettingViewModel.swift index 7f312c8a..ec08f84b 100644 --- a/Controller/CryptoSettingViewModel.swift +++ b/Controller/CryptoSettingViewModel.swift @@ -48,7 +48,6 @@ class CryptoSettingViewModel: ViewModel, ViewModelType { } func transform(input: Input) -> Output { - let showSnackbar = PublishRelay() let modeList = input @@ -63,7 +62,7 @@ class CryptoSettingViewModel: ViewModel, ViewModelType { .compactMap { Algorithm(rawValue: $0.algorithm)?.keyLength }, input .algorithmChanged - .compactMap { Algorithm(rawValue: $0)?.keyLength }, + .compactMap { Algorithm(rawValue: $0)?.keyLength } ]) // 保存配置 @@ -72,8 +71,7 @@ class CryptoSettingViewModel: ViewModel, ViewModelType { do { _ = try AESCryptoModel(cryptoFields: fields) return true - } - catch { + } catch { showSnackbar.accept(error.rawString()) return false } @@ -90,14 +88,17 @@ class CryptoSettingViewModel: ViewModel, ViewModelType { // 保存配置 self?.dependencies.settingFieldRelay.accept(fields) return true - } - catch { + } catch { showSnackbar.accept(error.rawString()) return false } } let copy = Driver.combineLatest(copyScript, dependencies.deviceKey, dependencies.serverAddress) - .map { fields, deviceKey,serverAddress in + .compactMap { fields, deviceKey, serverAddress -> String? in + guard fields.mode != "GCM" else { + showSnackbar.accept(NSLocalizedString("gcmNotSupported")) + return nil + } let key = fields.key ?? "" let iv = fields.iv ?? "" return @@ -129,14 +130,14 @@ class CryptoSettingViewModel: ViewModel, ViewModelType { echo $ciphertext # \(NSLocalizedString("ciphertextComment")) - curl --data-urlencode "ciphertext=$ciphertext"\( iv.count == 0 ? "" : " --data-urlencode \"iv=\(iv)\"") \(serverAddress)/$deviceKey + curl --data-urlencode "ciphertext=$ciphertext"\(iv.count == 0 ? "" : " --data-urlencode \"iv=\(iv)\"") \(serverAddress)/$deviceKey """ } return Output( initial: Driver.just(( algorithmList: [Algorithm.aes128, Algorithm.aes192, Algorithm.aes256], - modeList: ["CBC", "ECB", /* "GCM" */], // GCM 还没准备好示例代码,暂时禁用 + modeList: ["CBC", "ECB", "GCM"], paddingList: ["pkcs7"], initialFields: dependencies.settingFieldRelay.value )),