Access.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 CryptoSwift
  10. class Access: XCTestCase {
  11. let cipher = try! AES(key: "secret0key000000", iv: "0123456789012345")
  12. let authenticator = HMAC(key: Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"), variant: .sha1)
  13. func testChecksum() {
  14. let _ = Checksum.crc32([1,2,3])
  15. let _ = Checksum.crc16([1,2,3])
  16. }
  17. func testHash() {
  18. let _ = Hash.md5([1,2,3])
  19. let _ = Hash.sha1([1,2,3])
  20. let _ = Hash.sha224([1,2,3])
  21. let _ = Hash.sha256([1,2,3])
  22. let _ = Hash.sha384([1,2,3])
  23. let _ = Hash.sha512([1,2,3])
  24. }
  25. func testArrayExtension() {
  26. let array = Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3")
  27. let _ = array.toHexString()
  28. let _ = array.md5()
  29. let _ = array.sha1()
  30. let _ = array.sha256()
  31. let _ = array.sha384()
  32. let _ = array.sha512()
  33. let _ = array.crc32()
  34. let _ = array.crc16()
  35. do {
  36. let _ = try array.encrypt(cipher: cipher)
  37. let _ = try array.decrypt(cipher: cipher)
  38. let _ = try array.authenticate(with: authenticator)
  39. } catch {
  40. XCTFail(error.localizedDescription)
  41. }
  42. }
  43. func testCollectionExtension() {
  44. // nothing public
  45. }
  46. func testStringExtension() {
  47. let string = "foofoobarbar"
  48. let _ = string.md5()
  49. let _ = string.sha1()
  50. let _ = string.sha224()
  51. let _ = string.sha256()
  52. let _ = string.sha384()
  53. let _ = string.sha512()
  54. let _ = string.crc16()
  55. let _ = string.crc32()
  56. do {
  57. let _ = try string.encrypt(cipher: cipher)
  58. let _ = try string.authenticate(with: authenticator)
  59. } catch {
  60. XCTFail(error.localizedDescription)
  61. }
  62. }
  63. func testStringFoundationExtension() {
  64. let string = "aPf/i9th9iX+vf49eR7PYk2q7S5xmm3jkRLejgzHNJs="
  65. do {
  66. let _ = try string.decryptBase64ToString(cipher: cipher)
  67. let _ = try string.decryptBase64(cipher: cipher)
  68. } catch {
  69. XCTFail(error.localizedDescription)
  70. }
  71. }
  72. func testIntExtension() {
  73. // nothing public
  74. }
  75. func testUInt16Extension() {
  76. // nothing public
  77. }
  78. func testUInt32Extension() {
  79. // nothing public
  80. }
  81. func testUInt64Extension() {
  82. // nothing public
  83. }
  84. func testUInt8Extension() {
  85. // nothing public
  86. }
  87. func testDataExtension() {
  88. let data = Data(bytes: [1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8])
  89. let _ = data.checksum()
  90. let _ = data.md5()
  91. let _ = data.sha1()
  92. let _ = data.sha224()
  93. let _ = data.sha256()
  94. let _ = data.sha384()
  95. let _ = data.sha512()
  96. let _ = data.crc16()
  97. let _ = data.crc32()
  98. let _ = data.bytes
  99. let _ = data.toHexString()
  100. do {
  101. let _ = try data.encrypt(cipher: cipher)
  102. let _ = try data.decrypt(cipher: cipher)
  103. let _ = try data.authenticate(with: authenticator)
  104. } catch {
  105. XCTFail(error.localizedDescription)
  106. }
  107. }
  108. func testPadding() {
  109. // PKCS7
  110. let _ = PKCS7().add(to: [1,2,3], blockSize: 16)
  111. let _ = PKCS7().remove(from: [1,2,3], blockSize: 16)
  112. // NoPadding
  113. let _ = NoPadding().add(to: [1,2,3], blockSize: 16)
  114. let _ = NoPadding().remove(from: [1,2,3], blockSize: 16)
  115. // ZeroPadding
  116. let _ = ZeroPadding().add(to: [1,2,3], blockSize: 16)
  117. let _ = ZeroPadding().remove(from: [1,2,3], blockSize: 16)
  118. }
  119. func testPBKDF() {
  120. do {
  121. let _ = PKCS5.PBKDF1.Variant.md5
  122. let _ = try PKCS5.PBKDF1(password: [1,2,3,4,5,6,7], salt: [1,2,3,4,5,6,7,8]).calculate()
  123. let _ = try PKCS5.PBKDF2(password: [1,2,3,4,5,6,7], salt: [1,2,3,4]).calculate()
  124. } catch {
  125. XCTFail(error.localizedDescription)
  126. }
  127. }
  128. func testAuthenticators() {
  129. do {
  130. let _ = try HMAC(key: Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"), variant: .sha1).authenticate([1,2,3])
  131. 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])
  132. } catch {
  133. XCTFail(error.localizedDescription)
  134. }
  135. }
  136. func testAES() {
  137. do {
  138. let aes = try AES(key: "secret0key000000", iv: "0123456789012345")
  139. let _ = aes.makeEncryptor()
  140. let _ = aes.makeDecryptor()
  141. let enc = try aes.encrypt([1,2,3])
  142. let _ = try aes.decrypt(enc)
  143. 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())
  144. let _ = AES.Variant.aes128
  145. let _ = AES.blockSize
  146. } catch {
  147. XCTFail(error.localizedDescription)
  148. }
  149. }
  150. func testRabbit() {
  151. do {
  152. let rabbit = try Rabbit(key: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
  153. let enc = rabbit.encrypt([1,2,3])
  154. let _ = rabbit.decrypt(enc)
  155. XCTAssertThrowsError(try Rabbit(key: "123"))
  156. let _ = Rabbit.blockSize
  157. let _ = Rabbit.keySize
  158. let _ = Rabbit.ivSize
  159. } catch {
  160. XCTFail(error.localizedDescription)
  161. }
  162. }
  163. func testChaCha20() {
  164. let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
  165. let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
  166. do {
  167. let _ = ChaCha20.blockSize
  168. let chacha20 = try ChaCha20(key: key, iv: iv)
  169. let enc = try chacha20.encrypt([1,3,4])
  170. let _ = try chacha20.decrypt(enc)
  171. XCTAssertThrowsError(try ChaCha20(key: "123", iv: "12345678"))
  172. let _ = chacha20.makeEncryptor()
  173. let _ = chacha20.makeDecryptor()
  174. } catch {
  175. XCTFail(error.localizedDescription)
  176. }
  177. }
  178. func testUpdatable() {
  179. // TODO
  180. }
  181. }