-
Notifications
You must be signed in to change notification settings - Fork 5
/
getpass_windows.go
59 lines (49 loc) · 1.18 KB
/
getpass_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
// +build windows
package termutil
import (
"syscall"
"io"
"errors"
)
var (
f_getwch uintptr // wint_t _getwch(void)
)
func init() {
f_getwch = syscall.MustLoadDLL("msvcrt.dll").MustFindProc("_getwch").Addr()
}
func GetPass(prompt string, prompt_fd, input_fd uintptr) ([]byte, error) {
// Firstly, print the prompt.
written := 0
buf := []byte(prompt)
for written < len(prompt) {
n, err := syscall.Write(syscall.Handle(prompt_fd), buf[written:])
if err != nil {
return nil, err
}
if n == 0 {
return nil, io.EOF
}
written += n
}
// Write a newline after we're done, since it won't be echoed when the
// user presses 'Enter'.
defer syscall.Write(syscall.Handle(prompt_fd), []byte("\r\n"))
// Read the characters
var chars []uint16
for {
ret, _, _ := syscall.Syscall(f_getwch, 0, 0, 0, 0)
if ret == 0x000A || ret == 0x000D {
break
} else if ret == 0x0003 {
return nil, errors.New("Input has been interrupted by user.")
} else if ret == 0x0008 {
chars = chars[0:len(chars)-2]
} else {
chars = append(chars, uint16(ret))
}
}
// Convert to string...
s := syscall.UTF16ToString(chars)
// ... and back to UTF-8 bytes.
return []byte(s), nil
}