forked from firebase/firebase-admin-go
/
hash.go
260 lines (229 loc) · 7.87 KB
/
hash.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package hash contains a collection of password hash algorithms that can be used with the
// auth.ImportUsers() API. Refer to https://firebase.google.com/docs/auth/admin/import-users for
// more details about supported hash algorithms.
package hash // import "firebase.google.com/go/auth/hash"
import (
"encoding/base64"
"errors"
"fmt"
"firebase.google.com/go/internal"
)
// Bcrypt represents the BCRYPT hash algorithm.
//
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_bcrypt_hashed_passwords
// for more details.
type Bcrypt struct{}
// Config returns the validated hash configuration.
func (b Bcrypt) Config() (internal.HashConfig, error) {
return internal.HashConfig{"hashAlgorithm": "BCRYPT"}, nil
}
// StandardScrypt represents the standard scrypt hash algorithm.
//
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_standard_scrypt_hashed_passwords
// for more details.
type StandardScrypt struct {
BlockSize int
DerivedKeyLength int
MemoryCost int
Parallelization int
}
// Config returns the validated hash configuration.
func (s StandardScrypt) Config() (internal.HashConfig, error) {
return internal.HashConfig{
"hashAlgorithm": "STANDARD_SCRYPT",
"dkLen": s.DerivedKeyLength,
"blockSize": s.BlockSize,
"parallelization": s.Parallelization,
"memoryCost": s.MemoryCost,
}, nil
}
// Scrypt represents the scrypt hash algorithm.
//
// This is the modified scrypt used by Firebase Auth (https://github.com/firebase/scrypt).
// Rounds must be between 1 and 8, and the MemoryCost must be between 1 and 14. Key is required.
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_firebase_scrypt_hashed_passwords
// for more details.
type Scrypt struct {
Key []byte
SaltSeparator []byte
Rounds int
MemoryCost int
}
// Config returns the validated hash configuration.
func (s Scrypt) Config() (internal.HashConfig, error) {
if len(s.Key) == 0 {
return nil, errors.New("signer key not specified")
}
if s.Rounds < 1 || s.Rounds > 8 {
return nil, errors.New("rounds must be between 1 and 8")
}
if s.MemoryCost < 1 || s.MemoryCost > 14 {
return nil, errors.New("memory cost must be between 1 and 14")
}
return internal.HashConfig{
"hashAlgorithm": "SCRYPT",
"signerKey": base64.RawURLEncoding.EncodeToString(s.Key),
"saltSeparator": base64.RawURLEncoding.EncodeToString(s.SaltSeparator),
"rounds": s.Rounds,
"memoryCost": s.MemoryCost,
}, nil
}
// HMACMD5 represents the HMAC SHA512 hash algorithm.
//
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_hmac_hashed_passwords
// for more details. Key is required.
type HMACMD5 struct {
Key []byte
}
// Config returns the validated hash configuration.
func (h HMACMD5) Config() (internal.HashConfig, error) {
return hmacConfig("HMAC_MD5", h.Key)
}
// HMACSHA1 represents the HMAC SHA512 hash algorithm.
//
// Key is required.
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_hmac_hashed_passwords
// for more details.
type HMACSHA1 struct {
Key []byte
}
// Config returns the validated hash configuration.
func (h HMACSHA1) Config() (internal.HashConfig, error) {
return hmacConfig("HMAC_SHA1", h.Key)
}
// HMACSHA256 represents the HMAC SHA512 hash algorithm.
//
// Key is required.
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_hmac_hashed_passwords
// for more details.
type HMACSHA256 struct {
Key []byte
}
// Config returns the validated hash configuration.
func (h HMACSHA256) Config() (internal.HashConfig, error) {
return hmacConfig("HMAC_SHA256", h.Key)
}
// HMACSHA512 represents the HMAC SHA512 hash algorithm.
//
// Key is required.
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_hmac_hashed_passwords
// for more details.
type HMACSHA512 struct {
Key []byte
}
// Config returns the validated hash configuration.
func (h HMACSHA512) Config() (internal.HashConfig, error) {
return hmacConfig("HMAC_SHA512", h.Key)
}
// MD5 represents the MD5 hash algorithm.
//
// Rounds must be between 0 and 8192.
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
// for more details.
type MD5 struct {
Rounds int
}
// Config returns the validated hash configuration.
func (h MD5) Config() (internal.HashConfig, error) {
return basicConfig("MD5", h.Rounds)
}
// PBKDF2SHA256 represents the PBKDF2SHA256 hash algorithm.
//
// Rounds must be between 0 and 120000.
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
// for more details.
type PBKDF2SHA256 struct {
Rounds int
}
// Config returns the validated hash configuration.
func (h PBKDF2SHA256) Config() (internal.HashConfig, error) {
return basicConfig("PBKDF2_SHA256", h.Rounds)
}
// PBKDFSHA1 represents the PBKDFSHA1 hash algorithm.
//
// Rounds must be between 0 and 120000.
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
// for more details.
type PBKDFSHA1 struct {
Rounds int
}
// Config returns the validated hash configuration.
func (h PBKDFSHA1) Config() (internal.HashConfig, error) {
return basicConfig("PBKDF_SHA1", h.Rounds)
}
// SHA1 represents the SHA1 hash algorithm.
//
// Rounds must be between 1 and 8192.
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
// for more details.
type SHA1 struct {
Rounds int
}
// Config returns the validated hash configuration.
func (h SHA1) Config() (internal.HashConfig, error) {
return basicConfig("SHA1", h.Rounds)
}
// SHA256 represents the SHA256 hash algorithm.
//
// Rounds must be between 1 and 8192.
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
// for more details.
type SHA256 struct {
Rounds int
}
// Config returns the validated hash configuration.
func (h SHA256) Config() (internal.HashConfig, error) {
return basicConfig("SHA256", h.Rounds)
}
// SHA512 represents the SHA512 hash algorithm.
//
// Rounds must be between 1 and 8192.
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
// for more details.
type SHA512 struct {
Rounds int
}
// Config returns the validated hash configuration.
func (h SHA512) Config() (internal.HashConfig, error) {
return basicConfig("SHA512", h.Rounds)
}
func hmacConfig(name string, key []byte) (internal.HashConfig, error) {
if len(key) == 0 {
return nil, errors.New("signer key not specified")
}
return internal.HashConfig{
"hashAlgorithm": name,
"signerKey": base64.RawURLEncoding.EncodeToString(key),
}, nil
}
func basicConfig(name string, rounds int) (internal.HashConfig, error) {
minRounds := 0
maxRounds := 120000
switch name {
case "MD5":
maxRounds = 8192
case "SHA1", "SHA256", "SHA512":
minRounds = 1
maxRounds = 8192
}
if rounds < minRounds || maxRounds < rounds {
return nil, fmt.Errorf("rounds must be between %d and %d", minRounds, maxRounds)
}
return internal.HashConfig{
"hashAlgorithm": name,
"rounds": rounds,
}, nil
}