Contents.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 CryptoSwift
  8. do {
  9. let sharedKey = Data(hex: "ff72161a98db39eb81b92079e937120b67fefe3f07d53ebf995d5422eb4a7ee4")
  10. let iv = Data(hex: "54b37c9f12934210bc44f01b2752dbce")
  11. let ctr = CTR(iv: iv.bytes)
  12. let aes = try AES(key: sharedKey.bytes, blockMode: ctr, padding: .noPadding)
  13. var fencryptor = try aes.makeEncryptor()
  14. let devicePublicKey = Data(hex: "5e10a67b08a33dcd4d8fb3edda5c4cd7f11b14a5da0ca96972f21cdba2bf0444")
  15. let deviceVerifyData = Data(hex: "b9b808c634aab0387f6cc02ee30e1e606c6d6d4dcb9a18fa1d9fc94a1cb267bd")
  16. let encryptedPK = try fencryptor.update(withBytes: devicePublicKey.bytes)
  17. print("Encrypted pk : \(encryptedPK.toHexString())")
  18. let decryptedVD = try fencryptor.update(withBytes: deviceVerifyData.bytes)
  19. print("Decrypted device verify : \(decryptedVD.toHexString())")
  20. let wifiConfigRequest = Data(hex: "5219080262150a0954656a6f6e6964686912085a6f657932363131")
  21. let encryptedRequest = try fencryptor.update(withBytes: wifiConfigRequest.bytes)
  22. print("Encrypted request : \(encryptedRequest.toHexString())")
  23. let response = Data(hex: "083a536260fb")
  24. let decryptedResponse = try fencryptor.finish(withBytes: response.bytes)
  25. print("Decrypted response : \(decryptedResponse.toHexString())")
  26. // let data = Data(hex: "0123456789abcdef")
  27. // print("Encrypted sample data \(try fencryptor.update(withBytes: data.bytes).toHexString())")
  28. // print("Decrypted sample data \(try fencryptor.update(withBytes: data.bytes).toHexString())")
  29. } catch {
  30. }
  31. ///*:
  32. // # Data types conversinn
  33. // */
  34. //let data = Data(bytes: [0x01, 0x02, 0x03])
  35. //let bytes = data.bytes
  36. //let bytesHex = Array<UInt8>(hex: "0x010203")
  37. //let hexString = bytesHex.toHexString()
  38. //
  39. ///*:
  40. // # Digest
  41. // */
  42. //data.md5()
  43. //data.sha1()
  44. //data.sha224()
  45. //data.sha256()
  46. //data.sha384()
  47. //data.sha512()
  48. //
  49. //bytes.sha1()
  50. //"123".sha1()
  51. //Digest.sha1(bytes)
  52. //
  53. ////: Digest calculated incrementally
  54. //do {
  55. // var digest = MD5()
  56. // _ = try digest.update(withBytes: [0x31, 0x32])
  57. // _ = try digest.update(withBytes: [0x33])
  58. // let result = try digest.finish()
  59. // print(result)
  60. //} catch {}
  61. //
  62. ///*:
  63. // # CRC
  64. // */
  65. //bytes.crc16()
  66. //bytes.crc32()
  67. //bytes.crc32c()
  68. //
  69. ///*:
  70. // # HMAC
  71. // */
  72. //
  73. //do {
  74. // let key: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23, 25, 26, 27, 28, 29, 30, 31, 32]
  75. // try Poly1305(key: key).authenticate(bytes)
  76. // try HMAC(key: key, variant: .sha256).authenticate(bytes)
  77. //} catch {}
  78. //
  79. ///*:
  80. // # PBKDF1, PBKDF2
  81. // */
  82. //
  83. //do {
  84. // let password: Array<UInt8> = Array("s33krit".utf8)
  85. // let salt: Array<UInt8> = Array("nacllcan".utf8)
  86. //
  87. // try PKCS5.PBKDF1(password: password, salt: salt, variant: .sha1, iterations: 4096).calculate()
  88. //
  89. // let value = try PKCS5.PBKDF2(password: password, salt: salt, iterations: 4096, variant: .sha256).calculate()
  90. // print(value)
  91. //} catch {}
  92. //
  93. ///*:
  94. // # Padding
  95. // */
  96. //Padding.pkcs7.add(to: bytes, blockSize: AES.blockSize)
  97. //
  98. ///*:
  99. // # ChaCha20
  100. // */
  101. //
  102. //do {
  103. // let key: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
  104. // let iv: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8]
  105. // let message = Array<UInt8>(repeating: 7, count: 10)
  106. //
  107. // let encrypted = try ChaCha20(key: key, iv: iv).encrypt(message)
  108. // let decrypted = try ChaCha20(key: key, iv: iv).decrypt(encrypted)
  109. // print(decrypted)
  110. //} catch {
  111. // print(error)
  112. //}
  113. //
  114. ///*:
  115. // # AES
  116. // ### One-time shot.
  117. // Encrypt all data at once.
  118. // */
  119. //do {
  120. // let aes = try AES(key: "passwordpassword", iv: "drowssapdrowssap") // aes128
  121. // let ciphertext = try aes.encrypt(Array("Nullam quis risus eget urna mollis ornare vel eu leo.".utf8))
  122. // print(ciphertext.toHexString())
  123. //} catch {
  124. // print(error)
  125. //}
  126. //
  127. ///*:
  128. // ### Incremental encryption
  129. //
  130. // Instantiate Encryptor for AES encryption (or decryptor for decryption) and process input data partially.
  131. // */
  132. //do {
  133. // var encryptor = try AES(key: "passwordpassword", iv: "drowssapdrowssap").makeEncryptor()
  134. //
  135. // var ciphertext = Array<UInt8>()
  136. // // aggregate partial results
  137. // ciphertext += try encryptor.update(withBytes: Array("Nullam quis risus ".utf8))
  138. // ciphertext += try encryptor.update(withBytes: Array("eget urna mollis ".utf8))
  139. // ciphertext += try encryptor.update(withBytes: Array("ornare vel eu leo.".utf8))
  140. // // finish at the end
  141. // ciphertext += try encryptor.finish()
  142. //
  143. // print(ciphertext.toHexString())
  144. //} catch {
  145. // print(error)
  146. //}
  147. //
  148. ///*:
  149. // ### Encrypt stream
  150. // */
  151. //do {
  152. // // write until all is written
  153. // func writeTo(stream: OutputStream, bytes: Array<UInt8>) {
  154. // var writtenCount = 0
  155. // while stream.hasSpaceAvailable && writtenCount < bytes.count {
  156. // writtenCount += stream.write(bytes, maxLength: bytes.count)
  157. // }
  158. // }
  159. //
  160. // let aes = try AES(key: "passwordpassword", iv: "drowssapdrowssap")
  161. // var encryptor = try! aes.makeEncryptor()
  162. //
  163. // // prepare streams
  164. // let data = Data(bytes: (0 ..< 100).map { $0 })
  165. // let inputStream = InputStream(data: data)
  166. // let outputStream = OutputStream(toMemory: ())
  167. // inputStream.open()
  168. // outputStream.open()
  169. //
  170. // var buffer = Array<UInt8>(repeating: 0, count: 2)
  171. //
  172. // // encrypt input stream data and write encrypted result to output stream
  173. // while inputStream.hasBytesAvailable {
  174. // let readCount = inputStream.read(&buffer, maxLength: buffer.count)
  175. // if readCount > 0 {
  176. // try encryptor.update(withBytes: buffer[0 ..< readCount]) { bytes in
  177. // writeTo(stream: outputStream, bytes: bytes)
  178. // }
  179. // }
  180. // }
  181. //
  182. // // finalize encryption
  183. // try encryptor.finish { bytes in
  184. // writeTo(stream: outputStream, bytes: bytes)
  185. // }
  186. //
  187. // // print result
  188. // if let ciphertext = outputStream.property(forKey: Stream.PropertyKey(rawValue: Stream.PropertyKey.dataWrittenToMemoryStreamKey.rawValue)) as? Data {
  189. // print("Encrypted stream data: \(ciphertext.toHexString())")
  190. // }
  191. //
  192. //} catch {
  193. // print(error)
  194. //}