Linuxシステムプログラミング29システムデータファイルと情報-パスワード検証の例

mhr @ ubuntu:〜/ work / linux / muluheyonghucaozuo / 29 $ gedit / etc / shadow
mhr @ ubuntu:〜/ work / linux / muluheyonghucaozuo / 29 $ su-
パスワード:
root @ ubuntu:〜#
root @ ubuntu:〜#
root @ubuntu:〜#gedit / etc / shadow

...
mhr:$1$xxxxxxx$tYJRN8GLdveK/y47nATsA1:18142:0:99999:7:::
...

$1$ : 前两个 $之间的数字,表示加密方式
$xxxxxxx$ :第二个和第三个 $之间的字符串是杂字串,即原串或上该杂字串 经过上述加密方式之后 会得到后面的串。
$tYJRN8GLdveK/y47nATsA1 :原串或上杂字串 经过上述加密方式之后 会得到后面该串。

NAMEの
getspnam - GETシャドウパスワードファイルのエントリ

書式
/ *一般的なシャドウパスワードファイルのAPI * /
書式#include <shadow.h>

   struct spwd *getspnam(const char *name);

通过一个 user name 获取 他在shadow文件中对应的一行内容,即和 用root 权限 查看  /etc/shadow 中的内容一致。


       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 */
       };

NAME
crypt、crypt_r-パスワードとデータの暗号化

書式
の#define _XOPEN_SOURCE / * feature_test_macros(7)参照* /
書式#include <unistd.h>

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

-lcryptとリンクします。

戻り値
成功すると、暗号化されたパスワードへのポインタが返されます。エラーの場合、NULLが返されます。

NOTES
   Glibc notes
       The  glibc2  version  of  this  function supports additional encryption
       algorithms.

   If salt is a character string starting with the characters "$id$"  fol‐
   lowed by a string terminated by "$":

          $id$salt$encrypted

   就是 即指定ID 加密方式,又指定杂字串。


          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)

実験

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

int main(int argc, char* argv[])
{
	char *input_pass;
	struct spwd *shdowline;
	char *crypted_passl;
		

	if(argc < 2)
	{
		fprintf(stderr,"Usage ... \n");
		exit(1);
	}

	//不加回显的获取密码
	input_pass = getpass("PassWord:");
	
	//通过一个 user name 获取 他在shadow文件中对应的一行内容
	shdowline = getspnam(argv[1]);

	crypted_passl = crypt(input_pass,shdowline->sp_pwdp);

	if(strcmp(shdowline->sp_pwdp,crypted_passl))	
		puts("ok");
	else
		puts("failed");

	exit(0);
	
}

おすすめ

転載: blog.csdn.net/LinuxArmbiggod/article/details/106038597