ScryptTests.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2021 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  10. //
  11. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. import XCTest
  16. @testable import CryptoSwift
  17. class Scrypt: XCTestCase {
  18. func testScrypt_0() {
  19. let password = Array("password".data(using: .ascii)!)
  20. let salt = Array("NaCl".data(using: .ascii)!)
  21. let deriver = try! CryptoSwift.Scrypt(password: password, salt: salt, dkLen: 64, N: 1024, r: 8, p: 16)
  22. let derived = try! deriver.calculate()
  23. let expected: [UInt8] = Array<UInt8>.init(hex: """
  24. fd ba be 1c 9d 34 72 00 78 56 e7 19 0d 01 e9 fe
  25. 7c 6a d7 cb c8 23 78 30 e7 73 76 63 4b 37 31 62
  26. 2e af 30 d9 2e 22 a3 88 6f f1 09 27 9d 98 30 da
  27. c7 27 af b9 4a 83 ee 6d 83 60 cb df a2 cc 06 40
  28. """.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\t", with: ""))
  29. XCTAssertEqual(derived, expected)
  30. }
  31. func testScrypt_1() {
  32. let password = Array("pleaseletmein".data(using: .ascii)!)
  33. let salt = Array("SodiumChloride".data(using: .ascii)!)
  34. let deriver = try! CryptoSwift.Scrypt(password: password, salt: salt, dkLen: 64, N: 16384, r: 8, p: 1)
  35. let derived = try! deriver.calculate()
  36. let expected: [UInt8] = Array<UInt8>.init(hex: """
  37. 70 23 bd cb 3a fd 73 48 46 1c 06 cd 81 fd 38 eb
  38. fd a8 fb ba 90 4f 8e 3e a9 b5 43 f6 54 5d a1 f2
  39. d5 43 29 55 61 3f 0f cf 62 d4 97 05 24 2a 9a f9
  40. e6 1e 85 dc 0d 65 1e 40 df cf 01 7b 45 57 58 87
  41. """.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\t", with: ""))
  42. XCTAssertEqual(derived, expected)
  43. }
  44. // Takes too long to run in debug mode!
  45. func testScrypt_2() {
  46. #if !DEBUG
  47. let password = Array("pleaseletmein".data(using: .ascii)!)
  48. let salt = Array("SodiumChloride".data(using: .ascii)!)
  49. let deriver = try! CryptoSwift.Scrypt(password: password, salt: salt, dkLen: 64, N: 1048576, r: 8, p: 1)
  50. let derived = try! deriver.calculate()
  51. let expected: [UInt8] = Array<UInt8>.init(hex: """
  52. 21 01 cb 9b 6a 51 1a ae ad db be 09 cf 70 f8 81
  53. ec 56 8d 57 4a 2f fd 4d ab e5 ee 98 20 ad aa 47
  54. 8e 56 fd 8f 4b a5 d0 9f fa 1c 6d 92 7c 40 f4 c3
  55. 37 30 40 49 e8 a9 52 fb cb f4 5c 6f a7 7a 41 a4
  56. """.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\t", with: ""))
  57. XCTAssertEqual(derived, expected)
  58. #endif
  59. }
  60. static let allTests = [
  61. ("testScrypt_0", testScrypt_0),
  62. ("testScrypt_1", testScrypt_1),
  63. ("testScrypt_2", testScrypt_2)
  64. ]
  65. }