-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
54 lines (45 loc) · 1.53 KB
/
app.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
package app
import (
"crypto/sha512"
"encoding/base64"
"github.com/phrozen/password-hash-exercise/internal/store"
)
// 512 bits / 8 = 64 bytes * 8 / 6 = 86
// plus padding for a multiple of 4 = 88
const HASH_LENGTH = 88
// App (application) implements the core business logic based on requirements
// which is to save hashed passwords and retrieve them later.
type App struct {
store store.Store
}
// New creates an application with the given Store implementation
func New(s store.Store) *App {
return &App{store: s}
}
// GetHash returns the hash at the given id from the Store
func (app *App) GetHash(id int) (string, error) {
hash, err := app.store.Get(id)
return string(hash), err
}
// SetHash receives a password to be hashed with SHA512 algorithm and
// then converted to base64 encoding and saved to the Store, returns
// the id where the hash is/will be saved.
func (app *App) SetHash(password string) (int, error) {
hash := app.hash([]byte(password))
return app.store.Set(hash)
}
// Close runs all tear down operations like closing the Store
func (app *App) Close() error {
return app.store.Close()
}
// hashes any input with SHA512 and encodes to standard base64
// returned []byte has ALWAYS 88 bytes length and should never fail
func (app *App) hash(input []byte) []byte {
hash := sha512.Sum512(input)
// Deterministic buffer size will improve
// performance, if slow, try sync.Pool
output := make([]byte, HASH_LENGTH)
// convert fixed size array 'hash' by slicing it
base64.StdEncoding.Encode(output, hash[:])
return output
}