-
Notifications
You must be signed in to change notification settings - Fork 4
/
passphrase.go
62 lines (51 loc) · 1.71 KB
/
passphrase.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
package crypto
import (
"fmt"
"io/ioutil"
"math/rand"
"strings"
"github.com/ethereum/go-ethereum/console"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,_"
// PromptPassphrase prompts the user for a passphrase. Set confirmation to true
// to require the user to confirm the passphrase.
func PromptPassphrase(confirmation bool) (string, error) {
passphrase, err := console.Stdin.PromptPassword("Passphrase: ")
if err != nil {
return "", fmt.Errorf("Failed to read passphrase: %v", err)
}
if confirmation {
confirm, err := console.Stdin.PromptPassword("Repeat passphrase: ")
if err != nil {
return "", fmt.Errorf("Failed to read passphrase confirmation: %v", err)
}
if passphrase != confirm {
return "", fmt.Errorf("Passphrases do not match")
}
}
return passphrase, nil
}
// GetPassphrase obtains a passphrase given by the user. It first checks the
// --passfile command line flag and ultimately prompts the user for a
// passphrase.
func GetPassphrase(passwordFile string, confirmation bool) (string, error) {
// Look for the --passfile flag.
if passwordFile != "" {
content, err := ioutil.ReadFile(passwordFile)
if err != nil {
return "", fmt.Errorf("Failed to read passphrase file '%s': %v", passwordFile, err)
}
return strings.TrimRight(string(content), "\r\n"), nil
}
// Otherwise prompt the user for the passphrase.
return PromptPassphrase(confirmation)
}
//RandomPassphrase generates a random passphrase
func RandomPassphrase(n int) string {
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,_"
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}