-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrypt.go
66 lines (53 loc) · 1.27 KB
/
scrypt.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
// 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/scrypt"
)
const (
scrypts = "Scrypt"
scryptFormat = "%s(%d-%d-%d)"
)
var (
defaultScryptn = 32768
defaultScryptr = 8
defaultScryptp = 1
)
type scryptKSF struct {
n, r, p int
}
func scryptKSFNew() keyStretchingFunction {
return &scryptKSF{
n: defaultScryptn,
r: defaultScryptr,
p: defaultScryptp,
}
}
func (s *scryptKSF) Harden(password, salt []byte, length int) []byte {
k, err := scrypt.Key(password, salt, s.n, s.r, s.p, length)
if err != nil {
panic(fmt.Errorf("unexpected error : %w", err))
}
return k
}
// Parameterize replaces the functions parameters with the new ones. Must match the amount of parameters.
func (s *scryptKSF) Parameterize(parameters ...int) {
if len(parameters) != 3 {
panic(errParams)
}
s.n = parameters[0]
s.r = parameters[1]
s.p = parameters[2]
}
func (s *scryptKSF) String() string {
return fmt.Sprintf(scryptFormat, scrypts, s.n, s.r, s.p)
}
func (s *scryptKSF) Params() []int {
return []int{s.n, s.r, s.p}
}