DigestTests.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // DigestTests.swift
  3. // CryptoSwiftTests
  4. //
  5. // Created by Marcin Krzyzanowski on 06/07/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. // http://www.di-mgt.com.au/sha_testvectors.html (http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf)
  9. //
  10. import XCTest
  11. import Foundation
  12. @testable import CryptoSwift
  13. final class DigestTests: XCTestCase {
  14. func testMD5Data() {
  15. let data = [0x31, 0x32, 0x33] as Array<UInt8> // "1", "2", "3"
  16. XCTAssertEqual(Digest.md5(data), [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70], "MD5 calculation failed");
  17. }
  18. func testMD5String() {
  19. XCTAssertEqual("123".md5(), "202cb962ac59075b964b07152d234b70", "MD5 calculation failed");
  20. XCTAssertEqual("".md5(), "d41d8cd98f00b204e9800998ecf8427e", "MD5 calculation failed")
  21. XCTAssertEqual("a".md5(), "0cc175b9c0f1b6a831c399e269772661", "MD5 calculation failed")
  22. XCTAssertEqual("abc".md5(), "900150983cd24fb0d6963f7d28e17f72", "MD5 calculation failed")
  23. XCTAssertEqual("message digest".md5(), "f96b697d7cb7938d525a2f31aaf161d0", "MD5 calculation failed")
  24. XCTAssertEqual("abcdefghijklmnopqrstuvwxyz".md5(), "c3fcd3d76192e4007dfb496cca67e13b", "MD5 calculation failed")
  25. XCTAssertEqual("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".md5(), "d174ab98d277d9f5a5611c2c9f419d9f", "MD5 calculation failed")
  26. XCTAssertEqual("12345678901234567890123456789012345678901234567890123456789012345678901234567890".md5(), "57edf4a22be3c955ac49da2e2107b67a", "MD5 calculation failed")
  27. }
  28. func testMD5Updates() {
  29. do {
  30. var hash = MD5()
  31. let _ = try hash.update(withBytes: [0x31, 0x32])
  32. let _ = try hash.update(withBytes: [0x33])
  33. let result = try hash.finish()
  34. XCTAssertEqual(result, [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70])
  35. } catch {
  36. XCTFail()
  37. }
  38. }
  39. func testMD5PerformanceSwift() {
  40. self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
  41. let arr = Array<UInt8>(repeating: 200, count: 1024 * 1024)
  42. self.startMeasuring()
  43. _ = Digest.md5(arr)
  44. self.stopMeasuring()
  45. })
  46. }
  47. func testSHA1() {
  48. let data:Data = Data(bytes: UnsafePointer<UInt8>([0x31, 0x32, 0x33] as Array<UInt8>), count: 3)
  49. if let hash = data.sha1() {
  50. XCTAssertEqual(hash.toHexString(), "40bd001563085fc35165329ea1ff5c5ecbdbbeef", "SHA1 calculation failed");
  51. }
  52. XCTAssertEqual("abc".sha1(), "a9993e364706816aba3e25717850c26c9cd0d89d", "SHA1 calculation failed")
  53. XCTAssertEqual("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha1(), "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "SHA1 calculation failed")
  54. XCTAssertEqual("".sha1(), "da39a3ee5e6b4b0d3255bfef95601890afd80709", "SHA1 calculation failed")
  55. }
  56. func testSHA224() {
  57. let data:Data = Data(bytes: UnsafePointer<UInt8>([0x31, 0x32, 0x33] as Array<UInt8>), count: 3)
  58. if let hash = data.sha224() {
  59. XCTAssertEqual(hash.toHexString(), "78d8045d684abd2eece923758f3cd781489df3a48e1278982466017f", "SHA224 calculation failed");
  60. }
  61. }
  62. func testSHA256() {
  63. let data:Data = Data(bytes: UnsafePointer<UInt8>([0x31, 0x32, 0x33] as Array<UInt8>), count: 3)
  64. if let hash = data.sha256() {
  65. XCTAssertEqual(hash.toHexString(), "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3", "SHA256 calculation failed");
  66. }
  67. XCTAssertEqual("Rosetta code".sha256(), "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf", "SHA256 calculation failed")
  68. XCTAssertEqual("".sha256(), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "SHA256 calculation failed")
  69. }
  70. func testSHA384() {
  71. let data:Data = Data(bytes: UnsafePointer<UInt8>([49, 50, 51] as Array<UInt8>), count: 3)
  72. if let hash = data.sha384() {
  73. XCTAssertEqual(hash.toHexString(), "9a0a82f0c0cf31470d7affede3406cc9aa8410671520b727044eda15b4c25532a9b5cd8aaf9cec4919d76255b6bfb00f", "SHA384 calculation failed");
  74. }
  75. XCTAssertEqual("The quick brown fox jumps over the lazy dog.".sha384(), "ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7", "SHA384 calculation failed");
  76. XCTAssertEqual("".sha384(), "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", "SHA384 calculation failed")
  77. XCTAssertEqual("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".sha384(), "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039", "SHA384 calculation failed")
  78. }
  79. func testSHA512() {
  80. XCTAssertEqual("abc".sha512(), "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", "length 24 bits");
  81. XCTAssertEqual("".sha512(), "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", "the bit string of length 0");
  82. XCTAssertEqual("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha512(), "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445", "length 448 bits");
  83. XCTAssertEqual("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".sha512(), "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909", "length 896 bits");
  84. XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1000000).sha512(), Array<UInt8>(hex: "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
  85. }
  86. func testCRC32() {
  87. let data:Data = Data(bytes: UnsafePointer<UInt8>([49, 50, 51] as Array<UInt8>), count: 3)
  88. if let crc = data.crc32(seed: nil) {
  89. XCTAssertEqual(crc.toHexString(), "884863d2", "CRC32 calculation failed");
  90. }
  91. XCTAssertEqual("".crc32(seed: nil), "00000000", "CRC32 calculation failed");
  92. }
  93. func testCRC32NotReflected() {
  94. let bytes : Array<UInt8> = [0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39]
  95. let data:Data = Data(bytes: UnsafePointer<UInt8>(bytes), count: bytes.count)
  96. if let crc = data.crc32(seed: nil, reflect: false) {
  97. XCTAssertEqual(crc.toHexString(), "fc891918", "CRC32 (with reflection) calculation failed");
  98. }
  99. XCTAssertEqual("".crc32(seed: nil, reflect: false), "00000000", "CRC32 (with reflection) calculation failed");
  100. }
  101. func testCRC16() {
  102. let result = Checksum.crc16([49,50,51,52,53,54,55,56,57] as Array<UInt8>)
  103. XCTAssert(result == 0xBB3D, "CRC16 failed")
  104. }
  105. func testChecksum() {
  106. let data:Data = Data(bytes: UnsafePointer<UInt8>([49, 50, 51] as Array<UInt8>), count: 3)
  107. XCTAssert(data.checksum() == 0x96, "Invalid checksum")
  108. }
  109. static let allTests = [
  110. ("testMD5Data", testMD5Data),
  111. ("testMD5String", testMD5String),
  112. ("testMD5Updates", testMD5Updates),
  113. ("testMD5PerformanceSwift", testMD5PerformanceSwift),
  114. ("testSHA1", testSHA1),
  115. ("testSHA224", testSHA224),
  116. ("testSHA256", testSHA256),
  117. ("testSHA384", testSHA384),
  118. ("testSHA512", testSHA512),
  119. ("testCRC32", testCRC32),
  120. ("testCRC32NotReflected", testCRC32NotReflected),
  121. ("testCRC15", testCRC16),
  122. ("testChecksum", testChecksum)
  123. ]
  124. }