mirror of
https://github.com/metabolist/metatext.git
synced 2024-05-18 16:28:21 +00:00
29 lines
755 B
Swift
29 lines
755 B
Swift
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
import Base16
|
|
import CryptoKit
|
|
import Foundation
|
|
import Secrets
|
|
|
|
public struct ImageSerializationService {
|
|
private let key: SymmetricKey
|
|
|
|
public init(environment: AppEnvironment) throws {
|
|
key = try SymmetricKey(data: Secrets.imageCacheKey(keychain: environment.keychain))
|
|
}
|
|
}
|
|
|
|
public extension ImageSerializationService {
|
|
func serialize(data: Data) throws -> Data {
|
|
try ChaChaPoly.seal(data, using: key).combined
|
|
}
|
|
|
|
func deserialize(data: Data) throws -> Data {
|
|
try ChaChaPoly.open(.init(combined: data), using: key)
|
|
}
|
|
|
|
func cacheKey(forKey key: String) -> String {
|
|
Data(SHA256.hash(data: Data(key.utf8))).base16EncodedString()
|
|
}
|
|
}
|