forked from andrew-d/go-termutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getpass_windows.go
78 lines (63 loc) · 1.49 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// +build windows
package termutil
import (
"syscall"
"io"
)
var (
f_putwch uintptr // wint_t _putwch(wchar_t c)
f_getwch uintptr // wint_t _getwch(void)
)
func init() {
msvcrt := loadLibrary("msvcrt.dll")
f_putwch = getProcAddress(msvcrt, "_putwch")
f_getwch = getProcAddress(msvcrt, "_getwch")
}
func loadLibrary(name string) uintptr {
lib, err := syscall.LoadLibrary(name)
if err != nil {
panic(err)
}
return uintptr(lib)
}
func getProcAddress(library uintptr, name string) uintptr {
addr, err := syscall.GetProcAddress(syscall.Handle(library), name)
if err != nil {
panic(err)
}
return uintptr(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 == 0x0010 || ret == 0x0013 {
break
} 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
}