initial commit

This commit is contained in:
Jon Lundy
2022-12-07 14:19:04 -07:00
commit 0c36a25333
10 changed files with 808 additions and 0 deletions

94
pkg/unix/unix.go Normal file
View File

@@ -0,0 +1,94 @@
package unix
import (
"crypto/md5"
"crypto/subtle"
"fmt"
"github.com/sour-is/go-passwd"
"golang.org/x/crypto/bcrypt"
)
var All = []passwd.Passwder{
&Blowfish{},
&MD5{},
}
type MD5 struct{}
func (p *MD5) Passwd(pass string, check string) (string, error) {
h := md5.New()
fmt.Fprint(h, pass)
hash := fmt.Sprintf("$1$%x", h.Sum(nil))
return hashCheck(hash, check)
}
func (p *MD5) ApplyPasswd(passwd *passwd.Passwd) {
passwd.Register("1", p)
}
type Blowfish struct{}
func (p *Blowfish) Passwd(pass string, check string) (string, error) {
if check == "" {
b, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(b), nil
}
err := bcrypt.CompareHashAndPassword([]byte(check), []byte(pass))
if err != nil {
return "", err
}
return check, nil
}
func (p *Blowfish) ApplyPasswd(passwd *passwd.Passwd) {
passwd.Register("2a", p)
}
// type SHA256 struct{}
// func (p *SHA256) Passwd(pass string, check string) (string, error) {
// h := sha256.New()
// fmt.Fprint(h, pass)
// hash := fmt.Sprintf("$5$%x", h.Sum(nil))
// return hashCheck(hash, check)
// }
// func (p *SHA256) ApplyPasswd(passwd *passwd.Passwd) {
// passwd.Register("5", p)
// }
// type SHA512 struct{}
// func (p *SHA512) Passwd(pass string, check string) (string, error) {
// h := sha512.New()
// fmt.Fprint(h, pass)
// hash := fmt.Sprintf("$6$%x", h.Sum(nil))
// return hashCheck(hash, check)
// }
// func (p *SHA512) ApplyPasswd(passwd *passwd.Passwd) {
// passwd.Register("6", p)
// }
func hashCheck(hash, check string) (string, error) {
if check == "" {
return hash, nil
}
if subtle.ConstantTimeCompare([]byte(hash), []byte(check)) == 1 {
return hash, nil
}
return hash, passwd.ErrNoMatch
}

42
pkg/unix/unix_test.go Normal file
View File

@@ -0,0 +1,42 @@
package unix_test
import (
"fmt"
"testing"
"github.com/matryer/is"
"github.com/sour-is/go-passwd"
"github.com/sour-is/go-passwd/pkg/unix"
)
func TestPasswdHash(t *testing.T) {
type testCase struct {
pass, hash string
}
tests := []testCase{
{"passwd", "$1$76a2173be6393254e72ffa4d6df1030a"},
{"passwd", "$2a$10$GkJwB.nOaaeAvRGgyl2TI.kruM8e.iIo.OozgdslegpNlC/vIFKRq"},
}
is := is.New(t)
// Generate additional test cases for each algo.
for _, algo := range unix.All {
hash, err := algo.Passwd("passwd", "")
is.NoErr(err)
tests = append(tests, testCase{"passwd", hash})
}
pass := passwd.New(unix.All...)
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.Equal(hash, tt.hash)
is.NoErr(err)
})
}
}