Skip to content

Commit d771e97

Browse files
authored
Don't use custom PBKDF2 function (#382)
1 parent 73710c0 commit d771e97

File tree

4 files changed

+85
-42
lines changed

4 files changed

+85
-42
lines changed

models/user.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/Unknwon/com"
2626
"github.com/go-xorm/xorm"
2727
"github.com/nfnt/resize"
28+
"golang.org/x/crypto/pbkdf2"
2829

2930
"code.gitea.io/git"
3031
api "code.gitea.io/sdk/gitea"
@@ -361,7 +362,7 @@ func (u *User) NewGitSig() *git.Signature {
361362

362363
// EncodePasswd encodes password to safe format.
363364
func (u *User) EncodePasswd() {
364-
newPasswd := base.PBKDF2([]byte(u.Passwd), []byte(u.Salt), 10000, 50, sha256.New)
365+
newPasswd := pbkdf2.Key([]byte(u.Passwd), []byte(u.Salt), 10000, 50, sha256.New)
365366
u.Passwd = fmt.Sprintf("%x", newPasswd)
366367
}
367368

modules/base/tool.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
package base
66

77
import (
8-
"crypto/hmac"
98
"crypto/md5"
109
"crypto/rand"
1110
"crypto/sha1"
1211
"encoding/base64"
1312
"encoding/hex"
1413
"fmt"
15-
"hash"
1614
"html/template"
1715
"math"
1816
"net/http"
@@ -97,45 +95,6 @@ func GetRandomString(n int, alphabets ...byte) string {
9795
return string(bytes)
9896
}
9997

100-
// PBKDF2 http://code.google.com/p/go/source/browse/pbkdf2/pbkdf2.go?repo=crypto
101-
// FIXME: use https://godoc.org/golang.org/x/crypto/pbkdf2?
102-
func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte {
103-
prf := hmac.New(h, password)
104-
hashLen := prf.Size()
105-
numBlocks := (keyLen + hashLen - 1) / hashLen
106-
107-
var buf [4]byte
108-
dk := make([]byte, 0, numBlocks*hashLen)
109-
U := make([]byte, hashLen)
110-
for block := 1; block <= numBlocks; block++ {
111-
// N.B.: || means concatenation, ^ means XOR
112-
// for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter
113-
// U_1 = PRF(password, salt || uint(i))
114-
prf.Reset()
115-
prf.Write(salt)
116-
buf[0] = byte(block >> 24)
117-
buf[1] = byte(block >> 16)
118-
buf[2] = byte(block >> 8)
119-
buf[3] = byte(block)
120-
prf.Write(buf[:4])
121-
dk = prf.Sum(dk)
122-
T := dk[len(dk)-hashLen:]
123-
copy(U, T)
124-
125-
// U_n = PRF(password, U_(n-1))
126-
for n := 2; n <= iter; n++ {
127-
prf.Reset()
128-
prf.Write(U)
129-
U = U[:0]
130-
U = prf.Sum(U)
131-
for x := range U {
132-
T[x] ^= U[x]
133-
}
134-
}
135-
}
136-
return dk[:keyLen]
137-
}
138-
13998
// VerifyTimeLimitCode verify time limit code
14099
func VerifyTimeLimitCode(data string, minutes int, code string) bool {
141100
if len(code) <= 18 {

vendor/golang.org/x/crypto/pbkdf2/pbkdf2.go

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/vendor.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,12 @@
872872
"revision": "9477e0b78b9ac3d0b03822fd95422e2fe07627cd",
873873
"revisionTime": "2016-10-31T15:37:30Z"
874874
},
875+
{
876+
"checksumSHA1": "1MGpGDQqnUoRpv7VEcQrXOBydXE=",
877+
"path": "golang.org/x/crypto/pbkdf2",
878+
"revision": "8e06e8ddd9629eb88639aba897641bff8031f1d3",
879+
"revisionTime": "2016-09-10T18:59:01Z"
880+
},
875881
{
876882
"checksumSHA1": "LlElMHeTC34ng8eHzjvtUhAgrr8=",
877883
"path": "golang.org/x/crypto/ssh",

0 commit comments

Comments
 (0)