]> Cypherpunks.ru repositories - balloon.git/blob - balloon_test.go
68c108f4ac4b26c53df8d8a0a0fd2afe35c39c6b
[balloon.git] / balloon_test.go
1 /*
2 balloon -- Balloon password hashing function
3 Copyright (C) 2016-2018 Sergey Matveev <stargrave@stargrave.org>
4
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 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this program.  If not, see
17 <http://www.gnu.org/licenses/>.
18 */
19
20 package balloon
21
22 import (
23         "crypto/rand"
24         "crypto/sha256"
25         "testing"
26         "testing/quick"
27 )
28
29 func TestB(t *testing.T) {
30         f := func(passwd, salt []byte, s, t uint8) bool {
31                 if len(passwd) == 0 || len(salt) == 0 {
32                         return true
33                 }
34                 B(sha256.New(), passwd, salt, int(s)%16+1, int(t)%16+1)
35                 return true
36         }
37         if err := quick.Check(f, nil); err != nil {
38                 t.Error(err)
39         }
40 }
41
42 func TestH(t *testing.T) {
43         f := func(passwd, salt []byte, s, t, p uint8) bool {
44                 if len(passwd) == 0 || len(salt) == 0 {
45                         return true
46                 }
47                 H(sha256.New, passwd, salt, int(s)%16+1, int(t)%16+1, int(p)%8+1)
48                 return true
49         }
50         if err := quick.Check(f, nil); err != nil {
51                 t.Error(err)
52         }
53 }
54
55 func BenchmarkB(b *testing.B) {
56         passwd := make([]byte, 8)
57         rand.Read(passwd)
58         salt := make([]byte, 8)
59         rand.Read(salt)
60         b.ResetTimer()
61         for i := 0; i < b.N; i++ {
62                 B(sha256.New(), passwd, salt, 1<<10/sha256.New().Size(), 4)
63         }
64 }
65
66 func BenchmarkH(b *testing.B) {
67         passwd := make([]byte, 8)
68         rand.Read(passwd)
69         salt := make([]byte, 8)
70         rand.Read(salt)
71         b.ResetTimer()
72         for i := 0; i < b.N; i++ {
73                 H(sha256.New, passwd, salt, 1<<10/sha256.New().Size(), 4, 4)
74         }
75 }