HMACTests.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // HMACTests.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 29/08/14.
  6. // Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. import Foundation
  9. import XCTest
  10. import CryptoSwift
  11. class HMACTests: XCTestCase {
  12. override func setUp() {
  13. super.setUp()
  14. }
  15. override func tearDown() {
  16. super.tearDown()
  17. }
  18. func testMD5() {
  19. let key:[UInt8] = []
  20. let msg:[UInt8] = []
  21. let expectedMac:[UInt8] = [0x74,0xe6,0xf7,0x29,0x8a,0x9c,0x2d,0x16,0x89,0x35,0xf5,0x8c,0x00,0x1b,0xad,0x88]
  22. let hmac = Authenticator.HMAC(key: NSData.withBytes(key), variant: .md5).authenticate(NSData.withBytes(msg))
  23. XCTAssertEqual(hmac!, NSData.withBytes(expectedMac), "Invalid authentication result")
  24. }
  25. func testSHA1() {
  26. let key:[UInt8] = []
  27. let msg:[UInt8] = []
  28. let expectedMac:[UInt8] = [0xfb,0xdb,0x1d,0x1b,0x18,0xaa,0x6c,0x08,0x32,0x4b,0x7d,0x64,0xb7,0x1f,0xb7,0x63,0x70,0x69,0x0e,0x1d]
  29. let hmac = Authenticator.HMAC(key: NSData.withBytes(key), variant: .sha1).authenticate(NSData.withBytes(msg))
  30. XCTAssertEqual(hmac!, NSData.withBytes(expectedMac), "Invalid authentication result")
  31. }
  32. func testSHA256() {
  33. let key:[UInt8] = []
  34. let msg:[UInt8] = []
  35. let expectedMac:[UInt8] = [0xb6,0x13,0x67,0x9a,0x08,0x14,0xd9,0xec,0x77,0x2f,0x95,0xd7,0x78,0xc3,0x5f,0xc5,0xff,0x16,0x97,0xc4,0x93,0x71,0x56,0x53,0xc6,0xc7,0x12,0x14,0x42,0x92,0xc5,0xad]
  36. let hmac = Authenticator.HMAC(key: NSData.withBytes(key), variant: .sha256).authenticate(NSData.withBytes(msg))
  37. XCTAssertEqual(hmac!, NSData.withBytes(expectedMac), "Invalid authentication result")
  38. }
  39. }