forked from lonng/nanoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.go
38 lines (30 loc) · 870 Bytes
/
password.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
package algoutil
import (
"bytes"
"crypto/sha1"
"encoding/base64"
"fmt"
"strings"
"github.com/pborman/uuid"
)
func passwordHash(pwd, salt string) string {
buf := &bytes.Buffer{}
fmt.Fprintf(buf, "%s%x%s", salt, pwd, salt)
result1 := sha1.Sum(buf.Bytes())
buf.Reset()
fmt.Fprintf(buf, "%s%s%x%s%s", pwd, salt, result1, salt, pwd)
result2 := sha1.Sum(buf.Bytes())
buf.Reset()
fmt.Fprintf(buf, "%x", result2)
return base64.StdEncoding.EncodeToString(buf.Bytes())
}
// PasswordHash accept password and generate with uuid as salt
// FORMAT: sha1.Sum(pwd + salt + sha1.Sum(salt + pwd + salt) + salt + pwd)
func PasswordHash(pwd string) (hash, salt string) {
salt = strings.Replace(uuid.New(), "-", "", -1)
hash = passwordHash(pwd, salt)
return hash, salt
}
func VerifyPassword(pwd, salt, hash string) bool {
return passwordHash(pwd, salt) == hash
}