Last active
April 4, 2019 15:14
-
-
Save YannisDC/96aade810b1fca982aed4a9237d156bf to your computer and use it in GitHub Desktop.
Playground that shows propertyLevel encryption for Blockstack Byte option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
struct Person: Codable { | |
let name: String | |
let age: Int | |
let friends: [Person]? | |
} | |
let person = Person(name: "Brad", age: 53, friends: nil) | |
protocol Identifiable { | |
var uuid: String { get set } | |
} | |
protocol Encryptable { | |
var encrypted: Bool { get } | |
func encrypt() | |
func decrypt() | |
} | |
public class EncryptablePropertyCodable<T: Codable>: Encryptable { | |
var value: T | |
var encryptedValue: String | |
var encrypted: Bool | |
init(value: T, encrypted: Bool) { | |
self.value = value | |
self.encrypted = encrypted | |
self.encryptedValue = "" | |
} | |
func encrypt() { | |
do { | |
let object = try JSONEncoder().encode(value) | |
// Do actual encryption and get the signature back | |
if let string = String(data: object, encoding: .utf8) { | |
encryptedValue = string | |
} else { | |
print("not a valid UTF-8 sequence") | |
} | |
} catch{ | |
print("Encoding failed") | |
} | |
} | |
func decrypt() { | |
do { | |
let data : [UInt8] = [UInt8](self.encryptedValue.utf8) | |
// Do actual decryption and get the data back | |
let object = try JSONDecoder().decode(T.self, from: Data(data)) | |
self.value = object | |
} catch{ | |
print("Decoding failed") | |
} | |
} | |
} | |
protocol EncryptableObject { | |
func encryptObject() | |
func decryptObject() | |
} | |
struct Test: EncryptableObject, Identifiable { | |
public let testInt: EncryptablePropertyCodable<Person> | |
var uuid: String | |
init(uuid: String = UUID().uuidString) { | |
self.testInt = EncryptablePropertyCodable<Person>(value: person, encrypted: true) | |
self.uuid = uuid | |
} | |
func encryptObject() { | |
print("\nEncryption happens") | |
Mirror(reflecting: self) | |
.children | |
.forEach { (property) in | |
if let encryptableValue = property.value as? Encryptable { | |
encryptableValue.encrypt() | |
} | |
} | |
} | |
func decryptObject() { | |
print("\nDecryption happens") | |
Mirror(reflecting: self) | |
.children | |
.forEach { (property) in | |
if let encryptableValue = property.value as? Encryptable { | |
encryptableValue.decrypt() | |
} | |
} | |
} | |
} | |
let test = Test() | |
print("Value= \(test.testInt.value) EncryptedValue= \(test.testInt.encryptedValue)") | |
test.encryptObject() | |
print("Value= \(test.testInt.value) EncryptedValue= \(test.testInt.encryptedValue)") | |
print("\nDecrypted value gets updated") | |
test.testInt.encryptedValue = #"{"name":"Yannis","age":53}"# | |
print("Value= \(test.testInt.value) EncryptedValue= \(test.testInt.encryptedValue)") | |
test.decryptObject() | |
print("Value= \(test.testInt.value) EncryptedValue= \(test.testInt.encryptedValue)") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment