Support using specified IV decryption.

close: #188
This commit is contained in:
Fin 2023-03-15 16:57:17 +08:00
parent fdfbb06e6c
commit a14014f509
No known key found for this signature in database
GPG Key ID: CFB59B99D87A7B93
2 changed files with 10 additions and 5 deletions

View File

@ -45,7 +45,7 @@ struct CryptoSettingFields: Codable {
let mode: String let mode: String
let padding: String let padding: String
let key: String? let key: String?
let iv: String? var iv: String?
} }
struct AESCryptoModel { struct AESCryptoModel {

View File

@ -234,10 +234,15 @@ class NotificationService: UNNotificationServiceExtension {
} }
} }
func decrypt(ciphertext: String) throws -> [String: Any] { func decrypt(ciphertext: String, iv: String? = nil) throws -> [String: Any] {
guard let fields = CryptoSettingManager.shared.fields else { guard var fields = CryptoSettingManager.shared.fields else {
throw "No encryption key set" throw "No encryption key set"
} }
if let iv = iv {
// Support using specified IV decryption
fields.iv = iv
}
let aes = try AESCryptoModel(cryptoFields: fields) let aes = try AESCryptoModel(cryptoFields: fields)
let json = try aes.decrypt(ciphertext: ciphertext) let json = try aes.decrypt(ciphertext: ciphertext)
@ -258,8 +263,8 @@ class NotificationService: UNNotificationServiceExtension {
// 使 bestAttemptContent // 使 bestAttemptContent
if let ciphertext = userInfo["ciphertext"] as? String { if let ciphertext = userInfo["ciphertext"] as? String {
do { do {
var map = try decrypt(ciphertext: ciphertext) var map = try decrypt(ciphertext: ciphertext, iv: userInfo["iv"] as? String)
for (key,val) in map { for (key, val) in map {
// key // key
map[key.lowercased()] = val map[key.lowercased()] = val
} }