speakeasy_windows.go 864 B

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