Skip to content

Instantly share code, notes, and snippets.

@MyoThuraZaw
Forked from vialyx/DataCrypterUsage.swift
Created April 18, 2020 18:38
Show Gist options
  • Save MyoThuraZaw/f62e8b14b1df12aa0464d8b57b1e5154 to your computer and use it in GitHub Desktop.
Save MyoThuraZaw/f62e8b14b1df12aa0464d8b57b1e5154 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