2 balloon -- Balloon password hashing function
3 Copyright (C) 2016-2023 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program. If not, see
16 <http://www.gnu.org/licenses/>.
31 func TestB(t *testing.T) {
32 f := func(passwd, salt []byte, s, t uint8) bool {
33 if len(passwd) == 0 || len(salt) == 0 {
36 B(sha512.New(), passwd, salt, uint64(s)%16+1, uint64(t)%16+1)
39 if err := quick.Check(f, nil); err != nil {
44 func TestH(t *testing.T) {
45 f := func(passwd, salt []byte, s, t, p uint8) bool {
46 if len(passwd) == 0 || len(salt) == 0 {
49 H(sha512.New, passwd, salt, int(s)%16+1, int(t)%16+1, int(p)%8+1)
52 if err := quick.Check(f, nil); err != nil {
57 func BenchmarkB(b *testing.B) {
58 passwd := make([]byte, 8)
60 salt := make([]byte, 8)
63 sCost := uint64(1 << 10 / h.Size())
65 for i := 0; i < b.N; i++ {
66 B(h, passwd, salt, sCost, 4)
70 func BenchmarkH(b *testing.B) {
71 passwd := make([]byte, 8)
73 salt := make([]byte, 8)
75 sCost := 1 << 10 / sha512.New().Size()
77 for i := 0; i < b.N; i++ {
78 H(sha512.New, passwd, salt, sCost, 4, 4)
82 func mustHexDecode(s string) []byte {
83 b, err := hex.DecodeString(s)
90 func TestVectors(t *testing.T) {
91 // taken from Nettle 3.9
93 B(sha256.New(), []byte("password"), []byte("salt"), 1, 1),
94 mustHexDecode("eefda4a8a75b461fa389c1dcfaf3e9dfacbc26f81f22e6f280d15cc18c417545"),
100 B(sha256.New(), []byte{0}, []byte{0}, 3, 3),
101 mustHexDecode("4fc7e302ffa29ae0eac31166cee7a552d1d71135f4e0da66486fb68a749b73a4"),
107 B(sha256.New(), nil, []byte("salt"), 3, 3),
108 mustHexDecode("5f02f8206f9cd212485c6bdf85527b698956701ad0852106f94b94ee94577378"),
114 B(sha256.New(), []byte("password"), nil, 3, 3),
115 mustHexDecode("20aa99d7fe3f4df4bd98c655c5480ec98b143107a331fd491deda885c4d6a6cc"),
121 B(sha256.New(), []byte("hunter42"), []byte("examplesalt"), 1024, 3),
122 mustHexDecode("716043dff777b44aa7b88dcbab12c078abecfac9d289c5b5195967aa63440dfb"),