Skip to content

Instantly share code, notes, and snippets.

@vialyx
Last active April 18, 2020 18:38
Show Gist options
  • Save vialyx/5bf917552a497feb8b5a5086c2fbdf9e to your computer and use it in GitHub Desktop.
Save vialyx/5bf917552a497feb8b5a5086c2fbdf9e to your computer and use it in GitHub Desktop.
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