Access.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // TODO
  138. }
  139. func testRabbit() {
  140. // TODO
  141. }
  142. func testChaCha20() {
  143. // TODO
  144. }
  145. func testUpdatable() {
  146. // TODO
  147. }
  148. }