Access.swift 7.5 KB

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