2022-12-07 14:19:04 -07:00
|
|
|
package scrypt_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/matryer/is"
|
|
|
|
|
|
|
|
"github.com/sour-is/go-passwd"
|
|
|
|
"github.com/sour-is/go-passwd/pkg/scrypt"
|
2022-12-07 17:04:46 -07:00
|
|
|
"github.com/sour-is/go-passwd/pkg/unix"
|
2022-12-07 14:19:04 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPasswdHash(t *testing.T) {
|
|
|
|
type testCase struct {
|
2022-12-10 08:58:08 -07:00
|
|
|
pass, hash []byte
|
2022-12-07 14:19:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []testCase{}
|
|
|
|
algos := scrypt.All
|
|
|
|
|
|
|
|
is := is.New(t)
|
|
|
|
// Generate additional test cases for each algo.
|
|
|
|
for _, algo := range algos {
|
2022-12-10 08:58:08 -07:00
|
|
|
hash, err := algo.Passwd([]byte("passwd"), nil)
|
2022-12-07 14:19:04 -07:00
|
|
|
is.NoErr(err)
|
2022-12-10 08:58:08 -07:00
|
|
|
tests = append(tests, testCase{[]byte("passwd"), hash})
|
2022-12-07 14:19:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pass := passwd.New(algos...)
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
t.Run(fmt.Sprint("Test-", i), func(t *testing.T) {
|
|
|
|
is := is.New(t)
|
|
|
|
|
|
|
|
hash, err := pass.Passwd(tt.pass, tt.hash)
|
|
|
|
is.NoErr(err)
|
|
|
|
is.Equal(hash, tt.hash)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-12-07 17:04:46 -07:00
|
|
|
|
|
|
|
func TestPasswdIsPreferred(t *testing.T) {
|
|
|
|
is := is.New(t)
|
|
|
|
|
|
|
|
pass := passwd.New(scrypt.Scrypt2, &unix.MD5{})
|
|
|
|
|
2022-12-10 08:58:08 -07:00
|
|
|
ok := pass.IsPreferred([]byte("16384$8$1$b97ed09792dd74b71dcb7fc8caf04a89$0b5cda82b17298ec4bf6d2139f7ea8587d8478fcc68c09e2506a7cf08b2817c0"))
|
2022-12-07 17:04:46 -07:00
|
|
|
is.True(!ok)
|
|
|
|
|
2022-12-10 08:58:08 -07:00
|
|
|
ok = pass.IsPreferred([]byte("$s2$16384$8$1$iEdwbgXyKa5GNGNW/0NsOA$9YN/hzbskVVDZ887ppqv5su0n8SxVXwDB/rhVhAc9xQ"))
|
2022-12-07 17:04:46 -07:00
|
|
|
is.True(ok)
|
|
|
|
|
2022-12-10 08:58:08 -07:00
|
|
|
ok = pass.IsPreferred([]byte("$s2$16384$7$1$iEdwbgXyKa5GNGNW/0NsOA$9YN/hzbskVVDZ887ppqv5su0n8SxVXwDB/rhVhAc9xQ"))
|
2022-12-07 17:04:46 -07:00
|
|
|
is.True(!ok)
|
|
|
|
|
2022-12-10 08:58:08 -07:00
|
|
|
ok = pass.IsPreferred([]byte("$1$76a2173be6393254e72ffa4d6df1030a"))
|
2022-12-07 17:04:46 -07:00
|
|
|
is.True(!ok)
|
|
|
|
}
|