Skip to content

Commit

Permalink
允许使用GCM Mode
Browse files Browse the repository at this point in the history
close: #262
  • Loading branch information
Finb committed Dec 9, 2024
1 parent 585d0b8 commit 8a2a7fc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
23 changes: 23 additions & 0 deletions Bark/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -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" : {
Expand Down
23 changes: 20 additions & 3 deletions Controller/CryptoSettingController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class CryptoSettingController: BaseViewController<CryptoSettingViewModel> {
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
}()

Expand All @@ -50,7 +49,7 @@ class CryptoSettingController: BaseViewController<CryptoSettingViewModel> {
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
)
Expand Down Expand Up @@ -174,7 +173,6 @@ class CryptoSettingController: BaseViewController<CryptoSettingViewModel> {
}

override func bindViewModel() {

func getFieldValues() -> CryptoSettingFields {
return CryptoSettingFields(
algorithm: self.algorithmFeild.currentValue!,
Expand Down Expand Up @@ -216,6 +214,7 @@ class CryptoSettingController: BaseViewController<CryptoSettingViewModel> {
self?.keyTextField.text = fields.key
self?.ivTextField.text = fields.iv
}
self?.setIvLengthPlaceholder(mode: self?.modeFeild.currentValue)
}).disposed(by: rx.disposeBag)

output.modeListChanged
Expand All @@ -229,6 +228,13 @@ class CryptoSettingController: BaseViewController<CryptoSettingViewModel> {
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)
Expand All @@ -243,4 +249,15 @@ class CryptoSettingController: BaseViewController<CryptoSettingViewModel> {
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 = ""
}
}
}
19 changes: 10 additions & 9 deletions Controller/CryptoSettingViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class CryptoSettingViewModel: ViewModel, ViewModelType {
}

func transform(input: Input) -> Output {

let showSnackbar = PublishRelay<String>()

let modeList = input
Expand All @@ -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 }
])

// 保存配置
Expand All @@ -72,8 +71,7 @@ class CryptoSettingViewModel: ViewModel, ViewModelType {
do {
_ = try AESCryptoModel(cryptoFields: fields)
return true
}
catch {
} catch {
showSnackbar.accept(error.rawString())
return false
}
Expand All @@ -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
Expand Down Expand Up @@ -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
)),
Expand Down

0 comments on commit 8a2a7fc

Please sign in to comment.