Access.swift 7.7 KB

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