PBKDF.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // PBKDF.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 04/04/16.
  6. // Copyright © 2016 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. import XCTest
  9. @testable import CryptoSwift
  10. class PBKDF: XCTestCase {
  11. func testPBKDF1() {
  12. let password: Array<UInt8> = [0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64]
  13. let salt: Array<UInt8> = [0x78, 0x57, 0x8E, 0x5A, 0x5D, 0x63, 0xCB, 0x06]
  14. let value = try! PKCS5.PBKDF1(password: password, salt: salt, iterations: 1000, keyLength: 16).calculate()
  15. XCTAssertEqual(value.toHexString(), "dc19847e05c64d2faf10ebfb4a3d2a20")
  16. }
  17. func testPBKDF2() {
  18. // P = "password", S = "salt", c = 1, dkLen = 20
  19. XCTAssertEqual([0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06, 0x2f, 0xe0, 0x37, 0xa6],
  20. try PKCS5.PBKDF2(password: [0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64], salt: [0x73, 0x61, 0x6C, 0x74], iterations: 1, keyLength: 20, variant: .sha1).calculate())
  21. // P = "password", S = "salt", c = 2, dkLen = 20
  22. XCTAssertEqual([0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0, 0xd8, 0xde, 0x89, 0x57],
  23. try PKCS5.PBKDF2(password: [0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64], salt: [0x73, 0x61, 0x6C, 0x74], iterations: 2, keyLength: 20, variant: .sha1).calculate())
  24. // P = "password", S = "salt", c = 4096, dkLen = 20
  25. XCTAssertEqual([0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0, 0x65, 0xa4, 0x29, 0xc1],
  26. try PKCS5.PBKDF2(password: [0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64], salt: [0x73, 0x61, 0x6C, 0x74], iterations: 4096, keyLength: 20, variant: .sha1).calculate())
  27. // P = "password", S = "salt", c = 16777216, dkLen = 20
  28. // Commented because it takes a lot of time with Debug build to finish.
  29. // XCTAssertEqual([0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4, 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c, 0x26, 0x34, 0xe9, 0x84],
  30. // try PKCS5.PBKDF2(password: [0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64], salt: [0x73, 0x61, 0x6C, 0x74], iterations: 16777216, keyLength: 20, variant: .sha1).calculate())
  31. // P = "passwordPASSWORDpassword", S = "saltSALTsaltSALTsaltSALTsaltSALTsalt", c = 4096, dkLen = 25
  32. XCTAssertEqual([0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b, 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a, 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70, 0x38],
  33. try PKCS5.PBKDF2(password: [0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64, 0x50, 0x41, 0x53, 0x53, 0x57, 0x4F, 0x52, 0x44, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64], salt: [0x73, 0x61, 0x6C, 0x74, 0x53, 0x41, 0x4C, 0x54, 0x73, 0x61, 0x6C, 0x74, 0x53, 0x41, 0x4C, 0x54, 0x73, 0x61, 0x6C, 0x74, 0x53, 0x41, 0x4C, 0x54, 0x73, 0x61, 0x6C, 0x74, 0x53, 0x41, 0x4C, 0x54, 0x73, 0x61, 0x6C, 0x74], iterations: 4096, keyLength: 25, variant: .sha1).calculate())
  34. // P = "pass\0word", S = "sa\0lt", c = 4096, dkLen = 16
  35. XCTAssertEqual([0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3],
  36. try PKCS5.PBKDF2(password: [0x70, 0x61, 0x73, 0x73, 0x00, 0x77, 0x6F, 0x72, 0x64], salt: [0x73, 0x61, 0x00, 0x6C, 0x74], iterations: 4096, keyLength: 16, variant: .sha1).calculate())
  37. }
  38. func testPBKDF2Length() {
  39. let password: Array<UInt8> = Array("s33krit".utf8)
  40. let salt: Array<UInt8> = Array("nacl".utf8)
  41. let value = try! PKCS5.PBKDF2(password: password, salt: salt, iterations: 2, keyLength: 8, variant: .sha1).calculate()
  42. XCTAssert(value.toHexString() == "a53cf3df485e5cd9", "PBKDF2 fail")
  43. }
  44. static let allTests = [
  45. ("testPBKDF1", testPBKDF1),
  46. ("testPBKDF2", testPBKDF2),
  47. ("testPBKDF2Length", testPBKDF2Length),
  48. ]
  49. }