Access.swift 7.2 KB

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