Skip to content

Commit

Permalink
Config Template
Browse files Browse the repository at this point in the history
  • Loading branch information
consuelita committed Dec 14, 2018
1 parent dccd274 commit 1645860
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 1 deletion.
34 changes: 34 additions & 0 deletions Sources/ReplicantSwift/ReplicantConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
86 changes: 86 additions & 0 deletions Sources/ReplicantSwift/ReplicantConfigTemplate.swift
Original file line number Diff line number Diff line change
@@ -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<CFError>?

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()
}
}
36 changes: 35 additions & 1 deletion Sources/ReplicantSwift/ReplicantServerConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}

}

0 comments on commit 1645860

Please sign in to comment.