Linux密码校验过程

密码存储在/etc/shadow,是被加密了的密码。

得不到原本的密码,只能得到加密的密码。

struct spwd *getspnam(const char *name);

得到一个包含有加密密码的结构体。

char *getpass(const char *s)

返回值是输入的密码,传参内容可以是任意值。

char *crypt(const char *key, const char *salt)

可以把密码计算成加密的密码。

#define _XOPEN_SOURCE       /* See feature_test_macros(7) */
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    int i;
    struct spwd *spw = NULL;
    char *pass = NULL;
    char *cry_key = NULL;

    if(argc < 2)
        exit(1);
    spw = getspnam(argv[1]);
    if(NULL == spw)
    {
        perror("getspnam()");
        exit(1);
    }
    pass = getpass("passwd:");
    if(NULL == pass)
    {
        perror("getpass:");
        exit(1);
    }
    cry_key = crypt(pass, spw->sp_pwdp);
    if(NULL == cry_key)
    {
        perror("crypt");
        exit(1);
    }
    if(strcmp(cry_key, spw->sp_pwdp) == 0)
        printf("验证正确\n");
    else
        printf("验证错误\n");

    exit(0);

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值