隐藏终端上的密码输入 [英] Hide password input on terminal

查看:29
本文介绍了隐藏终端上的密码输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用 * 编写密码时屏蔽我的密码.我将 Linux GCC 用于此代码.我知道一种解决方案是使用这样的 getch() 函数

#include int main(){字符 c,密码[10];国际我;while( (c=getch())!= '
');{密码[i] = c;printf("*");我++;}返回 1;}

但问题是 GCC 不包含 conio.h 文件,所以 getch() 对我来说没用.有人有解决办法吗?

解决方案

在 Linux 世界中,屏蔽通常不使用星号完成,通常只是关闭回显并且终端显示空白.如果您使用 su 或登录虚拟终端等

有一个库函数来处理获取密码,它不会用星号掩盖密码,但会禁用密码回显到终端.我从我拥有的一本 linux 书中提取了这个.我相信它是 posix 标准的一部分

<块引用>

#include char *getpass(const char *prompt);/*返回指向静态分配的输入密码字符串的指针成功时,或错误时为 NULL*/

getpass() 函数首先禁用回显和所有处理终端特殊字符(如中断字符,通常控制-C).

然后打印提示指向的字符串,并读取一行输入,返回带有尾随的以空字符结尾的输入字符串删除换行符,作为其函数结果.

谷歌搜索 getpass() 引用了 GNU 实现(应该在大多数 linux 发行版中)和一些示例代码,用于在需要时实现您自己的

http://www.gnu.org/s/hello/手册/libc/getpass.html

他们自己动手的例子:

#include #include ssize_tmy_getpass (char **lineptr, size_t *n, FILE *stream){struct termios 旧的,新的;int nread;/* 如果我们不能,请关闭回显并失败.*/if (tcgetattr (fileno (stream), &old) != 0)返回-1;新 = 旧;new.c_lflag &= ~ECHO;if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0)返回-1;/* 读取密码.*/nread = getline (lineptr, n, stream);/* 恢复终端.*/(void) tcsetattr (fileno (stream), TCSAFLUSH, &old);返回 nread;}

如果需要,您可以以此为基础修改它以显示星号.

I want to mask my password while writing it with *. I use Linux GCC for this code. I know one solution is to use getch() function like this

#include <conio.h>   
int main()
{
    char c,password[10];
    int i;
    while( (c=getch())!= '
');{
        password[i] = c;
        printf("*");
        i++;
    }
    return 1;
}

but the problem is that GCC does not include conio.h file so, getch() is useless for me. Does anyone have a solution?

解决方案

In the Linux world, masking isn't usually done with asterisks, normally echoing is just turned off and the terminal displays blanks E.g. if you use su or log into a virtual terminal etc.

There is a library function to handle getting passwords, it won't mask the password with asterisks but will disable echoing of the password to terminal. I pulled this out of a linux book I have. I believe its part of the posix standard

#include <unistd.h>
char *getpass(const char *prompt);

/*Returns pointer to statically allocated input password string
on success, or NULL on error*/

The getpass() function first disables echoing and all processing of terminal special characters (such as the interrupt character, normally Control-C).

It then prints the string pointed to by prompt, and reads a line of input, returning the null-terminated input string with the trailing newline stripped, as its function result.

A google search for getpass() has a reference to the GNU implementation (should be in most linux distros) and some sample code for implementing your own if need be

http://www.gnu.org/s/hello/manual/libc/getpass.html

Their example for rolling your own:

#include <termios.h>
#include <stdio.h>

ssize_t
my_getpass (char **lineptr, size_t *n, FILE *stream)
{
    struct termios old, new;
    int nread;

    /* Turn echoing off and fail if we can't. */
    if (tcgetattr (fileno (stream), &old) != 0)
        return -1;
    new = old;
    new.c_lflag &= ~ECHO;
    if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0)
        return -1;

    /* Read the password. */
    nread = getline (lineptr, n, stream);

    /* Restore terminal. */
    (void) tcsetattr (fileno (stream), TCSAFLUSH, &old);

    return nread;
}

If need be you could use this as the basis as modify it to display asterisks.

这篇关于隐藏终端上的密码输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆