Access.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // Access.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 06/08/16.
  6. // Copyright © 2016 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. import XCTest
  9. import Foundation
  10. // import without @testable to test public API
  11. import CryptoSwift
  12. class Access: XCTestCase {
  13. let cipher = try! AES(key: "secret0key000000", iv: "0123456789012345")
  14. let authenticator = HMAC(key: Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"), variant: .sha1)
  15. func testChecksum() {
  16. let _ = Checksum.crc32([1,2,3])
  17. let _ = Checksum.crc16([1,2,3])
  18. }
  19. func testRandomIV() {
  20. let _ = AES.randomIV(AES.blockSize)
  21. let _ = ChaCha20.randomIV(ChaCha20.blockSize)
  22. }
  23. func testDigest() {
  24. let _ = Digest.md5([1,2,3])
  25. let _ = Digest.sha1([1,2,3])
  26. let _ = Digest.sha224([1,2,3])
  27. let _ = Digest.sha256([1,2,3])
  28. let _ = Digest.sha384([1,2,3])
  29. let _ = Digest.sha512([1,2,3])
  30. let _ = Digest.sha3([1,2,3], variant: .sha224)
  31. let _ = SHA1().calculate(for: [0])
  32. let _ = SHA2(variant: .sha224).calculate(for: [0])
  33. let _ = SHA3(variant: .sha256).calculate(for: [0])
  34. let _ = MD5().calculate(for: [0])
  35. }
  36. func testArrayExtension() {
  37. let array = Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3")
  38. let _ = array.toHexString()
  39. let _ = array.md5()
  40. let _ = array.sha1()
  41. let _ = array.sha256()
  42. let _ = array.sha384()
  43. let _ = array.sha512()
  44. let _ = array.sha3(.sha224)
  45. let _ = array.crc32()
  46. let _ = array.crc16()
  47. do {
  48. let _ = try array.encrypt(cipher: cipher)
  49. let _ = try array.decrypt(cipher: cipher)
  50. let _ = try array.authenticate(with: authenticator)
  51. } catch {
  52. XCTFail(error.localizedDescription)
  53. }
  54. }
  55. func testCollectionExtension() {
  56. // nothing public
  57. }
  58. func testStringExtension() {
  59. let string = "foofoobarbar"
  60. let _ = string.md5()
  61. let _ = string.sha1()
  62. let _ = string.sha224()
  63. let _ = string.sha256()
  64. let _ = string.sha384()
  65. let _ = string.sha512()
  66. let _ = string.sha3(.sha224)
  67. let _ = string.crc16()
  68. let _ = string.crc32()
  69. do {
  70. let _ = try string.encrypt(cipher: cipher)
  71. let _ = try string.authenticate(with: authenticator)
  72. } catch {
  73. XCTFail(error.localizedDescription)
  74. }
  75. }
  76. func testStringFoundationExtension() {
  77. let string = "aPf/i9th9iX+vf49eR7PYk2q7S5xmm3jkRLejgzHNJs="
  78. do {
  79. let _ = try string.decryptBase64ToString(cipher: cipher)
  80. let _ = try string.decryptBase64(cipher: cipher)
  81. } catch {
  82. XCTFail(error.localizedDescription)
  83. }
  84. }
  85. func testIntExtension() {
  86. // nothing public
  87. }
  88. func testUInt16Extension() {
  89. // nothing public
  90. }
  91. func testUInt32Extension() {
  92. // nothing public
  93. }
  94. func testUInt64Extension() {
  95. // nothing public
  96. }
  97. func testUInt8Extension() {
  98. // nothing public
  99. }
  100. func testDataExtension() {
  101. let data = Data(bytes: [1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8])
  102. let _ = data.checksum()
  103. let _ = data.md5()
  104. let _ = data.sha1()
  105. let _ = data.sha224()
  106. let _ = data.sha256()
  107. let _ = data.sha384()
  108. let _ = data.sha512()
  109. let _ = data.sha3(.sha224)
  110. let _ = data.crc16()
  111. let _ = data.crc32()
  112. let _ = data.bytes
  113. let _ = data.toHexString()
  114. do {
  115. let _ = try data.encrypt(cipher: cipher)
  116. let _ = try data.decrypt(cipher: cipher)
  117. let _ = try data.authenticate(with: authenticator)
  118. } catch {
  119. XCTFail(error.localizedDescription)
  120. }
  121. }
  122. func testPadding() {
  123. // PKCS7
  124. let _ = PKCS7().add(to: [1,2,3], blockSize: 16)
  125. let _ = PKCS7().remove(from: [1,2,3], blockSize: 16)
  126. // NoPadding
  127. let _ = NoPadding().add(to: [1,2,3], blockSize: 16)
  128. let _ = NoPadding().remove(from: [1,2,3], blockSize: 16)
  129. // ZeroPadding
  130. let _ = ZeroPadding().add(to: [1,2,3], blockSize: 16)
  131. let _ = ZeroPadding().remove(from: [1,2,3], blockSize: 16)
  132. }
  133. func testPBKDF() {
  134. do {
  135. let _ = PKCS5.PBKDF1.Variant.md5
  136. let _ = try PKCS5.PBKDF1(password: [1,2,3,4,5,6,7], salt: [1,2,3,4,5,6,7,8]).calculate()
  137. let _ = try PKCS5.PBKDF2(password: [1,2,3,4,5,6,7], salt: [1,2,3,4]).calculate()
  138. } catch {
  139. XCTFail(error.localizedDescription)
  140. }
  141. }
  142. func testAuthenticators() {
  143. do {
  144. let _ = try HMAC(key: Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"), variant: .sha1).authenticate([1,2,3])
  145. let _ = try Poly1305(key: [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2]).authenticate([1,2,3])
  146. } catch {
  147. XCTFail(error.localizedDescription)
  148. }
  149. }
  150. func testAES() {
  151. do {
  152. let aes = try AES(key: "secret0key000000", iv: "0123456789012345")
  153. var encryptor = aes.makeEncryptor()
  154. let _ = try encryptor.update(withBytes: [1,2,3])
  155. let _ = try encryptor.finish()
  156. var decryptor = aes.makeDecryptor()
  157. let _ = try decryptor.update(withBytes: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
  158. let _ = try decryptor.finish()
  159. let enc = try aes.encrypt([1,2,3])
  160. let _ = try aes.decrypt(enc)
  161. let _ = try AES(key: [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6], iv: [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6], blockMode: .CBC, padding: NoPadding())
  162. let _ = AES.Variant.aes128
  163. let _ = AES.blockSize
  164. } catch {
  165. XCTFail(error.localizedDescription)
  166. }
  167. }
  168. func testRabbit() {
  169. do {
  170. let rabbit = try Rabbit(key: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
  171. let enc = rabbit.encrypt([1,2,3])
  172. let _ = rabbit.decrypt(enc)
  173. XCTAssertThrowsError(try Rabbit(key: "123"))
  174. let _ = Rabbit.blockSize
  175. let _ = Rabbit.keySize
  176. let _ = Rabbit.ivSize
  177. } catch {
  178. XCTFail(error.localizedDescription)
  179. }
  180. }
  181. func testChaCha20() {
  182. let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
  183. let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
  184. do {
  185. let _ = ChaCha20.blockSize
  186. let chacha20 = try ChaCha20(key: key, iv: iv)
  187. let enc = try chacha20.encrypt([1,3,4])
  188. let _ = try chacha20.decrypt(enc)
  189. XCTAssertThrowsError(try ChaCha20(key: "123", iv: "12345678"))
  190. let _ = chacha20.makeEncryptor()
  191. let _ = chacha20.makeDecryptor()
  192. } catch {
  193. XCTFail(error.localizedDescription)
  194. }
  195. }
  196. func testUpdatable() {
  197. // TODO
  198. }
  199. static let allTests = [
  200. ("testChecksum", testChecksum),
  201. ("testDigest", testDigest),
  202. ("testArrayExtension", testArrayExtension),
  203. ("testCollectionExtension", testCollectionExtension),
  204. ("testStringExtension", testStringExtension),
  205. ("testStringFoundationExtension", testStringFoundationExtension),
  206. ("testIntExtension", testIntExtension),
  207. ("testUInt16Extension", testUInt16Extension),
  208. ("testUInt32Extension", testUInt32Extension),
  209. ("testUInt64Extension", testUInt64Extension),
  210. ("testUInt8Extension", testUInt8Extension),
  211. ("testDataExtension", testDataExtension),
  212. ("testPadding", testPadding),
  213. ("testPBKDF", testPBKDF),
  214. ("testAuthenticators", testAuthenticators),
  215. ("testAES", testAES),
  216. ("testRabbit", testRabbit),
  217. ("testChaCha20", testChaCha20),
  218. ("testUpdatable", testUpdatable),
  219. ("testRandomIV", testRandomIV)
  220. ]
  221. }