github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/lib/others/sys/hidepass_windows.go (about)

     1  package sys
     2  
     3  import (
     4  	"os"
     5  	"fmt"
     6  )
     7  
     8  // New method (requires msvcrt.dll):
     9  import (
    10  	"syscall"
    11  )
    12  
    13  var (
    14  	msvcrt = syscall.NewLazyDLL("msvcrt.dll")
    15  	_getch = msvcrt.NewProc("_getch")
    16  )
    17  
    18  func getch() int {
    19  	res, _, _ := syscall.Syscall(_getch.Addr(), 0, 0, 0, 0)
    20  	return int(res)
    21  }
    22  
    23  func enterpassext(b []byte) (n int) {
    24  	for {
    25  		chr := byte(getch())
    26  		if chr==3 {
    27  			// Ctrl+C
    28  			ClearBuffer(b)
    29  			os.Exit(0)
    30  		}
    31  		if chr==13 || chr==10 {
    32  			fmt.Println()
    33  			break // Enter
    34  		}
    35  		if chr=='\b' {
    36  			if n>0 {
    37  				n--
    38  				b[n] = 0
    39  				fmt.Print("\b \b")
    40  			} else {
    41  				fmt.Print("\007")
    42  			}
    43  			continue
    44  		}
    45  		if chr<' ' {
    46  			fmt.Print("\007")
    47  			fmt.Println("\n", chr)
    48  			continue
    49  		}
    50  		if n==len(b) {
    51  			fmt.Print("\007")
    52  			continue
    53  		}
    54  		fmt.Print("*")
    55  		b[n] = chr
    56  		n++
    57  	}
    58  	return
    59  }
    60  
    61  func init() {
    62  	er := _getch.Find()
    63  	if er != nil {
    64  		println(er.Error())
    65  		println("WARNING: Characters will be visible during password input.")
    66  		return
    67  	}
    68  
    69  	secrespass = enterpassext
    70  }
    71  
    72  
    73  /*
    74  Old method (requires mingw):
    75  
    76  #include <conio.h>
    77  // end the comment here when enablign this method
    78  import "C"
    79  
    80  func getch() int {
    81  	return int(C._getch())
    82  }
    83  
    84  */