speakeasy_windows.go 890 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // +build windows
  2. package speakeasy
  3. import (
  4. "os"
  5. "syscall"
  6. )
  7. // SetConsoleMode function can be used to change value of ENABLE_ECHO_INPUT:
  8. // http://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx
  9. const ENABLE_ECHO_INPUT = 0x0004
  10. func getPassword() (password string, err error) {
  11. hStdin := syscall.Handle(os.Stdin.Fd())
  12. var oldMode uint32
  13. err = syscall.GetConsoleMode(hStdin, &oldMode)
  14. if err != nil {
  15. return
  16. }
  17. var newMode uint32 = (oldMode &^ ENABLE_ECHO_INPUT)
  18. err = setConsoleMode(hStdin, newMode)
  19. defer setConsoleMode(hStdin, oldMode)
  20. if err != nil {
  21. return
  22. }
  23. return readline()
  24. }
  25. func setConsoleMode(console syscall.Handle, mode uint32) (err error) {
  26. dll := syscall.MustLoadDLL("kernel32")
  27. proc := dll.MustFindProc("SetConsoleMode")
  28. r, _, err := proc.Call(uintptr(console), uintptr(mode))
  29. if r == 0 {
  30. return err
  31. }
  32. return nil
  33. }