forked from 1Password/srp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdf.go
63 lines (51 loc) · 1.55 KB
/
kdf.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
package srp
import (
"crypto/sha1" // #nosec See docs for KDFRFC5054 for warnings.
"math/big"
"strings"
"unicode"
"golang.org/x/text/unicode/norm"
)
/*
* Best to use KDF from github/agilebits/op/crypto
* I will import at some point
*/
/*
KDFRFC5054 is *not* recommended. Instead use a key derivation function (KDF) that
involves a hashing scheme designed for password hashing.
The SRP verifier that is stored by the server is like
a password hash with respect to crackability. Choose a KDF
that that makes the server stored verifiers hard to crack.
This computes the client's long term secret, x
from a username, password, and salt as described
in RFC 5054 §2.6, which says
x = SHA1(s | SHA1(I | ":" | P))
*/
func KDFRFC5054(salt []byte, username string, password string) (x *big.Int) {
p := []byte(PreparePassword(password))
u := []byte(PreparePassword(username))
innerHasher := sha1.New() // #nosec
innerHasher.Write(u)
innerHasher.Write([]byte(":"))
innerHasher.Write(p)
ih := innerHasher.Sum(nil)
oHasher := sha1.New() // #nosec
oHasher.Write(salt)
oHasher.Write(ih)
h := oHasher.Sum(nil)
x = bigIntFromBytes(h)
return x
}
// PreparePassword strips leading and trailing white space
// and normalizes to unicode NFKD
func PreparePassword(s string) string {
var out string
out = string(norm.NFKD.Bytes([]byte(s)))
out = strings.TrimLeftFunc(out, unicode.IsSpace)
out = strings.TrimRightFunc(out, unicode.IsSpace)
return out
}
/**
** Copyright 2017 AgileBits, Inc.
** Licensed under the Apache License, Version 2.0 (the "License").
**/