Skip to content

Commit 5c49be7

Browse files
committed
Move MD5Entry type to tests only where it belongs.
It's not part of the package public interface. Also fixes a number of golint errors in md5crypt.go
1 parent 67689a9 commit 5c49be7

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

md5crypt.go

+3-22
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
11
package auth
22

33
import "crypto/md5"
4-
import "strings"
54

65
const itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
76

8-
var md5_crypt_swaps = [16]int{12, 6, 0, 13, 7, 1, 14, 8, 2, 15, 9, 3, 5, 10, 4, 11}
7+
var md5CryptSwaps = [16]int{12, 6, 0, 13, 7, 1, 14, 8, 2, 15, 9, 3, 5, 10, 4, 11}
98

10-
type MD5Entry struct {
11-
Magic, Salt, Hash []byte
12-
}
13-
14-
func NewMD5Entry(e string) *MD5Entry {
15-
parts := strings.SplitN(e, "$", 4)
16-
if len(parts) != 4 {
17-
return nil
18-
}
19-
return &MD5Entry{
20-
Magic: []byte("$" + parts[1] + "$"),
21-
Salt: []byte(parts[2]),
22-
Hash: []byte(parts[3]),
23-
}
24-
}
25-
26-
/*
27-
MD5 password crypt implementation
28-
*/
9+
// MD5Crypt is the MD5 password crypt implementation.
2910
func MD5Crypt(password, salt, magic []byte) []byte {
3011
d := md5.New()
3112

@@ -79,7 +60,7 @@ func MD5Crypt(password, salt, magic []byte) []byte {
7960
result := make([]byte, 0, 22)
8061
v := uint(0)
8162
bits := uint(0)
82-
for _, i := range md5_crypt_swaps {
63+
for _, i := range md5CryptSwaps {
8364
v |= (uint(final[i]) << bits)
8465
for bits = bits + 8; bits > 6; bits -= 6 {
8566
result = append(result, itoa64[v&0x3f])

md5crypt_test.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
package auth
22

3-
import "testing"
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
type md5entry struct {
9+
Magic, Salt, Hash []byte
10+
}
11+
12+
func newEntry(e string) *md5entry {
13+
parts := strings.SplitN(e, "$", 4)
14+
if len(parts) != 4 {
15+
return nil
16+
}
17+
return &md5entry{
18+
Magic: []byte("$" + parts[1] + "$"),
19+
Salt: []byte(parts[2]),
20+
Hash: []byte(parts[3]),
21+
}
22+
}
423

524
func Test_MD5Crypt(t *testing.T) {
625
t.Parallel()
@@ -10,7 +29,7 @@ func Test_MD5Crypt(t *testing.T) {
1029
{"topsecret", "$apr1$JI4wh3am$AmhephVqLTUyAVpFQeHZC0"},
1130
}
1231
for _, tc := range test_cases {
13-
e := NewMD5Entry(tc[1])
32+
e := newEntry(tc[1])
1433
result := MD5Crypt([]byte(tc[0]), e.Salt, e.Magic)
1534
if string(result) != tc[1] {
1635
t.Fatalf("MD5Crypt returned '%s' instead of '%s'", string(result), tc[1])

0 commit comments

Comments
 (0)