-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcrypt.go
57 lines (45 loc) · 1.09 KB
/
bcrypt.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
// SPDX-License-Identifier: MIT
//
// Copyright (C) 2020 Daniel Bourdrez. All Rights Reserved.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree or at
// https://spdx.org/licenses/MIT.html
package ksf
import (
"fmt"
"golang.org/x/crypto/bcrypt"
)
const (
bcryptFormat = "%s(%d)"
bcrypts = "Bcrypt"
defaultBcryptCost = 10
)
type bcryptKSF struct {
time int
}
func bcryptNew() keyStretchingFunction {
return &bcryptKSF{
time: defaultBcryptCost,
}
}
func (b *bcryptKSF) Harden(password, _ []byte, _ int) []byte {
h, err := bcrypt.GenerateFromPassword(password, b.time)
if err != nil {
panic(err)
}
return h
}
// Parameterize replaces the functions parameters with the new ones. Must match the amount of parameters.
func (b *bcryptKSF) Parameterize(parameters ...int) {
if len(parameters) != 1 {
panic(errParams)
}
b.time = parameters[0]
}
func (b *bcryptKSF) String() string {
return fmt.Sprintf(bcryptFormat, bcrypts, b.time)
}
func (b *bcryptKSF) Params() []int {
return []int{b.time}
}