c - getpass 函数的更好替代方案

标签 c

今天我正在编写一些代码,在某个时候我需要 getpass 函数,一切都很好,至少在我使用 --std=c11 之前我得到了:

error: implicit declaration of function ‘getpass’ [-Werror=implicit-function-declaration]|

在互联网上我发现这个功能被认为是过时的。:

The getpass() function is not threadsafe because it manipulates global signal state.

The getpass() function is scheduled to be withdrawn from a future version of the X/Open CAE Specification.

所以我不能使用它,至少如果我用 --std=c11 编译的话是这样。

我试过的程序是这样的:

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>

    int main(void){
        char checkPasswd[BUFSIZ] = "iajsdiajsda32121312asda:ubuntu";
        char *checkName = "michi";

        char name[25];
        char *passwd;
        char *delimiter;

        printf("Login Name:\t");
        scanf("%24s" ,name);

        delimiter = strchr(checkPasswd, ':');
        delimiter[0] = '\0';
        delimiter++;

        passwd = getpass("Give a passwd:\t");

        if(strcmp(name,checkName) == 0){
             if(strcmp(passwd,delimiter) == 0){
                printf("Logged In\n\n");
             }else{
                printf("\t\t\tWrong Password\n");
                exit(1);
             }
        }else{
            printf("\t\t\tWrong Name\n");
            exit(1);
        }

        printf("\t\t\tWelcome %s\n\n", name);
        return 0;
    }

我在这里读了一些东西==>> Getting a password in C without using getpass (3)?还有一些答案,但我想知道在我尝试在那里找到的东西之前是否有简单的方法,类似于:

passwd = getpass("Give a passwd:\t");

或者我必须自己创建它?

这个程序没有做我需要的:

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

int main(void){
    struct termios oflags, nflags;
    char password[64];

    tcgetattr(fileno(stdin), &oflags);
    nflags = oflags;
    nflags.c_lflag &= ~ECHO;
    nflags.c_lflag |= ECHONL;

    if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
        perror("tcsetattr");
        return EXIT_FAILURE;
    }

    printf("password: ");
    fgets(password, sizeof(password), stdin);
    password[strlen(password) - 1] = 0;
    printf("you typed '%s'\n", password);

    if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
        perror("tcsetattr");
        return EXIT_FAILURE;
    }

    return 0;
}

所以问题是,我必须自己写还是有替代函数?

最佳答案

如果您的终端支持这些转义码,这将隐藏您输入的密码。

#include <stdio.h>

void UserPW ( char *pw, size_t pwsize) {
    int i = 0;
    int ch = 0;

    printf ( "\033[8m");//conceal typing
    while ( 1) {
        ch = getchar();
        if ( ch == '\r' || ch == '\n' || ch == EOF) {//get characters until CR or NL
            break;
        }
        if ( i < pwsize - 1) {//do not save pw longer than space in pw
            pw[i] = ch;       //longer pw can be entered but excess is ignored
            pw[i + 1] = '\0';
        }
        i++;
    }
    printf ( "\033[0A");//move cursor up one line
    printf ( "\033[21C");//move cursor 21 places
    while ( i) {
        printf ( "*");//overwrite password on screen. this is still concealed
        i--;
    }
    printf ( "\033[28m");//reveal typing
}

int main ( ) {
    char password[20];

    printf ( "Enter your password: ");
    fflush ( stdout);//prompt does not have '\n' so make sure it prints
    UserPW ( password, sizeof ( password));//password array and size
    printf ( "\nentered [%s]\n", password);//instead of printing you would verify the entered password
    return 0;
}

关于c - getpass 函数的更好替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32396188/

相关文章:

c - 换行符在格式字符串中时 scanf 的行为

c - 为什么即使文件存在并且正确打开和关闭, fprintf() 也不打印任何内容?

c++ - 错误:在给定的命令集中从 ‘int’到 ‘void*’ [-fpermissive]的无效转换

用于 header 封装的 C++ 包装器

c - 引用地址?

C类型转换成结构

c++ - 是否可以在进程中的所有线程上设置 pthread_sigmask?

c - 逻辑运算符和增量运算符

C编程,如何防止puts写入控制台?

c - stat st_mode 始终等于 16877