Contents.swift 2.8 KB

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