forked from dciangot/dodas-IAMClientRec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
input_windows.go
109 lines (80 loc) · 2.63 KB
/
input_windows.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"bytes"
"errors"
"fmt"
"os"
"syscall"
"golang.org/x/sys/windows"
"github.com/awnumar/memguard"
"github.com/gookit/color"
"github.com/rs/zerolog/log"
)
var (
errPasswordMismatch = errors.New("The two password inserted are not the same.")
)
// passwordReader is an io.Reader that reads from a specific file descriptor.
type passwordReader int
func readPassword(fd int) (*os.File, error) {
var st uint32
if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
return nil, err
}
old := st
st &^= (windows.ENABLE_ECHO_INPUT | windows.ENABLE_LINE_INPUT)
st |= (windows.ENABLE_PROCESSED_OUTPUT | windows.ENABLE_PROCESSED_INPUT)
if err := windows.SetConsoleMode(windows.Handle(fd), st); err != nil {
return nil, err
}
defer windows.SetConsoleMode(windows.Handle(fd), old)
var h windows.Handle
p, _ := windows.GetCurrentProcess()
if err := windows.DuplicateHandle(p, windows.Handle(fd), p, &h, 0, false, windows.DUPLICATE_SAME_ACCESS); err != nil {
return nil, err
}
f := os.NewFile(uintptr(h), "stdin")
return f, nil
}
func (t *GetInputWrapper) GetPassword(question string, only4Decription bool) (password *memguard.Enclave, err error) {
fmt.Print(question)
readPasswdFd, errCreateReader := readPassword(int(syscall.Stdin))
if errCreateReader != nil {
return nil, fmt.Errorf("get password %w", errCreateReader)
}
defer readPasswdFd.Close()
passEnclave, errEclBuf := memguard.NewBufferFromReaderUntil(readPasswdFd, '\n')
if errEclBuf != nil {
return nil, fmt.Errorf("get password enclave %w", errEclBuf)
}
for passEnclave.Size() == 0 {
readPasswdFd.Close()
fmt.Printf("\n%s Sorry, but an empty password is not allowed...\n", color.Red.Sprint("[X]==>"))
fmt.Print(question)
readPasswdFd, errCreateReader = readPassword(int(syscall.Stdin))
if errCreateReader != nil {
return nil, fmt.Errorf("get password %w", errCreateReader)
}
passEnclave, errEclBuf = memguard.NewBufferFromReaderUntil(readPasswdFd, '\n')
if errEclBuf != nil {
return nil, fmt.Errorf("get password enclave %w", errEclBuf)
}
}
fmt.Println()
if only4Decription {
password = passEnclave.Seal()
return password, nil
}
passMsg := fmt.Sprintf("%s Please, insert the password again: ", color.Yellow.Sprint("==>"))
fmt.Print(passMsg)
passEnclave2, err := memguard.NewBufferFromReaderUntil(readPasswdFd, '\n')
if err != nil {
return nil, fmt.Errorf("get password check %w", err)
}
fmt.Println()
if bytes.Equal(passEnclave.Bytes(), passEnclave2.Bytes()) {
password = passEnclave.Seal()
return password, nil
}
log.Err(errPasswordMismatch).Msg("GetPassword")
return nil, errPasswordMismatch
}