Contents.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*:
  2. To whom may be concerned: I offer professional support to all my open source projects.
  3. Contact: [marcin@krzyzanowskim.com](http://krzyzanowskim.com)
  4. */
  5. import CryptoSwift
  6. import Foundation
  7. import XCPlayground
  8. /*:
  9. # AES
  10. ### One-time shot.
  11. Encrypt all data at once.
  12. */
  13. do {
  14. let aes = try AES(key: "passwordpassword", iv: "drowssapdrowssap")
  15. let ciphertext = try aes.encrypt("Nullam quis risus eget urna mollis ornare vel eu leo.".utf8.map({$0}))
  16. print(ciphertext.toHexString())
  17. } catch {
  18. print(error)
  19. }
  20. /*:
  21. ### Incremental encryption
  22. Instantiate Encryptor for AES encryption (or decryptor for decryption) and process input data partially.
  23. */
  24. do {
  25. var encryptor = try AES(key: "passwordpassword", iv: "drowssapdrowssap").makeEncryptor()
  26. var ciphertext = Array<UInt8>()
  27. // aggregate partial results
  28. ciphertext += try encryptor.update(withBytes: "Nullam quis risus ".utf8.map({$0}))
  29. ciphertext += try encryptor.update(withBytes: "eget urna mollis ".utf8.map({$0}))
  30. ciphertext += try encryptor.update(withBytes: "ornare vel eu leo.".utf8.map({$0}))
  31. // finish at the end
  32. ciphertext += try encryptor.finish()
  33. print(ciphertext.toHexString())
  34. } catch {
  35. print(error)
  36. }
  37. /*:
  38. ### Encrypt stream
  39. */
  40. do {
  41. // write until all is written
  42. func writeToStream(stream: NSOutputStream, bytes: Array<UInt8>) {
  43. var writtenCount = 0
  44. while stream.hasSpaceAvailable && writtenCount < bytes.count {
  45. let c = stream.write(bytes, maxLength: bytes.count)
  46. if c <= 0 {
  47. break;
  48. }
  49. writtenCount += stream.write(bytes, maxLength: bytes.count)
  50. }
  51. }
  52. let aes = try AES(key: "passwordpassword", iv: "drowssapdrowssap")
  53. var encryptor = aes.makeEncryptor()
  54. // prepare streams
  55. let data = Data(bytes: (0..<100).map { $0 })
  56. let inputStream = InputStream(data: data)
  57. let outputStream = NSOutputStream(toMemory: ())
  58. inputStream.open()
  59. outputStream.open()
  60. var buffer = Array<UInt8>(repeating: 0, count: 2)
  61. // encrypt input stream data and write encrypted result to output stream
  62. while (inputStream.hasBytesAvailable) {
  63. let readCount = inputStream.read(&buffer, maxLength: buffer.count)
  64. if (readCount > 0) {
  65. try encryptor.update(withBytes: Array(buffer[0..<readCount])) { (bytes) in
  66. writeToStream(stream: outputStream, bytes: bytes)
  67. }
  68. }
  69. }
  70. // finalize encryption
  71. try encryptor.finish { (bytes) in
  72. writeToStream(stream: outputStream, bytes: bytes)
  73. }
  74. // print result
  75. if let ciphertext = outputStream.property(forKey: Stream.PropertyKey.dataWrittenToMemoryStreamKey.rawValue) as? Data {
  76. print("Encrypted stream data: \(ciphertext.toHexString())")
  77. }
  78. } catch {
  79. print(error)
  80. }