-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbkdf2.go
55 lines (43 loc) · 1.19 KB
/
pbkdf2.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
// 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 (
"crypto/sha512"
"fmt"
"golang.org/x/crypto/pbkdf2"
)
const (
defaultPBKDF2iterations = 10000
pbkdf2s = "PBKDF2"
pbkdf2Format = "%s(%d-SHA512)"
)
var defaultPBKDF2Hash = sha512.New
type pbkdf2KSF struct {
iterations int
}
func pbkdf2New() keyStretchingFunction {
return &pbkdf2KSF{
iterations: defaultPBKDF2iterations,
}
}
func (p *pbkdf2KSF) Harden(password, salt []byte, length int) []byte {
return pbkdf2.Key(password, salt, p.iterations, length, defaultPBKDF2Hash)
}
// Parameterize replaces the functions parameters with the new ones. Must match the amount of parameters.
func (p *pbkdf2KSF) Parameterize(parameters ...int) {
if len(parameters) != 1 {
panic(errParams)
}
p.iterations = parameters[0]
}
func (p *pbkdf2KSF) String() string {
return fmt.Sprintf(pbkdf2Format, pbkdf2s, p.iterations)
}
func (p *pbkdf2KSF) Params() []int {
return []int{p.iterations}
}