2
0

HashTests.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // CryptoSwiftTests.swift
  3. // CryptoSwiftTests
  4. //
  5. // Created by Marcin Krzyzanowski on 06/07/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. import XCTest
  9. import CryptoSwift
  10. final class CryptoSwiftTests: XCTestCase {
  11. override func setUp() {
  12. super.setUp()
  13. }
  14. override func tearDown() {
  15. super.tearDown()
  16. }
  17. func testMD5() {
  18. let data1:NSData = NSData(bytes: [0x31, 0x32, 0x33] as [UInt8], length: 3) // "1", "2", "3"
  19. if let hash = Hash.md5(data1).calculate() {
  20. XCTAssertEqual(hash.toHexString(), "202cb962ac59075b964b07152d234b70", "MD5 calculation failed");
  21. } else {
  22. XCTAssert(false, "Missing result")
  23. }
  24. var string:NSString = ""
  25. var data:NSData = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
  26. if let hashData = Hash.md5(data).calculate() {
  27. XCTAssertEqual(hashData.toHexString(), "d41d8cd98f00b204e9800998ecf8427e", "MD5 calculation failed")
  28. } else {
  29. XCTAssert(false, "Missing result")
  30. }
  31. if let hash = data1.md5() {
  32. XCTAssertEqual(hash.toHexString(), "202cb962ac59075b964b07152d234b70", "MD5 calculation failed");
  33. }
  34. if let hash = "123".md5() {
  35. XCTAssertEqual(hash, "202cb962ac59075b964b07152d234b70", "MD5 calculation failed");
  36. }
  37. if let hash = "".md5() {
  38. XCTAssertEqual(hash, "d41d8cd98f00b204e9800998ecf8427e", "MD5 calculation failed")
  39. } else {
  40. XCTAssert(false, "Hash for empty string is missing")
  41. }
  42. if let hash = "a".md5() {
  43. XCTAssertEqual(hash, "0cc175b9c0f1b6a831c399e269772661", "MD5 calculation failed")
  44. }
  45. if let hash = "abc".md5() {
  46. XCTAssertEqual(hash, "900150983cd24fb0d6963f7d28e17f72", "MD5 calculation failed")
  47. }
  48. if let hash = "message digest".md5() {
  49. XCTAssertEqual(hash, "f96b697d7cb7938d525a2f31aaf161d0", "MD5 calculation failed")
  50. }
  51. if let hash = "abcdefghijklmnopqrstuvwxyz".md5() {
  52. XCTAssertEqual(hash, "c3fcd3d76192e4007dfb496cca67e13b", "MD5 calculation failed")
  53. }
  54. if let hash = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".md5() {
  55. XCTAssertEqual(hash, "d174ab98d277d9f5a5611c2c9f419d9f", "MD5 calculation failed")
  56. }
  57. if let hash = "12345678901234567890123456789012345678901234567890123456789012345678901234567890".md5() {
  58. XCTAssertEqual(hash, "57edf4a22be3c955ac49da2e2107b67a", "MD5 calculation failed")
  59. }
  60. }
  61. func testMD5PerformanceSwift() {
  62. self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, forBlock: { () -> Void in
  63. let buf = UnsafeMutablePointer<UInt8>(calloc(2048, sizeof(UInt8)))
  64. let data = NSData(bytes: buf, length: 2048)
  65. self.startMeasuring()
  66. Hash.md5(data).calculate()
  67. self.stopMeasuring()
  68. buf.dealloc(1024)
  69. buf.destroy()
  70. })
  71. }
  72. func testMD5PerformanceCommonCrypto() {
  73. self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, forBlock: { () -> Void in
  74. let buf = UnsafeMutablePointer<UInt8>(calloc(2048, sizeof(UInt8)))
  75. let data = NSData(bytes: buf, length: 2048)
  76. self.startMeasuring()
  77. var outbuf = UnsafeMutablePointer<UInt8>.alloc(Int(CC_MD5_DIGEST_LENGTH))
  78. CC_MD5(data.bytes, CC_LONG(data.length), outbuf)
  79. let output = NSData(bytes: outbuf, length: Int(CC_MD5_DIGEST_LENGTH));
  80. outbuf.dealloc(Int(CC_MD5_DIGEST_LENGTH))
  81. outbuf.destroy()
  82. self.stopMeasuring()
  83. buf.dealloc(1024)
  84. buf.destroy()
  85. })
  86. }
  87. func testSHA1() {
  88. var data:NSData = NSData(bytes: [0x31, 0x32, 0x33] as [UInt8], length: 3)
  89. if let hash = data.sha1() {
  90. XCTAssertEqual(hash.toHexString(), "40bd001563085fc35165329ea1ff5c5ecbdbbeef", "SHA1 calculation failed");
  91. }
  92. if let hash = "abc".sha1() {
  93. XCTAssertEqual(hash, "a9993e364706816aba3e25717850c26c9cd0d89d", "SHA1 calculation failed")
  94. }
  95. if let hash = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha1() {
  96. XCTAssertEqual(hash, "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "SHA1 calculation failed")
  97. }
  98. if let hash = "".sha1() {
  99. XCTAssertEqual(hash, "da39a3ee5e6b4b0d3255bfef95601890afd80709", "SHA1 calculation failed")
  100. } else {
  101. XCTAssert(false, "SHA1 calculation failed")
  102. }
  103. }
  104. func testSHA224() {
  105. var data:NSData = NSData(bytes: [0x31, 0x32, 0x33] as [UInt8], length: 3)
  106. if let hash = data.sha224() {
  107. XCTAssertEqual(hash.toHexString(), "78d8045d684abd2eece923758f3cd781489df3a48e1278982466017f", "SHA224 calculation failed");
  108. }
  109. }
  110. func testSHA256() {
  111. var data:NSData = NSData(bytes: [0x31, 0x32, 0x33] as [UInt8], length: 3)
  112. if let hash = data.sha256() {
  113. XCTAssertEqual(hash.toHexString(), "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3", "SHA256 calculation failed");
  114. }
  115. if let hash = "Rosetta code".sha256() {
  116. XCTAssertEqual(hash, "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf", "SHA256 calculation failed")
  117. }
  118. if let hash = "".sha256() {
  119. XCTAssertEqual(hash, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "SHA256 calculation failed")
  120. } else {
  121. XCTAssert(false, "SHA256 calculation failed")
  122. }
  123. }
  124. func testSHA384() {
  125. var data:NSData = NSData(bytes: [49, 50, 51] as [UInt8], length: 3)
  126. if let hash = data.sha384() {
  127. XCTAssertEqual(hash.toHexString(), "9a0a82f0c0cf31470d7affede3406cc9aa8410671520b727044eda15b4c25532a9b5cd8aaf9cec4919d76255b6bfb00f", "SHA384 calculation failed");
  128. }
  129. if let hash = "The quick brown fox jumps over the lazy dog.".sha384() {
  130. XCTAssertEqual(hash, "ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7", "SHA384 calculation failed");
  131. }
  132. if let hash = "".sha384() {
  133. XCTAssertEqual(hash, "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", "SHA384 calculation failed")
  134. } else {
  135. XCTAssert(false, "SHA384 calculation failed")
  136. }
  137. }
  138. func testSHA512() {
  139. var data:NSData = NSData(bytes: [49, 50, 51] as [UInt8], length: 3)
  140. if let hash = data.sha512() {
  141. XCTAssertEqual(hash.toHexString(), "3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2", "SHA512 calculation failed");
  142. }
  143. if let hash = "The quick brown fox jumps over the lazy dog.".sha512() {
  144. XCTAssertEqual(hash, "91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed", "SHA512 calculation failed");
  145. }
  146. if let hash = "".sha512() {
  147. XCTAssertEqual(hash, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", "SHA512 calculation failed")
  148. } else {
  149. XCTAssert(false, "SHA512 calculation failed")
  150. }
  151. }
  152. func testCRC32() {
  153. var data:NSData = NSData(bytes: [49, 50, 51] as [UInt8], length: 3)
  154. if let crc = data.crc32() {
  155. XCTAssertEqual(crc.toHexString(), "884863d2", "CRC32 calculation failed");
  156. }
  157. if let crc = "".crc32() {
  158. XCTAssertEqual(crc, "00000000", "CRC32 calculation failed");
  159. } else {
  160. XCTAssert(false, "CRC32 calculation failed")
  161. }
  162. }
  163. func testCRC32Async() {
  164. let expect = expectationWithDescription("CRC32")
  165. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
  166. self.testCRC32()
  167. expect.fulfill()
  168. })
  169. waitForExpectationsWithTimeout(10, handler: { (error) -> Void in
  170. XCTAssertNil(error, "CRC32 async failed")
  171. })
  172. }
  173. }