<commit_before>require 'test_helper'

class LowLevelArgon2Test < Minitest::Test
  def test_that_it_has_a_version_number
    refute_nil ::Argon2::VERSION
  end

  def test_ffi_vector
    #./argon2 password somesalt -t 2 -m 16
    #Hash: 894af4ff2e2d26f3ce15f77a7e1c25db45b4e20439e9961772ba199caddb001e
    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>", 2, 16),
        '894af4ff2e2d26f3ce15f77a7e1c25db45b4e20439e9961772ba199caddb001e'

    #./argon2 password somesalt -t 2 -m 20
    #Hash: 58d4d929aeeafa40cc049f032035784fb085e8e0d0c5a51ea067341a93d6d286
    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>", 2, 20),
        '5<PASSWORD>035784fb085e8e0d0c5a51ea067341a93d6d286'

    #./argon2 password somesalt -t 2 -m 18
    #Hash: 55292398cce8fc78685e610d004ca9bda5c325a0a2e6285a0de5f816df139aa6
    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>", 2, 18),
        '55292398cce8fc78685e610d004ca9bda5c325a0a2e6285a0de5f81<PASSWORD>'

    #./argon2 password some<PASSWORD> -t 2 -m 8
    #Hash: e346b1e1aa7ca58c9bb862e223ba5604064398d4394e49e90972c6b54cef43ed
    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>", 2, 8),
        'e346b1e1aa7ca58c9bb862e223ba5604064398d4394e49e90972c6b54cef43ed'

    #./argon2 password some<PASSWORD> -t 1 -m 16
    #Hash: b49199e4ecb0f6659e6947f945e391c940b17106e1d0b0a9888006c7f87a789b
    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>", 1, 16),
        '<PASSWORD>'

    #./argon2 password somesalt -t 4 -m 16
    #Hash: 72207b3312d79995fbe7b30664837ae1246f9a98e07eac34835ca3498e705f85
    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>", 4, 16),
        '7<PASSWORD>95<PASSWORD>7b30664837ae1246f9a98e07eac34835ca3498e705f85'

    #./argon2 differentpassword some<PASSWORD> -t 2 -m 16 -p 1
    #Hash: 8e286f605ed7383987a4aac25a28a04808593b6e17613bc31457146c4f3f4361
    assert_equal Argon2::Engine.hash_argon2i(
        "differentpassword", "<PASSWORD>\0\0\0\0\0\0", 2, 16),
        '8e286f605ed7383987a4aac25a28a04808593b6e17613bc31457146c4f3f4361'

    #./argon2 password <PASSWORD> -t 2 -m 16 -p 1
    #Hash: 8f65b47d902fb2aee5e0b2bdc9041b249fc11f06f35551e0bee52716b41e8311
    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>\0\0\0\<PASSWORD>", 2, 16),
        '<PASSWORD>11f06f35551e0bee52716b41e8311'
  end

  def test_encoded_hash
    #./util.rb 894af4ff2e2d26f3ce15f77a7e1c25db45b4e20439e9961772ba199caddb001e
    #iUr0/y4tJvPOFfd6fhwl20W04gQ56ZYXcroZnK3bAB4
    assert_equal Argon2::Engine.hash_argon2i_encode(
        "password", "<PASSWORD>", 2, 16, nil),
        '$argon2i$m=65536,t=2,p=1$c29tZXNhbHQAAAAAAAAAAA$iUr0/y4tJvPOFfd6fhwl20W04gQ56ZYXcroZnK3bAB4'

    #./util.rb 58d4d929aeeafa40cc049f032035784fb085e8e0d0c5a51ea067341a93d6d286
    #WNTZKa7q+kDMBJ8DIDV4T7CF6ODQxaUeoGc0GpPW0oY
    assert_equal Argon2::Engine.hash_argon2i_encode(
        "password", "<PASSWORD>", 2, 20, nil),
        '$argon2i$m=1048576,t=2,p=1$c29tZXNhbHQAAAAAAAAAAA$WNTZKa7q+kDMBJ8DIDV4T7CF6ODQxaUeoGc0GpPW0oY'

    assert_equal Argon2::Engine.hash_argon2i_encode(
        "password", "<PASSWORD>", 2, 20, "secretkey"),
        '$argon2i$m=1048576,t=2,p=1$c29tZXNhbHQAAAAAAAAAAA$jeQsDsHmEllAkvTonKB5qrMPYnHLNqlKBP/j8zU6g+w'

    #./util.rb e346b1e1aa7ca58c9bb862e223ba5604064398d4394e49e90972c6b54cef43ed
    #40ax4ap8pYybuGLiI7pWBAZDmNQ5TknpCXLGtUzvQ+0
    assert_equal Argon2::Engine.hash_argon2i_encode(
        "password", "<PASSWORD>", 2, 8, nil),
        '$argon2i$m=256,t=2,p=1$c29tZXNhbHQAAAAAAAAAAA$40ax4ap8pYybuGLiI7pWBAZDmNQ5TknpCXLGtUzvQ+0'

    #./util.rb b49199e4ecb0f6659e6947f945e391c940b17106e1d0b0a9888006c7f87a789b
    #tJGZ5Oyw9mWeaUf5ReORyUCxcQbh0LCpiIAGx/h6eJs
    assert_equal Argon2::Engine.hash_argon2i_encode(
        "password", "<PASSWORD>", 1, 16, nil),
        '$argon2i$m=65536,t=1,p=1$c29tZXNhbHQAAAAAAAAAAA$tJGZ5Oyw9mWeaUf5ReORyUCxcQbh0LCpiIAGx/h6eJs'

    #./util.rb 8e286f605ed7383987a4aac25a28a04808593b6e17613bc31457146c4f3f4361
    #jihvYF7XODmHpKrCWiigSAhZO24XYTvDFFcUbE8/Q2E
    assert_equal Argon2::Engine.hash_argon2i_encode(
        "differentpassword", "<PASSWORD>", 2, 16, nil),
        '$argon2i$m=65536,t=2,p=1$c29tZXNhbHQAAAAAAAAAAA$jihvYF7XODmHpKrCWiigSAhZO24XYTvDFFcUbE8/Q2E'

    #./util.rb 8f65b47d902fb2aee5e0b2bdc9041b249fc11f06f35551e0bee52716b41e8311
    #j2W0fZAvsq7l4LK9yQQbJJ/BHwbzVVHgvuUnFrQegxE
    assert_equal Argon2::Engine.hash_argon2i_encode(
        "password", "<PASSWORD>", 2, 16, nil),
        '$argon2i$m=65536,t=2,p=1$ZGlmZnNhbHQAAAAAAAAAAA$j2W0fZAvsq7l4LK9yQQbJJ/BHwbzVVHgvuUnFrQegxE'
  end

  def test_encode
    assert Argon2::Engine.argon2i_verify("password", <PASSWORD>$m=<PASSWORD>,t=2,p=1$<PASSWORD>tZXNhbHQAAAAAAAAAAA$iUr0/y4tJvPOF<PASSWORD>l20W04gQ56ZYXcroZnK3bAB4", nil)
    refute Argon2::Engine.argon2i_verify("notword", "$argon2i$m=65536,t=2,p=1$c29tZXNhbHQAAAAAAAAAAA$iUr0/y4tJvPOFfd6fhwl20W04gQ56ZYXcroZnK3bAB4", nil)
  end
end<commit_msg>Updated test vectors for 1.3 algorithm. Removed test vectors with high memory requirements that could DoS low end devices.<commit_after>require 'test_helper'

# Vectors in this suite taken from test.c in reference

class LowLevelArgon2Test < Minitest::Test
  def test_that_it_has_a_version_number
    refute_nil ::Argon2::VERSION
  end

  def test_ffi_vector
    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>", 2, 16),
        '1c7eeef9e0e969b3024722fc864a1ca9f6ca20da73f9bf3f1731881beae2039e'

    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>", 2, 18),
        '<PASSWORD>'

    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>", 2, 8),
        '<PASSWORD>'

    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>\0\0\0\0\0\0\0", 1, 16),
        'fabd1ddbd86a101d326ac2abe79660202b10192925d2fd2483085df94df0c91a'

    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>", 4, 16),
        'b<PASSWORD>6e2c1cb1e7bffdb966ab3ceafae701d6b7789c3f1e6c6b22d82d99d5'

    assert_equal Argon2::Engine.hash_argon2i(
        "differentpassword", "<PASSWORD>\0\0\0\0\0\0", 2, 16),
        'b2db9d7c0d1288951aec4b6e1cd3835ea29a7da2ac13e6f48554a26b127146f9'

    assert_equal Argon2::Engine.hash_argon2i(
        "password", "<PASSWORD>", 2, 16),
        '<PASSWORD>'
  end

  def test_encoded_hash
    assert_equal Argon2::Engine.hash_argon2i_encode(
        "password", "<PASSWORD>", 2, 16, nil),
        '$argon2i$m=65536,t=2,p=1$c29tZXNhbHQAAAAAAAAAAA$HH7u+eDpabMCRyL8hkocqfbKINpz+b8/FzGIG+riA54'

    assert_equal Argon2::Engine.hash_argon2i_encode(
        "password", "<PASSWORD>", 2, 20, "secretkey"),
        '$argon2i$m=1048576,t=2,p=1$c29tZXNhbHQAAAAAAAAAAA$g4zEnkxFGFYY8LlY9ZT7H3uxo1/7tazYDZkCdLJz/Is'

    assert_equal Argon2::Engine.hash_argon2i_encode(
        "password", "<PASSWORD>", 2, 8, nil),
        '$argon2i$m=256,t=2,p=1$c29tZXNhbHQAAAAAAAAAAA$3+v51OrdaFn0zGqbsgBD/Z2n4eNr2s27BcpWn0Yyafg'

    assert_equal Argon2::Engine.hash_argon2i_encode(
        "password", "<PASSWORD>", 1, 16, nil),
        '$argon2i$m=65536,t=1,p=1$c29tZXNhbHQAAAAAAAAAAA$+r0d29hqEB0yasKr55ZgICsQGSkl0v0kgwhd+U3wyRo'

    assert_equal Argon2::Engine.hash_argon2i_encode(
        "differentpassword", "<PASSWORD>\0\0\0\0\0\0", 2, 16, nil),
        '$argon2i$m=65536,t=2,p=1$c29tZXNhbHQAAAAAAAAAAA$studfA0SiJUa7EtuHNODXqKafaKsE+b0hVSiaxJxRvk'

    assert_equal Argon2::Engine.hash_argon2i_encode(
        "password", "<PASSWORD>\0\0\0\0\0\0", 2, 16, nil),
        '$argon2i$m=65536,t=2,p=1$ZGlmZnNhbHQAAAAAAAAAAA$u2aGhl8sEJP3D1Q8lTX4B9W0LV3G1x8UpKeikZE+BeA'
  end

  def test_encode
    assert Argon2::Engine.argon2i_verify("password", <PASSWORD>,t=1,p=1$<PASSWORD>$+<PASSWORD>sQGSkl0v<PASSWORD>whd+U3wyRo", nil)
    refute Argon2::Engine.argon2i_verify("notword", <PASSWORD>,t=1,p=1$c29tZXNhbHQAAAAAAAAAAA$+r<PASSWORD>hq<PASSWORD>asKr55ZgICsQGSkl0v0kgwhd+U3wyRo", nil)
  end
end
{'fraction_non_alphanumeric': 0.11402962778538528, 'fraction_numerical': 0.18635628034358273, 'mean_word_length': 5.242424242424242, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 16, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2228681', 'n_tokens_mistral': 4635, 'n_tokens_neox': 3833, 'n_words': 385}