HashTests.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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(2048, sizeof(UInt8)))
  61. let data = NSData(bytes: buf, length: 2048)
  62. self.startMeasuring()
  63. Hash.md5(data.arrayOfBytes()).calculate()
  64. self.stopMeasuring()
  65. buf.dealloc(1024)
  66. buf.destroy()
  67. })
  68. }
  69. func testMD5PerformanceCommonCrypto() {
  70. self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, forBlock: { () -> Void in
  71. let buf = UnsafeMutablePointer<UInt8>(calloc(2048, sizeof(UInt8)))
  72. let data = NSData(bytes: buf, length: 2048)
  73. self.startMeasuring()
  74. let outbuf = UnsafeMutablePointer<UInt8>.alloc(Int(CC_MD5_DIGEST_LENGTH))
  75. CC_MD5(data.bytes, CC_LONG(data.length), outbuf)
  76. //let output = NSData(bytes: outbuf, length: Int(CC_MD5_DIGEST_LENGTH));
  77. outbuf.dealloc(Int(CC_MD5_DIGEST_LENGTH))
  78. outbuf.destroy()
  79. self.stopMeasuring()
  80. buf.dealloc(1024)
  81. buf.destroy()
  82. })
  83. }
  84. func testSHA1() {
  85. let data:NSData = NSData(bytes: [0x31, 0x32, 0x33] as [UInt8], length: 3)
  86. if let hash = data.sha1() {
  87. XCTAssertEqual(hash.toHexString(), "40bd001563085fc35165329ea1ff5c5ecbdbbeef", "SHA1 calculation failed");
  88. }
  89. if let hash = "abc".sha1() {
  90. XCTAssertEqual(hash, "a9993e364706816aba3e25717850c26c9cd0d89d", "SHA1 calculation failed")
  91. }
  92. if let hash = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha1() {
  93. XCTAssertEqual(hash, "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "SHA1 calculation failed")
  94. }
  95. if let hash = "".sha1() {
  96. XCTAssertEqual(hash, "da39a3ee5e6b4b0d3255bfef95601890afd80709", "SHA1 calculation failed")
  97. } else {
  98. XCTAssert(false, "SHA1 calculation failed")
  99. }
  100. }
  101. func testSHA224() {
  102. let data:NSData = NSData(bytes: [0x31, 0x32, 0x33] as [UInt8], length: 3)
  103. if let hash = data.sha224() {
  104. XCTAssertEqual(hash.toHexString(), "78d8045d684abd2eece923758f3cd781489df3a48e1278982466017f", "SHA224 calculation failed");
  105. }
  106. }
  107. func testSHA256() {
  108. let data:NSData = NSData(bytes: [0x31, 0x32, 0x33] as [UInt8], length: 3)
  109. if let hash = data.sha256() {
  110. XCTAssertEqual(hash.toHexString(), "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3", "SHA256 calculation failed");
  111. }
  112. if let hash = "Rosetta code".sha256() {
  113. XCTAssertEqual(hash, "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf", "SHA256 calculation failed")
  114. }
  115. if let hash = "".sha256() {
  116. XCTAssertEqual(hash, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "SHA256 calculation failed")
  117. } else {
  118. XCTAssert(false, "SHA256 calculation failed")
  119. }
  120. }
  121. func testSHA384() {
  122. let data:NSData = NSData(bytes: [49, 50, 51] as [UInt8], length: 3)
  123. if let hash = data.sha384() {
  124. XCTAssertEqual(hash.toHexString(), "9a0a82f0c0cf31470d7affede3406cc9aa8410671520b727044eda15b4c25532a9b5cd8aaf9cec4919d76255b6bfb00f", "SHA384 calculation failed");
  125. }
  126. if let hash = "The quick brown fox jumps over the lazy dog.".sha384() {
  127. XCTAssertEqual(hash, "ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7", "SHA384 calculation failed");
  128. }
  129. if let hash = "".sha384() {
  130. XCTAssertEqual(hash, "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", "SHA384 calculation failed")
  131. } else {
  132. XCTAssert(false, "SHA384 calculation failed")
  133. }
  134. }
  135. func testSHA512() {
  136. let data:NSData = NSData(bytes: [49, 50, 51] as [UInt8], length: 3)
  137. if let hash = data.sha512() {
  138. XCTAssertEqual(hash.toHexString(), "3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2", "SHA512 calculation failed");
  139. }
  140. if let hash = "The quick brown fox jumps over the lazy dog.".sha512() {
  141. XCTAssertEqual(hash, "91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed", "SHA512 calculation failed");
  142. }
  143. if let hash = "".sha512() {
  144. XCTAssertEqual(hash, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", "SHA512 calculation failed")
  145. } else {
  146. XCTAssert(false, "SHA512 calculation failed")
  147. }
  148. }
  149. func testCRC32() {
  150. let data:NSData = NSData(bytes: [49, 50, 51] as [UInt8], length: 3)
  151. if let crc = data.crc32() {
  152. XCTAssertEqual(crc.toHexString(), "884863d2", "CRC32 calculation failed");
  153. }
  154. if let crc = "".crc32() {
  155. XCTAssertEqual(crc, "00000000", "CRC32 calculation failed");
  156. } else {
  157. XCTAssert(false, "CRC32 calculation failed")
  158. }
  159. }
  160. func testChecksum() {
  161. let data:NSData = NSData(bytes: [49, 50, 51] as [UInt8], length: 3)
  162. XCTAssert(data.checksum() == 0x96, "Invalid checksum")
  163. }
  164. }