-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchPassphrase_linux.go
78 lines (65 loc) · 1.28 KB
/
fetchPassphrase_linux.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
//go:build linux
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
"syscall"
"time"
"golang.org/x/term"
)
func fetchPassphrase() (passphrase []byte) {
// See if we have a passphrase on stdin already
cn := make(chan []byte)
fd := int(os.Stdin.Fd())
syscall.SetNonblock(fd, true)
go func() {
sc := bufio.NewReader(os.Stdin)
s, e := sc.ReadString('\n')
if e != nil {
return
}
// remove newline
if strings.HasSuffix(s, "\n") {
s = strings.TrimSuffix(s, "\n")
}
cn <- []byte(s)
close(cn)
}()
// if passphrase found on stdin, return it
select {
case passphrase = <-cn:
syscall.SetNonblock(fd, false)
return passphrase
case <-time.After(time.Millisecond):
syscall.SetNonblock(fd, false)
}
close(cn)
if cliOpt.nopp {
// Expected passphrase sent over stdin
fmt.Println()
os.Exit(1)
}
fmt.Printf("passphrase: ")
p1, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
panic(err)
}
fmt.Println("") // move curser to newline
if cliOpt.generate {
fmt.Printf("confirm: ")
p2, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
panic(err)
}
fmt.Println("") // move curser to newline
if !bytes.Equal(p1, p2) {
fmt.Printf("passphrase mismatch\n")
os.Exit(1)
}
}
passphrase = p1
return passphrase
}