PBKDF.swift 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // CryptoSwift
  3. //
  4. // Created by Marcin Krzyzanowski on 04/04/16.
  5. // Copyright © 2016 Marcin Krzyzanowski. All rights reserved.
  6. //
  7. import XCTest
  8. @testable import CryptoSwift
  9. class PBKDF: XCTestCase {
  10. func testPBKDF1() {
  11. let password: Array<UInt8> = [0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64]
  12. let salt: Array<UInt8> = [0x78, 0x57, 0x8e, 0x5a, 0x5d, 0x63, 0xcb, 0x06]
  13. let value = try! PKCS5.PBKDF1(password: password, salt: salt, iterations: 1000, keyLength: 16).calculate()
  14. XCTAssertEqual(value.toHexString(), "dc19847e05c64d2faf10ebfb4a3d2a20")
  15. }
  16. func testPBKDF2() {
  17. // P = "password", S = "salt", c = 1, dkLen = 20
  18. XCTAssertEqual([0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06, 0x2f, 0xe0, 0x37, 0xa6],
  19. try PKCS5.PBKDF2(password: [0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64], salt: [0x73, 0x61, 0x6c, 0x74], iterations: 1, keyLength: 20, variant: .sha1).calculate())
  20. // P = "password", S = "salt", c = 2, dkLen = 20
  21. XCTAssertEqual([0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0, 0xd8, 0xde, 0x89, 0x57],
  22. try PKCS5.PBKDF2(password: [0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64], salt: [0x73, 0x61, 0x6c, 0x74], iterations: 2, keyLength: 20, variant: .sha1).calculate())
  23. // P = "password", S = "salt", c = 4096, dkLen = 20
  24. XCTAssertEqual([0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0, 0x65, 0xa4, 0x29, 0xc1],
  25. try PKCS5.PBKDF2(password: [0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64], salt: [0x73, 0x61, 0x6c, 0x74], iterations: 4096, keyLength: 20, variant: .sha1).calculate())
  26. // P = "password", S = "salt", c = 16777216, dkLen = 20
  27. // Commented because it takes a lot of time with Debug build to finish.
  28. // XCTAssertEqual([0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4, 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c, 0x26, 0x34, 0xe9, 0x84],
  29. // try PKCS5.PBKDF2(password: [0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64], salt: [0x73, 0x61, 0x6C, 0x74], iterations: 16777216, keyLength: 20, variant: .sha1).calculate())
  30. // P = "passwordPASSWORDpassword", S = "saltSALTsaltSALTsaltSALTsaltSALTsalt", c = 4096, dkLen = 25
  31. 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],
  32. 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())
  33. // P = "pass\0word", S = "sa\0lt", c = 4096, dkLen = 16
  34. XCTAssertEqual([0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3],
  35. 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())
  36. }
  37. func testPBKDF2Length() {
  38. let password: Array<UInt8> = "s33krit".bytes
  39. let salt: Array<UInt8> = "nacl".bytes
  40. let value = try! PKCS5.PBKDF2(password: password, salt: salt, iterations: 2, keyLength: 8, variant: .sha1).calculate()
  41. XCTAssertEqual(value.toHexString(), "a53cf3df485e5cd9")
  42. }
  43. #if !CI
  44. func testPerformance() {
  45. let password: Array<UInt8> = "s33krit".bytes
  46. let salt: Array<UInt8> = "nacl".bytes
  47. measure {
  48. _ = try! PKCS5.PBKDF2(password: password, salt: salt, iterations: 65536, keyLength: 32, variant: .sha1).calculate()
  49. }
  50. }
  51. #endif
  52. static func allTests() -> [(String, (PBKDF) -> () -> Void)] {
  53. var tests = [
  54. ("testPBKDF1", testPBKDF1),
  55. ("testPBKDF2", testPBKDF2),
  56. ("testPBKDF2Length", testPBKDF2Length),
  57. ]
  58. #if !CI
  59. tests += [("testPerformance", testPerformance)]
  60. #endif
  61. return tests
  62. }
  63. }