HashTests.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. @testable 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 = [0x31, 0x32, 0x33] as [UInt8] // "1", "2", "3"
  19. if let hash = Hash.md5(data1).calculate() {
  20. XCTAssertEqual(hash, [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70], "MD5 calculation failed");
  21. } else {
  22. XCTAssert(false, "Missing result")
  23. }
  24. let string:NSString = ""
  25. let data:NSData = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
  26. if let hash = Hash.md5(data.arrayOfBytes()).calculate() {
  27. XCTAssertEqual(hash, [0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e], "MD5 calculation failed")
  28. } else {
  29. XCTAssert(false, "Missing result")
  30. }
  31. if let hash = "123".md5() {
  32. XCTAssertEqual(hash, "202cb962ac59075b964b07152d234b70", "MD5 calculation failed");
  33. }
  34. if let hash = "".md5() {
  35. XCTAssertEqual(hash, "d41d8cd98f00b204e9800998ecf8427e", "MD5 calculation failed")
  36. } else {
  37. XCTAssert(false, "Hash for empty string is missing")
  38. }
  39. if let hash = "a".md5() {
  40. XCTAssertEqual(hash, "0cc175b9c0f1b6a831c399e269772661", "MD5 calculation failed")
  41. }
  42. if let hash = "abc".md5() {
  43. XCTAssertEqual(hash, "900150983cd24fb0d6963f7d28e17f72", "MD5 calculation failed")
  44. }
  45. if let hash = "message digest".md5() {
  46. XCTAssertEqual(hash, "f96b697d7cb7938d525a2f31aaf161d0", "MD5 calculation failed")
  47. }
  48. if let hash = "abcdefghijklmnopqrstuvwxyz".md5() {
  49. XCTAssertEqual(hash, "c3fcd3d76192e4007dfb496cca67e13b", "MD5 calculation failed")
  50. }
  51. if let hash = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".md5() {
  52. XCTAssertEqual(hash, "d174ab98d277d9f5a5611c2c9f419d9f", "MD5 calculation failed")
  53. }
  54. if let hash = "12345678901234567890123456789012345678901234567890123456789012345678901234567890".md5() {
  55. XCTAssertEqual(hash, "57edf4a22be3c955ac49da2e2107b67a", "MD5 calculation failed")
  56. }
  57. }
  58. func testMD5PerformanceSwift() {
  59. self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, forBlock: { () -> Void in
  60. let buf = UnsafeMutablePointer<UInt8>(calloc(1024 * 1024, sizeof(UInt8)))
  61. let data = NSData(bytes: buf, length: 1024 * 1024)
  62. let arr = data.arrayOfBytes()
  63. self.startMeasuring()
  64. Hash.md5(arr).calculate()
  65. self.stopMeasuring()
  66. buf.dealloc(1024 * 1024)
  67. buf.destroy()
  68. })
  69. }
  70. func testMD5PerformanceCommonCrypto() {
  71. self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, forBlock: { () -> Void in
  72. let buf = UnsafeMutablePointer<UInt8>(calloc(1024 * 1024, sizeof(UInt8)))
  73. let data = NSData(bytes: buf, length: 1024 * 1024)
  74. let outbuf = UnsafeMutablePointer<UInt8>.alloc(Int(CC_MD5_DIGEST_LENGTH))
  75. self.startMeasuring()
  76. CC_MD5(data.bytes, CC_LONG(data.length), outbuf)
  77. //let output = NSData(bytes: outbuf, length: Int(CC_MD5_DIGEST_LENGTH));
  78. self.stopMeasuring()
  79. outbuf.dealloc(Int(CC_MD5_DIGEST_LENGTH))
  80. outbuf.destroy()
  81. buf.dealloc(1024 * 1024)
  82. buf.destroy()
  83. })
  84. }
  85. func testSHA1() {
  86. let data:NSData = NSData(bytes: [0x31, 0x32, 0x33] as [UInt8], length: 3)
  87. if let hash = data.sha1() {
  88. XCTAssertEqual(hash.toHexString(), "40bd001563085fc35165329ea1ff5c5ecbdbbeef", "SHA1 calculation failed");
  89. }
  90. if let hash = "abc".sha1() {
  91. XCTAssertEqual(hash, "a9993e364706816aba3e25717850c26c9cd0d89d", "SHA1 calculation failed")
  92. }
  93. if let hash = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha1() {
  94. XCTAssertEqual(hash, "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "SHA1 calculation failed")
  95. }
  96. if let hash = "".sha1() {
  97. XCTAssertEqual(hash, "da39a3ee5e6b4b0d3255bfef95601890afd80709", "SHA1 calculation failed")
  98. } else {
  99. XCTAssert(false, "SHA1 calculation failed")
  100. }
  101. }
  102. func testSHA224() {
  103. let data:NSData = NSData(bytes: [0x31, 0x32, 0x33] as [UInt8], length: 3)
  104. if let hash = data.sha224() {
  105. XCTAssertEqual(hash.toHexString(), "78d8045d684abd2eece923758f3cd781489df3a48e1278982466017f", "SHA224 calculation failed");
  106. }
  107. }
  108. func testSHA256() {
  109. let data:NSData = NSData(bytes: [0x31, 0x32, 0x33] as [UInt8], length: 3)
  110. if let hash = data.sha256() {
  111. XCTAssertEqual(hash.toHexString(), "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3", "SHA256 calculation failed");
  112. }
  113. if let hash = "Rosetta code".sha256() {
  114. XCTAssertEqual(hash, "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf", "SHA256 calculation failed")
  115. }
  116. if let hash = "".sha256() {
  117. XCTAssertEqual(hash, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "SHA256 calculation failed")
  118. } else {
  119. XCTAssert(false, "SHA256 calculation failed")
  120. }
  121. }
  122. func testSHA384() {
  123. let data:NSData = NSData(bytes: [49, 50, 51] as [UInt8], length: 3)
  124. if let hash = data.sha384() {
  125. XCTAssertEqual(hash.toHexString(), "9a0a82f0c0cf31470d7affede3406cc9aa8410671520b727044eda15b4c25532a9b5cd8aaf9cec4919d76255b6bfb00f", "SHA384 calculation failed");
  126. }
  127. if let hash = "The quick brown fox jumps over the lazy dog.".sha384() {
  128. XCTAssertEqual(hash, "ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7", "SHA384 calculation failed");
  129. }
  130. if let hash = "".sha384() {
  131. XCTAssertEqual(hash, "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", "SHA384 calculation failed")
  132. } else {
  133. XCTAssert(false, "SHA384 calculation failed")
  134. }
  135. }
  136. func testSHA512() {
  137. let data:NSData = NSData(bytes: [49, 50, 51] as [UInt8], length: 3)
  138. if let hash = data.sha512() {
  139. XCTAssertEqual(hash.toHexString(), "3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2", "SHA512 calculation failed");
  140. }
  141. if let hash = "The quick brown fox jumps over the lazy dog.".sha512() {
  142. XCTAssertEqual(hash, "91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed", "SHA512 calculation failed");
  143. }
  144. if let hash = "".sha512() {
  145. XCTAssertEqual(hash, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", "SHA512 calculation failed")
  146. } else {
  147. XCTAssert(false, "SHA512 calculation failed")
  148. }
  149. }
  150. func testCRC32() {
  151. let data:NSData = NSData(bytes: [49, 50, 51] as [UInt8], length: 3)
  152. if let crc = data.crc32() {
  153. XCTAssertEqual(crc.toHexString(), "884863d2", "CRC32 calculation failed");
  154. }
  155. if let crc = "".crc32() {
  156. XCTAssertEqual(crc, "00000000", "CRC32 calculation failed");
  157. } else {
  158. XCTAssert(false, "CRC32 calculation failed")
  159. }
  160. }
  161. func testCRC16() {
  162. let result = CRC().crc16([49,50,51,52,53,54,55,56,57] as [UInt8])
  163. XCTAssert(result == 0xBB3D, "CRC16 failed")
  164. }
  165. func testChecksum() {
  166. let data:NSData = NSData(bytes: [49, 50, 51] as [UInt8], length: 3)
  167. XCTAssert(data.checksum() == 0x96, "Invalid checksum")
  168. }
  169. }