|
@@ -7,8 +7,11 @@ import CryptoSwift
|
|
|
import Foundation
|
|
|
import XCPlayground
|
|
|
|
|
|
-//: # AES
|
|
|
-//: One-time shot
|
|
|
+/*:
|
|
|
+ # AES
|
|
|
+ ### One-time shot.
|
|
|
+ Encrypt all data at once.
|
|
|
+ */
|
|
|
do {
|
|
|
let aes = try AES(key: "passwordpassword", iv: "drowssapdrowssap")
|
|
|
let ciphertext = try aes.encrypt("Nullam quis risus eget urna mollis ornare vel eu leo.".utf8.map({$0}))
|
|
@@ -17,15 +20,20 @@ do {
|
|
|
print(error)
|
|
|
}
|
|
|
|
|
|
-//: Incremental encryption
|
|
|
+/*:
|
|
|
+ ### Incremental encryption
|
|
|
+
|
|
|
+ Instantiate Encryptor for AES encryption (or decryptor for decryption) and process input data partially.
|
|
|
+ */
|
|
|
do {
|
|
|
- let aes = try AES(key: "passwordpassword", iv: "drowssapdrowssap")
|
|
|
- var encryptor = aes.makeEncryptor()
|
|
|
+ var encryptor = try AES(key: "passwordpassword", iv: "drowssapdrowssap").makeEncryptor()
|
|
|
|
|
|
var ciphertext = Array<UInt8>()
|
|
|
+ // aggregate partial results
|
|
|
ciphertext += try encryptor.update(withBytes: "Nullam quis risus ".utf8.map({$0}))
|
|
|
ciphertext += try encryptor.update(withBytes: "eget urna mollis ".utf8.map({$0}))
|
|
|
ciphertext += try encryptor.update(withBytes: "ornare vel eu leo.".utf8.map({$0}))
|
|
|
+ // finish at the end
|
|
|
ciphertext += try encryptor.finish()
|
|
|
|
|
|
print(ciphertext.toHexString())
|
|
@@ -33,7 +41,9 @@ do {
|
|
|
print(error)
|
|
|
}
|
|
|
|
|
|
-//: Encrypt stream incrementally
|
|
|
+/*:
|
|
|
+ ### Encrypt stream
|
|
|
+ */
|
|
|
do {
|
|
|
// write until all is written
|
|
|
func writeToStream(stream: NSOutputStream, bytes: Array<UInt8>) {
|