/
sha512.go
163 lines (140 loc) · 4.17 KB
/
sha512.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Package sha512 implements the SHA-512 hashing algorithm for crypt(3).
package sha512
import (
"crypto"
_ "crypto/sha512"
"crypto/subtle"
"strconv"
"github.com/sergeymakinen/go-crypt"
crypthash "github.com/sergeymakinen/go-crypt/hash"
"github.com/sergeymakinen/go-crypt/internal/hashutil"
"github.com/sergeymakinen/go-crypt/sha256/sha2crypt"
)
const (
MaxSaltLength = 16
DefaultSaltLength = MaxSaltLength
)
// InvalidSaltLengthError values describe errors resulting from an invalid length of a salt.
type InvalidSaltLengthError int
func (e InvalidSaltLengthError) Error() string {
return "invalid salt length " + strconv.FormatInt(int64(e), 10)
}
// InvalidSaltError values describe errors resulting from an invalid character in a hash string.
type InvalidSaltError byte
func (e InvalidSaltError) Error() string {
return "invalid character " + strconv.QuoteRuneToASCII(rune(e)) + " in salt"
}
const (
MinRounds = 1000
MaxRounds = 999999999
DefaultRounds = 656000
ImplicitRounds = 5000 // the value if the rounds parameter is omitted from the hash string
)
// InvalidRoundsError values describe errors resulting from an invalid round count.
type InvalidRoundsError uint32
func (e InvalidRoundsError) Error() string {
return "invalid round count " + strconv.FormatUint(uint64(e), 10)
}
// Permutation table for final digest.
var permFinal = [64]byte{
42, 21, 0, 1,
43, 22, 23, 2,
44, 45, 24, 3,
4, 46, 25, 26,
5, 47, 48, 27,
6, 7, 49, 28,
29, 8, 50, 51,
30, 9, 10, 52,
31, 32, 11, 53,
54, 33, 12, 13,
55, 34, 35, 14,
56, 57, 36, 15,
16, 58, 37, 38,
17, 59, 60, 39,
18, 19, 61, 40,
41, 20, 62, 63,
}
// Key returns a SHA-512 key derived from the password, salt and rounds.
func Key(password, salt []byte, rounds uint32) ([]byte, error) {
if n := len(salt); n > MaxSaltLength {
return nil, InvalidSaltLengthError(n)
}
if i := hashutil.HashEncoding.IndexAnyInvalid(salt); i >= 0 {
return nil, InvalidSaltError(salt[i])
}
if rounds < MinRounds || rounds > MaxRounds {
return nil, InvalidRoundsError(rounds)
}
return sha2crypt.Encrypt(crypto.SHA512, password, salt, rounds, permFinal[:])
}
const Prefix = "$6$"
// UnsupportedPrefixError values describe errors resulting from an unsupported prefix string.
type UnsupportedPrefixError string
func (e UnsupportedPrefixError) Error() string {
return "unsupported prefix " + strconv.Quote(string(e))
}
type hashPrefix string
func (h *hashPrefix) UnmarshalText(text []byte) error {
if s := string(text); s != Prefix {
return UnsupportedPrefixError(s)
}
*h = Prefix
return nil
}
const sumLength = 86
type scheme struct {
HashPrefix hashPrefix
Rounds uint32 `hash:"param:rounds,omitempty"`
Salt []byte
Sum [sumLength]byte
}
// NewHash returns the crypt(3) SHA-512 hash of the password with the given rounds.
func NewHash(password string, rounds uint32) (string, error) {
scheme := scheme{
HashPrefix: Prefix,
Rounds: rounds,
Salt: hashutil.HashEncoding.Rand(DefaultSaltLength),
}
key, err := Key([]byte(password), scheme.Salt, scheme.Rounds)
if err != nil {
return "", err
}
crypthash.LittleEndianEncoding.Encode(scheme.Sum[:], key)
return crypthash.Marshal(scheme)
}
// Params returns the hashing salt and rounds used to create
// the given crypt(3) SHA-512 hash.
func Params(hash string) (salt []byte, rounds uint32, err error) {
var scheme scheme
if err = crypthash.Unmarshal(hash, &scheme); err != nil {
return
}
if scheme.Rounds == 0 {
scheme.Rounds = ImplicitRounds
}
return scheme.Salt, scheme.Rounds, nil
}
// Check compares the given crypt(3) SHA-512 hash with a new hash derived from the password.
// Returns nil on success, or an error on failure.
func Check(hash, password string) error {
var scheme scheme
if err := crypthash.Unmarshal(hash, &scheme); err != nil {
return err
}
if scheme.Rounds == 0 {
scheme.Rounds = ImplicitRounds
}
key, err := Key([]byte(password), scheme.Salt, scheme.Rounds)
if err != nil {
return err
}
var b [sumLength]byte
crypthash.LittleEndianEncoding.Encode(b[:], key)
if subtle.ConstantTimeCompare(b[:], scheme.Sum[:]) == 0 {
return crypt.ErrPasswordMismatch
}
return nil
}
func init() {
crypt.RegisterHash(Prefix, Check)
}