English 中文(简体)
• 在戈马获得绕过功能?
原标题:getpasswd functionality in Go?

<>Situation:

我想从<代码>st进入密码。 console - , 但不重复用户类型。 是否在戈马有与<条码>的植被/代码”的功能相类似?

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
}

http://play.golang.org/p/l-9IP1mrhA

我也有类似的使用案例,以下的法典对我来说是好的。 如果你仍坐在这里,会自由尝试。

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的。 职业会计和视窗有不同的传承价值。





相关问题
Encrypting SALTEDHASHED weblogic password in java

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 ...

Should I support Unicode in passwords?

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 ...

Why does my method return a null password?

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 ...

How to mask password in c?

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 ...

MySQL password function

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 ...

How should I store a user s LDAP password in a cookie?

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 ...

Simple JAVA: Password Verifier problem

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 ...

Password protect a simple web site

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 ...