]> git.openstreetmap.org Git - rails.git/blob - test/lib/password_hash_test.rb
Update to rails 7.0.2.2
[rails.git] / test / lib / password_hash_test.rb
1 require "test_helper"
2
3 class PasswordHashTest < ActiveSupport::TestCase
4   def test_md5_without_salt
5     assert PasswordHash.check("5f4dcc3b5aa765d61d8327deb882cf99", nil, "password")
6     assert_not PasswordHash.check("5f4dcc3b5aa765d61d8327deb882cf99", nil, "wrong")
7     assert PasswordHash.upgrade?("5f4dcc3b5aa765d61d8327deb882cf99", nil)
8   end
9
10   def test_md5_with_salt
11     assert PasswordHash.check("67a1e09bb1f83f5007dc119c14d663aa", "salt", "password")
12     assert_not PasswordHash.check("67a1e09bb1f83f5007dc119c14d663aa", "salt", "wrong")
13     assert_not PasswordHash.check("67a1e09bb1f83f5007dc119c14d663aa", "wrong", "password")
14     assert PasswordHash.upgrade?("67a1e09bb1f83f5007dc119c14d663aa", "salt")
15   end
16
17   def test_pbkdf2_1000_32_sha512
18     assert PasswordHash.check("ApT/28+FsTBLa/J8paWfgU84SoRiTfeY8HjKWhgHy08=", "sha512!1000!HR4z+hAvKV2ra1gpbRybtoNzm/CNKe4cf7bPKwdUNrk=", "password")
19     assert_not PasswordHash.check("ApT/28+FsTBLa/J8paWfgU84SoRiTfeY8HjKWhgHy08=", "sha512!1000!HR4z+hAvKV2ra1gpbRybtoNzm/CNKe4cf7bPKwdUNrk=", "wrong")
20     assert_not PasswordHash.check("ApT/28+FsTBLa/J8paWfgU84SoRiTfeY8HjKWhgHy08=", "sha512!1000!HR4z+hAvKV2ra1gwrongtoNzm/CNKe4cf7bPKwdUNrk=", "password")
21     assert PasswordHash.upgrade?("ApT/28+FsTBLa/J8paWfgU84SoRiTfeY8HjKWhgHy08=", "sha512!1000!HR4z+hAvKV2ra1gpbRybtoNzm/CNKe4cf7bPKwdUNrk=")
22   end
23
24   def test_pbkdf2_10000_32_sha512
25     assert PasswordHash.check("3wYbPiOxk/tU0eeIDjUhdvi8aDP3AbFtwYKKxF1IhGg=", "sha512!10000!OUQLgtM7eD8huvanFT5/WtWaCwdOdrir8QOtFwxhO0A=", "password")
26     assert_not PasswordHash.check("3wYbPiOxk/tU0eeIDjUhdvi8aDP3AbFtwYKKxF1IhGg=", "sha512!10000!OUQLgtM7eD8huvanFT5/WtWaCwdOdrir8QOtFwxhO0A=", "wrong")
27     assert_not PasswordHash.check("3wYbPiOxk/tU0eeIDjUhdvi8aDP3AbFtwYKKxF1IhGg=", "sha512!10000!OUQLgtMwronguvanFT5/WtWaCwdOdrir8QOtFwxhO0A=", "password")
28     assert PasswordHash.upgrade?("3wYbPiOxk/tU0eeIDjUhdvi8aDP3AbFtwYKKxF1IhGg=", "sha512!10000!OUQLgtM7eD8huvanFT5/WtWaCwdOdrir8QOtFwxhO0A=")
29   end
30
31   def test_argon2_upgradeable
32     assert PasswordHash.check("$argon2id$v=19$m=65536,t=1,p=1$KXGHWfWMf5H5kY4uU3ua8A$YroVvX6cpJpljTio62k19C6UpuIPtW7me2sxyU2dyYg", nil, "password")
33     assert_not PasswordHash.check("$argon2id$v=19$m=65536,t=1,p=1$KXGHWfWMf5H5kY4uU3ua8A$YroVvX6cpJpljTio62k19C6UpuIPtW7me2sxyU2dyYg", nil, "wrong")
34     assert PasswordHash.upgrade?("$argon2id$v=19$m=65536,t=1,p=1$KXGHWfWMf5H5kY4uU3ua8A$YroVvX6cpJpljTio62k19C6UpuIPtW7me2sxyU2dyYg", nil)
35   end
36
37   def test_argon2
38     assert PasswordHash.check("$argon2id$v=19$m=65536,t=2,p=1$b2E7zSvjT6TC5DXrqvfxwg$P4hly807ckgYc+kfvaf3rqmJcmKStzw+kV14oMaz8PQ", nil, "password")
39     assert_not PasswordHash.check("$argon2id$v=19$m=65536,t=2,p=1$b2E7zSvjT6TC5DXrqvfxwg$P4hly807ckgYc+kfvaf3rqmJcmKStzw+kV14oMaz8PQ", nil, "wrong")
40     assert_not PasswordHash.upgrade?("$argon2id$v=19$m=65536,t=2,p=1$b2E7zSvjT6TC5DXrqvfxwg$P4hly807ckgYc+kfvaf3rqmJcmKStzw+kV14oMaz8PQ", nil)
41   end
42
43   def test_default
44     hash1, salt1 = PasswordHash.create("password")
45     hash2, salt2 = PasswordHash.create("password")
46     assert_not_equal hash1, hash2
47     assert_nil salt1
48     assert_nil salt2
49     assert PasswordHash.check(hash1, salt1, "password")
50     assert_not PasswordHash.check(hash1, salt1, "wrong")
51     assert PasswordHash.check(hash2, salt2, "password")
52     assert_not PasswordHash.check(hash2, salt2, "wrong")
53     assert_not PasswordHash.upgrade?(hash1, salt1)
54     assert_not PasswordHash.upgrade?(hash2, salt2)
55   end
56
57   def test_format
58     hash, _salt = PasswordHash.create("password")
59     format = Argon2::HashFormat.new(hash)
60
61     assert_equal "argon2id", format.variant
62     assert format.version <= 19
63   end
64 end