Last active
November 14, 2017 09:32
-
-
Save piotrbernad/f3709b3691cf24426af5b55708c85f43 to your computer and use it in GitHub Desktop.
Cipher.swift
This file contains hidden or 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
| enum Cipher { | |
| case root | |
| case generated(Data) | |
| func decrypt(data: Data) throws -> Data { | |
| /// In this case we use simple XOR decryption | |
| /// In real work implementation we should use AES here | |
| let _key = Array(key) | |
| var _data: Data = Data() | |
| for (offset, element) in data.enumerated() { | |
| _data.append(element ^ _key[offset % _key.count]) | |
| } | |
| return _data | |
| } | |
| func encrypt(data: Data) throws -> Data { | |
| /// In this case we use simple XOR encryption | |
| /// In real work implementation we should use AES here | |
| let _key = Array(key) | |
| var _data: Data = Data() | |
| for (offset, element) in data.enumerated() { | |
| _data.append(element ^ _key[offset % _key.count]) | |
| } | |
| return _data | |
| } | |
| /// Returns key that should be used for encryption/decryption | |
| private var key: Data { | |
| switch self { | |
| case .generated(let key): | |
| return key | |
| case .root: | |
| return self.rootKey | |
| } | |
| } | |
| /// Returns data describing root key | |
| private var rootKey: Data { | |
| // generate random 256bit key using `openssl rand -base64 32` | |
| return Data(base64Encoded: "Ph6jJKFgPp2cnjfZazdvMVCB9whUrpr10Ywat6BlQYQ=")! | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment