-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssha.go
45 lines (36 loc) · 873 Bytes
/
ssha.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
// SSHA password
package main
import (
"crypto/rand"
"crypto/sha1"
"crypto/subtle"
"encoding/base64"
"fmt"
)
func generateSSHA(password string) string {
salt := make([]byte, 8)
rand.Read(salt)
hash := createSSHAHash(password, salt)
return fmt.Sprintf("{SSHA}%s", base64.StdEncoding.EncodeToString(hash))
}
func validateSSHA(password string, hash string) bool {
if len(hash) < 7 || hash[:6] != "{SSHA}" {
return false
}
data, err := base64.StdEncoding.DecodeString(hash[6:])
if len(data) < 21 || err != nil {
return false
}
newHash := createSSHAHash(password, data[20:])
if subtle.ConstantTimeCompare(newHash, data) == 1 {
return true
}
return false
}
func createSSHAHash(password string, salt []byte) []byte {
pass := []byte(password)
str := append(pass, salt...)
sum := sha1.Sum(str)
result := append(sum[:], salt...)
return result
}