Skip to content

Commit 6ee57e3

Browse files
author
eau
committed
cleanups
1 parent ef61f76 commit 6ee57e3

File tree

10 files changed

+78
-59
lines changed

10 files changed

+78
-59
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ while encrypting new password and changing old ones can use the new "profile".
109109
110110
# Changelog
111111
112-
* v0.1.0: initial release
112+
* v0.1.2: fix hash parsing.
113113
* v0.1.1: fix /issues/1
114+
* v0.1.0: initial release
114115
115116
# Status
116117

argon.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ type Argon2Params struct {
8989
Version int
9090
Time uint32
9191
Memory uint32
92-
Thread uint8
9392
Saltlen uint32
9493
Keylen uint32
94+
Thread uint8
95+
Masked bool // are parameters private
9596
// unexported
96-
salt []byte // on compare only..
97-
Masked bool // are parameters private
97+
salt []byte // on compare only..
9898
}
9999

100100
// [0] password: 'prout' hashed: '$2id$aiOE.rPFUFkkehxc6utWY.$1$65536$8$32$Wv1IMP6xwaqVaQGOX6Oxe.eSEbozeRJLzln8ZlthZfS'
@@ -107,14 +107,16 @@ func newArgon2ParamsFromFields(fields []string) (*Argon2Params, error) {
107107
// salt
108108
salt, err := base64Decode([]byte(fields[0])) // process the salt
109109
if err != nil {
110-
return nil, err
110+
fmt.Printf("b64 decode error: %v\n", err)
111+
return nil, ErrParse
111112
}
112113
saltlen := uint32(len(salt))
113114

114115
// ARGON FIELD: ["mezIC/cmChATxAfFFe9ele" "2" "65536" "8" "32" "omYy81uRZcZv6JkbH17wA0s1CSpH4UQttXBB42oKMXK"]
115116
timeint, err := strconv.ParseInt(fields[1], 10, 32)
116117
if err != nil {
117-
return nil, err
118+
fmt.Printf("invalid time decode error: %v\n", err)
119+
return nil, ErrParse
118120
}
119121
time := uint32(timeint)
120122

base64.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import "encoding/base64"
1414

1515
const alphabet = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
1616

17-
var bcEncoding = base64.NewEncoding(alphabet)
18-
1917
func base64Encode(src []byte) []byte {
18+
bcEncoding := base64.NewEncoding(alphabet)
2019
n := bcEncoding.EncodedLen(len(src))
2120
dst := make([]byte, n)
2221
bcEncoding.Encode(dst, src)
@@ -32,6 +31,7 @@ func base64Decode(src []byte) ([]byte, error) {
3231
src = append(src, '=')
3332
}
3433

34+
bcEncoding := base64.NewEncoding(alphabet)
3535
dst := make([]byte, bcEncoding.DecodedLen(len(src)))
3636
n, err := bcEncoding.Decode(dst, src)
3737
if err != nil {

cmd/pcrypt/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ func main() {
4444
switch {
4545
case len(*checkFlag) > 0:
4646
for idx, passwordStr := range argv {
47-
fmt.Printf("[%d] is '%s' the passwd? %v\n", idx, passwordStr, passwd.Compare([]byte(*checkFlag), []byte(passwordStr)))
47+
fmt.Printf("[%d] is '%s' the passwd? %v\n",
48+
idx,
49+
passwordStr,
50+
passwd.Compare([]byte(*checkFlag), []byte(passwordStr)),
51+
)
4852
}
4953
default:
5054
for idx, passwordStr := range argv {

error.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ package passwd
55
// also avoid your error checks can be diverted cross packages
66
// when in usage in the rest of an package ecosystem
77

8+
// Error is the type helping defining errors as constants.
89
type Error string
910

1011
func (e Error) Error() string { return string(e) }
1112

1213
const (
13-
ErrParse = Error("parse error")
14+
// ErrParse when a parse error happened
15+
ErrParse = Error("parse error")
16+
// ErrUnsupported when a feature is not supported
1417
ErrUnsupported = Error("unsupported")
15-
ErrMismatch = Error("mismatch")
16-
ErrUnsafe = Error("unsafe parameters")
18+
// ErrMismatch is returned when Compare() call does not match
19+
ErrMismatch = Error("mismatch")
20+
// ErrValidate is to validate password hashing parameters strength
21+
ErrUnsafe = Error("unsafe parameters")
1722
)

go.mod

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module git.sr.ht/~eau/passwd
33
go 1.12
44

55
require (
6-
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a
7-
golang.org/x/text v0.3.0
6+
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443
7+
golang.org/x/sys v0.0.0-20190621203818-d432491b9138 // indirect
8+
golang.org/x/text v0.3.2
89
)

go.sum

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a h1:Igim7XhdOpBnWPuYJ70XcNpq8q3BCACtVgNfoJxOV7g=
2-
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
3-
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e h1:nFYrTHrdrAOpShe27kaFHjsqYSEQ0KWqdWLu3xuZJts=
4-
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2+
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443 h1:IcSOAf4PyMp3U3XbIEj1/xJ2BjNN2jWv7JoyOsMxXUU=
3+
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
4+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
5+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
6+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7+
golang.org/x/sys v0.0.0-20190621203818-d432491b9138 h1:t8BZD9RDjkm9/h7yYN6kE8oaeov5r9aztkB7zKA5Tkg=
8+
golang.org/x/sys v0.0.0-20190621203818-d432491b9138/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
59
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
10+
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
11+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
12+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

parse.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func parseFromHashToParams(hashed []byte) (interface{}, error) {
4141
sp, err := newScryptParamsFromFields(fields[1:]) // mismatch.
4242
if err != nil {
4343
// XXX wrapp the error
44-
return nil, ErrParse
44+
return nil, err
4545
}
4646
return *sp, nil
4747
case idArgon2i:
@@ -51,9 +51,8 @@ func parseFromHashToParams(hashed []byte) (interface{}, error) {
5151
ap, err := newArgon2ParamsFromFields(fields[1:]) // mismatch.
5252
if err != nil {
5353
// XXX wrapp the error
54-
return nil, ErrParse
54+
return nil, err
5555
}
56-
//return ap.Compare(hashed, password)
5756
return *ap, nil
5857
}
5958
return nil, ErrParse
@@ -74,7 +73,7 @@ func parseFromHashToSalt(hashed []byte) ([]byte, error) {
7473
case idArgon2id:
7574
salt, err := base64Decode([]byte(fields[1])) // process the salt
7675
if err != nil {
77-
return nil, ErrParse
76+
return nil, err
7877
}
7978
return salt, nil
8079
}

passwd.go

+36-36
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ const (
5252
ScryptParanoid
5353
BcryptDefault
5454
BcryptParanoid
55-
Argon2Custom
56-
ScryptCustom
57-
BcryptCustom
55+
Argon2Custom // value for custom
56+
ScryptCustom // value for custom
57+
BcryptCustom // value for custom
5858
)
5959

6060
var (
@@ -78,7 +78,7 @@ type Profile struct {
7878
params interface{} // parameters
7979
}
8080

81-
// New instanciate a new Profile
81+
// New instantiate a new Profile
8282
func New(profile HashProfile) (*Profile, error) {
8383
var p Profile
8484

@@ -95,39 +95,10 @@ func New(profile HashProfile) (*Profile, error) {
9595
return nil, ErrUnsupported
9696
}
9797

98-
// NewCustom instanciates a new Profile using user defined hash parameters
99-
func NewCustom(params interface{}) (*Profile, error) {
100-
var p Profile
101-
102-
switch v := params.(type) {
103-
case BcryptParams:
104-
p = Profile{
105-
t: BcryptCustom,
106-
params: v,
107-
}
108-
return &p, nil
109-
case ScryptParams:
110-
p = Profile{
111-
t: ScryptCustom,
112-
params: v,
113-
}
114-
return &p, nil
115-
case Argon2Params:
116-
p = Profile{
117-
t: Argon2Custom,
118-
params: v,
119-
}
120-
return &p, nil
121-
}
122-
123-
return nil, ErrUnsupported
124-
}
125-
12698
// NewMasked instanciates a new masked Profile.
12799
// "masked" translate to the fact that no hash parameters will be provided in
128100
// the resulting hash.
129101
func NewMasked(profile HashProfile) (*Profile, error) {
130-
131102
switch profile {
132103
case Argon2idDefault, Argon2idParanoid, ScryptDefault, ScryptParanoid, BcryptDefault, BcryptParanoid:
133104
var p Profile
@@ -161,6 +132,34 @@ func NewMasked(profile HashProfile) (*Profile, error) {
161132
return nil, ErrUnsupported
162133
}
163134

135+
// NewCustom instanciates a new Profile using user defined hash parameters
136+
func NewCustom(params interface{}) (*Profile, error) {
137+
var p Profile
138+
139+
switch v := params.(type) {
140+
case BcryptParams:
141+
p = Profile{
142+
t: BcryptCustom,
143+
params: v,
144+
}
145+
return &p, nil
146+
case ScryptParams:
147+
p = Profile{
148+
t: ScryptCustom,
149+
params: v,
150+
}
151+
return &p, nil
152+
case Argon2Params:
153+
p = Profile{
154+
t: Argon2Custom,
155+
params: v,
156+
}
157+
return &p, nil
158+
}
159+
160+
return nil, ErrUnsupported
161+
}
162+
164163
// Hash is the Profile method for computing the hash value
165164
// respective of the selected profile.
166165
// it takes the plaintext password to hash and output its hashed value
@@ -187,8 +186,8 @@ func (p *Profile) Hash(password []byte) ([]byte, error) {
187186
func (p *Profile) Compare(hashed, password []byte) error {
188187
salt, err := parseFromHashToSalt(hashed)
189188
if err != nil {
190-
fmt.Printf("error: %v\n", err)
191-
return err
189+
fmt.Printf("compare parse error: %v\n", err)
190+
return ErrMismatch
192191
}
193192

194193
switch v := p.params.(type) {
@@ -220,7 +219,8 @@ func Compare(hashed, password []byte) error {
220219

221220
params, err := parseFromHashToParams(hashed)
222221
if err != nil {
223-
return err
222+
fmt.Printf("compare parse error: %v\n", err)
223+
return ErrMismatch
224224
}
225225

226226
//fmt.Printf("PARAM TYPE: %T\n", params)

scrypt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ type ScryptParams struct {
8080
Saltlen uint32 // 128 bits min.
8181
Keylen uint32 // 128 bits min.
8282
// unexported
83-
salt []byte // my salt..
8483
Masked bool // are parameters private
84+
salt []byte // my salt..
8585
}
8686

8787
func newScryptParamsFromFields(fields []string) (*ScryptParams, error) {

0 commit comments

Comments
 (0)