1.getspnam函数
#include <shadow.h>
struct spwd *getspnam(const char *name);
struct spwd {
char *sp_namp; /* Login name */
char *sp_pwdp; /* Encrypted password */
long sp_lstchg; /* Date of last change
(measured in days since
1970-01-01 00:00:00 +0000 (UTC)) */
long sp_min; /* Min # of days between changes */
long sp_max; /* Max # of days between changes */
long sp_warn; /* # of days before password expires
to warn user to change it */
long sp_inact; /* # of days after password expires
until account is disabled */
long sp_expire; /* Date when account expires
(measured in days since
1970-01-01 00:00:00 +0000 (UTC)) */
unsigned long sp_flag; /* Reserved */
};
2.crypt函数
原串 和 杂质串 返回一个加密串
#define _XOPEN_SOURCE /* See feature_test_macros(7) */
#include <unistd.h>
char *crypt(const char *key, const char *salt);
//原串 和 杂质串 返回一个加密串
Link with -lcrypt
①cc -D _POSIX_SOURCE file.c
②#define _XOPEN_SOURCE
决不是简单的宏定义
它是使程序符合系统环境的不可缺少的部分
If salt is a character string starting with the characters "$id$"
followed by a string terminated by "$":
$id$salt$encrypted
then instead of using the DES machine, id identifies the
encryption method used and this then determines how the rest of
the password string is interpreted.
The following values of id are supported:
ID | Method
─────────────────────────────────────────────────────────
1 | MD5
2a | Blowfish (not in mainline glibc; added in some
| Linux distributions)
5 | SHA-256 (since glibc 2.7)
6 | SHA-512 (since glibc 2.7)
既指定ID又指定字符串,公用一个参数
3.getpass函数
NAME
getpass - get a password
SYNOPSIS
#include <unistd.h>
char *getpass( const char *prompt);
关闭终端的回显功能,需要自己去实现这个prompt终端提示,返回你输入的密码
4.一个demo
#include <stdio.h>
#include <stdlib.h>
#include <shadow.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char *passwdInput;//保存 用户输入的字符串的地址
struct spwd * UserInfo;//保存用户信息
char *cryptedPassword;//保存经过加密的字符串
if(argc < 2)
{
fprintf(stderr, "No Enugh argc\n");
exit(0);
}
//得到用户输入的字符串 不回显 提示符
passwdInput = getpass("Password: ");
if(NULL == passwdInput)
{
perror("getpass :\n");
exit(1);
}
//获取用户信息
UserInfo = getspnam(argv[1]);
if(NULL == UserInfo)
{
perror("getspnam :\n");
exit(1);
}
//对获得的密码进行加密 sp_pwdp里面的格式 我们只会取到前两个$
cryptedPassword = crypt(passwdInput, UserInfo->sp_pwdp);
if(0 == strcmp(cryptedPassword, UserInfo->sp_pwdp))
{
printf("OK!\n");
}
else
{
printf("Failed\n");
}
return 0;
}
必须以root权限执行,root就是神一样的用户