Access.swift 7.2 KB

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