<>Situation:
我想从<代码>st
www.un.org/Depts/DGACM/index_spanish.htm 我所尝试的内容:。
我使用<条码>扫描。 阅读代码>,但与类型一致。
<>Situation:
我想从<代码>st
www.un.org/Depts/DGACM/index_spanish.htm 我所尝试的内容:。
我使用<条码>扫描。 阅读代码>,但与类型一致。
您可以通过在密码读取后再读stty -echo <<>/code>来回避,然后读到
sttyrep
。
The following is one of best ways to get it done.
First get term
package by go get golang.org/x/term
package main
import (
"bufio"
"fmt"
"os"
"strings"
"syscall"
"golang.org/x/term"
)
func main() {
username, password, _ := credentials()
fmt.Printf("Username: %s, Password: %s
", username, password)
}
func credentials() (string, string, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Username: ")
username, err := reader.ReadString(
)
if err != nil {
return "", "", err
}
fmt.Print("Enter Password: ")
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", "", err
}
password := string(bytePassword)
return strings.TrimSpace(username), strings.TrimSpace(password), nil
}
仅看一看一只新鲜花邮件。 有些人写了很简单的一揽子选择供使用。 您可在此查阅:。
如同:
package main
import "fmt"
import "github.com/howeyc/gopass"
func main() {
fmt.Printf("Password: ")
pass := gopass.GetPasswd()
// Do something with pass
}
自Go ~v1.11以来 有一个正式包裹<>条码>:golang.org/x/term,以取代已折旧的<代码>加密/ssh/terminal。 除其他外,它有以下职能: 代码。
例常使用:
package main
import (
"fmt"
"os"
"syscall"
"golang.org/x/term"
)
func main() {
fmt.Print("Password: ")
bytepw, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
os.Exit(1)
}
pass := string(bytepw)
fmt.Printf("
You ve entered: %q
", pass)
}
我也有类似的使用案例,以下的法典对我来说是好的。 如果你仍坐在这里,会自由尝试。
import (
"fmt"
"golang.org/x/crypto/ssh/terminal"
)
func main() {
fmt.Printf("Now, please type in the password (mandatory): ")
password, _ := terminal.ReadPassword(0)
fmt.Printf("Password is : %s", password)
}
当然,你需要事先安装使用<条码>的终端包裹。
这里是我利用你可能认为有用的Go1.6.2制定的解决办法。
它只使用以下标准包:bufio
,fmt
,os
strings and sysbet
。 更具体地说,它使用<代码>systation.ForkExec()和sysbet.Wait4()
, 仅以可拆卸/可撤销的终端呼呼号。
我已经测试了它。 它不会在窗户上工作。
// getPassword - Prompt for password. Use stty to disable echoing.
import ( "bufio"; "fmt"; "os"; "strings"; "syscall" )
func getPassword(prompt string) string {
fmt.Print(prompt)
// Common settings and variables for both stty calls.
attrs := syscall.ProcAttr{
Dir: "",
Env: []string{},
Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()},
Sys: nil}
var ws syscall.WaitStatus
// Disable echoing.
pid, err := syscall.ForkExec(
"/bin/stty",
[]string{"stty", "-echo"},
&attrs)
if err != nil {
panic(err)
}
// Wait for the stty process to complete.
_, err = syscall.Wait4(pid, &ws, 0, nil)
if err != nil {
panic(err)
}
// Echo is disabled, now grab the data.
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString(
)
if err != nil {
panic(err)
}
// Re-enable echo.
pid, err = syscall.ForkExec(
"/bin/stty",
[]string{"stty", "echo"},
&attrs)
if err != nil {
panic(err)
}
// Wait for the stty process to complete.
_, err = syscall.Wait4(pid, &ws, 0, nil)
if err != nil {
panic(err)
}
return strings.TrimSpace(text)
}
要求通过Go ForkExec()功能进行发射:
package main
import (
os "os"
bufio "bufio"
fmt "fmt"
str "strings"
)
func main() {
fmt.Println();
if passwd, err := Getpasswd("Enter password: "); err == nil {
fmt.Printf("
Password: %s
",passwd)
}
}
func Getpasswd(prompt string) (passwd string, err os.Error) {
fmt.Print(prompt);
const stty_arg0 = "/bin/stty";
stty_argv_e_off := []string{"stty","-echo"};
stty_argv_e_on := []string{"stty","echo"};
const exec_cwdir = "";
fd := []*os.File{os.Stdin,os.Stdout,os.Stderr};
pid, err := os.ForkExec(stty_arg0,stty_argv_e_off,nil,exec_cwdir,fd);
if err != nil {
return passwd, os.NewError(fmt.Sprintf("Failed turning off console echo for password entry:
%s",err))
}
rd := bufio.NewReader(os.Stdin);
os.Wait(pid,0);
line, err := rd.ReadString(
);
if err == nil {
passwd = str.TrimSpace(line)
} else {
err = os.NewError(fmt.Sprintf("Failed during password entry: %s",err))
}
pid, e := os.ForkExec(stty_arg0,stty_argv_e_on,nil,exec_cwdir,fd);
if e == nil {
os.Wait(pid,0)
} else if err == nil {
err = os.NewError(fmt.Sprintf("Failed turning on console echo post password entry:
%s",e))
}
return passwd, err
}
此处是专门针对含水层的版本:
func terminalEcho(show bool) {
// Enable or disable echoing terminal input. This is useful specifically for
// when users enter passwords.
// calling terminalEcho(true) turns on echoing (normal mode)
// calling terminalEcho(false) hides terminal input.
var termios = &syscall.Termios{}
var fd = os.Stdout.Fd()
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd,
syscall.TCGETS, uintptr(unsafe.Pointer(termios))); err != 0 {
return
}
if show {
termios.Lflag |= syscall.ECHO
} else {
termios.Lflag &^= syscall.ECHO
}
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd,
uintptr(syscall.TCSETS),
uintptr(unsafe.Pointer(termios))); err != 0 {
return
}
}
因此使用:
fmt.Print("password: ")
terminalEcho(false)
var pw string
fmt.Scanln(&pw)
terminalEcho(true)
fmt.Println("")
它是具体针对TCGETS的。 职业会计和视窗有不同的传承价值。
也可使用<条码>PasswordPrompt,。 一揽子计划。
在打字之前转折,然后在打字之后再回头。
如果没有第三个图书馆,你就能够找到与它做事的方法。 但是,它很难在Windows上。
可以通过以下方法做到这一点:SetConsoleMode
,有窗户kernel32.dll<>/code>,其中提到从 :C: • 如何在窗户群中消除重复?
func GetPassword(prompt string) (err error, text string) {
var modeOn, modeOff uint32
stdin := syscall.Handle(os.Stdin.Fd())
err = syscall.GetConsoleMode(stdin, &modeOn)
if err != nil {
return
}
modeOff = modeOn &^ 0x0004
proc := syscall.MustLoadDLL("kernel32").MustFindProc("SetConsoleMode")
fmt.Print(prompt)
_, _, _ = proc.Call(uintptr(stdin), uintptr(modeOff))
_, err = fmt.Scanln(&text)
if err != nil {
return
}
_, _, _ = proc.Call(uintptr(stdin), uintptr(modeOn))
fmt.Println()
return nil, strings.TrimSpace(text)
}
How to encrypt SALTEDHASHED password (used by Weblogic) in java? I need to be able to hash passwords in exactly the same way as WebLogic s authenticator does. Preferably without using WebLogic s ...
I would like to allow my users to use Unicode for their passwords. However I see a lot of sites don t support that (e.g. Gmail, Hotmail). So I m wondering if there s some technical or usability ...
I have two classes: a Generator class and a SystemManagement class. The Generator class, I can generate a password. The SystemManagement class imports Generator (from another package) and includes ...
In C, I want to display every single character that the user type as * (Ex, Please type in your password: *****) I m searching around but can t be able to find a solution for this. I m working on ...
Is it considered good or bad practice to use MySQL s password function to hash passwords used by an application? I can see pros and cons. I m curious if there is a general consensus on whether it is ...
So I have this black box authentication method, handed down to me from the accounts people, which basically amounts to ldap_bind($connection, $username, $password). But of course, I want my users to ...
I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...
I ve got an HTML-only website (with a bit of inconsequential javascript). The static pages are generated from a photo-blogging program that I created. There are hundreds of pages and thousands of ...