diff --git a/Sources/ReplicantSwift/ReplicantConfig.swift b/Sources/ReplicantSwift/ReplicantConfig.swift index 64a722f..24785ca 100644 --- a/Sources/ReplicantSwift/ReplicantConfig.swift +++ b/Sources/ReplicantSwift/ReplicantConfig.swift @@ -30,4 +30,38 @@ public struct ReplicantConfig: Codable self.addSequences = addSequences self.removeSequences = removeSequences } + + public func createJSON() -> String? + { + let encoder = JSONEncoder() + encoder.outputFormatting = .prettyPrinted + + do + { + let configData = try encoder.encode(self) + return String(data: configData, encoding: .utf8) + } + catch (let error) + { + print("Failed to encode config into JSON format: \(error)") + return nil + } + } + + static public func parseJSON(jsonString: String) -> ReplicantConfig? + { + let decoder = JSONDecoder() + let jsonData = jsonString.data + + do + { + let config = try decoder.decode(ReplicantConfig.self, from: jsonData) + return config + } + catch (let error) + { + print("\nUnable to decode JSON into ReplicantConfig: \(error)\n") + return nil + } + } } diff --git a/Sources/ReplicantSwift/ReplicantConfigTemplate.swift b/Sources/ReplicantSwift/ReplicantConfigTemplate.swift new file mode 100644 index 0000000..291940b --- /dev/null +++ b/Sources/ReplicantSwift/ReplicantConfigTemplate.swift @@ -0,0 +1,86 @@ +// +// ReplicantConfigTemplate.swift +// ReplicantSwift +// +// Created by Adelita Schule on 12/14/18. +// + +import Foundation + +public struct ReplicantConfigTemplate: Codable +{ + public var chunkSize: Int + public var chunkTimeout: Int + public var addSequences: [SequenceModel]? + public var removeSequences: [SequenceModel]? + + + public init?(chunkSize: Int, chunkTimeout: Int, addSequences: [SequenceModel]?, removeSequences: [SequenceModel]?) + { + guard chunkSize >= keySize + aesOverheadSize + else + { + print("\nUnable to initialize ReplicantConfig: chunkSize (\(chunkSize)) cannot be smaller than keySize + aesOverheadSize (\(keySize + aesOverheadSize))\n") + return nil + } + self.chunkSize = chunkSize + self.chunkTimeout = chunkTimeout + self.addSequences = addSequences + self.removeSequences = removeSequences + } + + public func createJSON() -> String? + { + let encoder = JSONEncoder() + encoder.outputFormatting = .prettyPrinted + + do + { + let configData = try encoder.encode(self) + return String(data: configData, encoding: .utf8) + } + catch (let error) + { + print("Failed to encode config into JSON format: \(error)") + return nil + } + } + + static public func parseJSON(jsonString: String) -> ReplicantConfigTemplate? + { + let decoder = JSONDecoder() + let jsonData = jsonString.data + + do + { + let config = try decoder.decode(ReplicantConfigTemplate.self, from: jsonData) + return config + } + catch (let error) + { + print("\nUnable to decode JSON into ReplicantConfigTemplate: \(error)\n") + return nil + } + } + + public mutating func createConfig(withServerKey serverPublicKey: SecKey) -> String? + { + // Encode key as data + var error: Unmanaged? + + guard let keyData = SecKeyCopyExternalRepresentation(serverPublicKey, &error) as Data? + else + { + print("\nUnable to generate public key external representation: \(error!.takeRetainedValue() as Error)\n") + return nil + } + + guard let replicantConfig = ReplicantConfig(serverPublicKey: keyData, chunkSize: self.chunkSize, chunkTimeout: self.chunkTimeout, addSequences: self.addSequences, removeSequences: self.removeSequences) + else + { + return nil + } + + return replicantConfig.createJSON() + } +} diff --git a/Sources/ReplicantSwift/ReplicantServerConfig.swift b/Sources/ReplicantSwift/ReplicantServerConfig.swift index 2230fe8..2ca0bf6 100644 --- a/Sources/ReplicantSwift/ReplicantServerConfig.swift +++ b/Sources/ReplicantSwift/ReplicantServerConfig.swift @@ -14,7 +14,6 @@ public struct ReplicantServerConfig: Codable public var addSequences: [SequenceModel]? public var removeSequences: [SequenceModel]? - public init?(serverPublicKey: SecKey, chunkSize: Int, chunkTimeout: Int, addSequences: [SequenceModel]?, removeSequences: [SequenceModel]?) { guard chunkSize >= keySize + aesOverheadSize @@ -28,4 +27,39 @@ public struct ReplicantServerConfig: Codable self.addSequences = addSequences self.removeSequences = removeSequences } + + public func createJSON() -> String? + { + let encoder = JSONEncoder() + encoder.outputFormatting = .prettyPrinted + + do + { + let serverConfigData = try encoder.encode(self) + return String(data: serverConfigData, encoding: .utf8) + } + catch (let error) + { + print("Failed to encode Server config into JSON format: \(error)") + return nil + } + } + + static public func parseJSON(jsonString: String) -> ReplicantServerConfig? + { + let decoder = JSONDecoder() + let jsonData = jsonString.data + + do + { + let config = try decoder.decode(ReplicantServerConfig.self, from: jsonData) + return config + } + catch (let error) + { + print("\nUnable to decode JSON into ReplicantServerConfig: \(error)\n") + return nil + } + } + }