-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassgen.go
79 lines (60 loc) · 1.33 KB
/
passgen.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
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"bufio"
"code.google.com/p/go.crypto/pbkdf2"
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/atotto/clipboard"
"github.com/howeyc/gopass"
"os"
)
const (
pbkdf2Rounds int = 100000
outputBytes int = 15
)
func main() {
masterHash := getMasterHash()
for {
id := getId()
if len(id) == 0 {
break
}
password := generatePassword(masterHash, id)
clipboard.WriteAll(base64.StdEncoding.EncodeToString(password))
fmt.Printf("Password copied to clipboard.\n")
clear(password)
}
clear(masterHash)
}
func getMasterHash() []byte {
hasher := sha256.New()
fmt.Printf("Master password: ")
hasher.Write(gopass.GetPasswd())
masterHash := hasher.Sum(nil)
fmt.Printf("Checksum: -> " + base64.StdEncoding.EncodeToString(masterHash[:3]) + " <-\n")
return masterHash
}
func getId() []byte {
bio := bufio.NewReader(os.Stdin)
fmt.Printf("Get password for ID (empty to quit): ")
id, err := bio.ReadBytes('\n')
check(err)
return id[:len(id)-1] // remove '\n' from the end
}
func generatePassword(masterHash, id []byte) []byte {
hasher := sha256.New()
hasher.Write(id)
idHash := hasher.Sum(nil)
return pbkdf2.Key(masterHash, idHash, pbkdf2Rounds, outputBytes, sha256.New)
}
func check(e error) {
if e != nil {
panic(e)
}
}
func clear(b []byte) {
for i := 0; i < len(b); i++ {
b[i] = 0
}
}