修改加密示例

This commit is contained in:
Fin 2023-03-21 16:07:34 +08:00
parent a14014f509
commit f05b41419b
No known key found for this signature in database
GPG Key ID: CFB59B99D87A7B93
4 changed files with 22 additions and 9 deletions

View File

@ -139,3 +139,8 @@ faqUrl = "https://bark.day.app/#/en-us/faq";
docUrl = "https://bark.day.app/#/en-us/?id=bark";
encryptionUrl = "https://bark.day.app/#/en-us/encryption";
documentation = "Documentation";
ivComment = "IV can be randomly generated, but if it is random, it needs to be passed in the iv parameter.";
opensslEncodingComment = "openssl requires Hex encoding of manual keys and IVs, not ASCII encoding.";
ciphertextComment = "URL encoding the ciphertext, there may be special characters.";
consoleComment = "The console will print";
keyComment = "Must be %d bit long";

View File

@ -142,3 +142,8 @@ faqUrl = "https://bark.day.app/#/faq";
docUrl = "https://bark.day.app/#/?id=bark";
encryptionUrl = "https://bark.day.app/#/encryption";
documentation = "使用文档";
ivComment = "IV可以是随机生成的但如果是随机的就需要放在 iv 参数里传递。";
opensslEncodingComment = "OpenSSL 要求输入的 Key 和 IV 需使用十六进制编码。";
ciphertextComment = "密文可能有特殊字符,所以记得 URL 编码一下。";
consoleComment = "控制台将打印";
keyComment = "必须%d位";

View File

@ -98,6 +98,8 @@ class CryptoSettingViewModel: ViewModel, ViewModelType {
}
let copy = Driver.combineLatest(copyScript, dependencies.deviceKey, dependencies.serverAddress)
.map { fields, deviceKey,serverAddress in
let key = fields.key ?? ""
let iv = fields.iv ?? ""
return
"""
#!/usr/bin/env bash
@ -111,21 +113,22 @@ class CryptoSettingViewModel: ViewModel, ViewModelType {
# push payload
json='{"body": "test", "sound": "birdsong"}'
# must be \(Int(fields.algorithm.suffix(3))! / 8) bit long
key='\(fields.key ?? "")'
iv='\(fields.iv ?? "")'
# \(String(format: NSLocalizedString("keyComment"), Int(fields.algorithm.suffix(3))! / 8))
key='\(key)'
# \(NSLocalizedString("ivComment"))
iv='\(iv)'
# openssl requires Hex encoding of manual keys and IVs, not ASCII encoding.
# \(NSLocalizedString("opensslEncodingComment"))
key=$(printf $key | xxd -ps -c 200)
iv=$(printf $iv | xxd -ps -c 200)
ciphertext=$(echo -n $json | openssl enc -aes-\(fields.algorithm.suffix(3))-\(fields.mode.lowercased()) -K $key -iv $iv | base64)
ciphertext=$(echo -n $json | openssl enc -aes-\(fields.algorithm.suffix(3))-\(fields.mode.lowercased()) -K $key \(iv.count > 0 ? "-iv $iv " : "")| base64)
# The console will print "\((try? AESCryptoModel(cryptoFields: fields).encrypt(text: "{\"body\": \"test\", \"sound\": \"birdsong\"}")) ?? "")"
# \(NSLocalizedString("consoleComment")) "\((try? AESCryptoModel(cryptoFields: fields).encrypt(text: "{\"body\": \"test\", \"sound\": \"birdsong\"}")) ?? "")"
echo $ciphertext
# URL encoding the ciphertext, there may be special characters.
curl --data-urlencode "ciphertext=$ciphertext" \(serverAddress)/$deviceKey
# \(NSLocalizedString("ciphertextComment"))
curl --data-urlencode "ciphertext=$ciphertext"\( iv.count == 0 ? "" : " --data-urlencode \"iv=\(iv)\"") \(serverAddress)/$deviceKey
"""
}

View File

@ -239,7 +239,7 @@ class NotificationService: UNNotificationServiceExtension {
throw "No encryption key set"
}
if let iv = iv {
// Support using specified IV decryption
// Support using specified IV parameter for decryption
fields.iv = iv
}