You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.1 KiB
45 lines
1.1 KiB
//
|
|
// KeyedEncodingContainer+Extensions.swift
|
|
// PadelClub
|
|
//
|
|
// Created by Laurent Morvillier on 18/09/2024.
|
|
//
|
|
|
|
import Foundation
|
|
import LeStorage
|
|
|
|
extension KeyedDecodingContainer {
|
|
|
|
func decodeEncrypted(key: Key) throws -> String {
|
|
let data = try self.decode(Data.self, forKey: key)
|
|
return try data.decryptData(pass: CryptoKey.pass.rawValue)
|
|
}
|
|
|
|
func decodeEncryptedIfPresent(key: Key) throws -> String? {
|
|
let data = try self.decodeIfPresent(Data.self, forKey: key)
|
|
if let data {
|
|
return try data.decryptData(pass: CryptoKey.pass.rawValue)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
}
|
|
|
|
extension KeyedEncodingContainer {
|
|
|
|
mutating func encodeAndEncrypt(_ value: Data, forKey key: Key) throws {
|
|
let encryped: Data = try value.encrypt(pass: CryptoKey.pass.rawValue)
|
|
try self.encode(encryped, forKey: key)
|
|
}
|
|
|
|
mutating func encodeAndEncryptIfPresent(_ value: Data?, forKey key: Key) throws {
|
|
guard let value else {
|
|
try encodeNil(forKey: key)
|
|
return
|
|
}
|
|
try self.encodeAndEncrypt(value, forKey: key)
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|