Last active
April 18, 2020 18:38
-
-
Save vialyx/5bf917552a497feb8b5a5086c2fbdf9e to your computer and use it in GitHub Desktop.
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
extension Data { | |
var hexString: String { | |
return self.reduce("", { $0 + String(format: "%02x", $1) }) | |
} | |
} | |
do { | |
let sourceData = "AES256".data(using: .utf8)! | |
let password = "password" | |
let salt = AES256Crypter.randomSalt() | |
let iv = AES256Crypter.randomIv() | |
let key = try AES256Crypter.createKey(password: password.data(using: .utf8)!, salt: salt) | |
let aes = try AES256Crypter(key: key, iv: iv) | |
let encryptedData = try aes.encrypt(sourceData) | |
let decryptedData = try aes.decrypt(encryptedData) | |
print("Encrypted hex string: \(encryptedData.hexString)") | |
print("Decrypted hex string: \(decryptedData.hexString)") | |
} catch { | |
print("Failed") | |
print(error) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment