在现在的linux和unix系统中,用户的密码都保存在shadow文件中,因为密码关系到系统的安全,所以只有root用户才有读shadow文件的权限。shadow中存放的内容是有着一定的格式的,看如下例子:

root:$1$v2wT9rQF$XSpGgoB93STC4EFSlgpjg1:14181:0:99999:7:::

冒号是分割符,分别代表着,每个字段分别代表着:

:用户名

:密码hash值

:密码修改距离1970年1月1日的时间

:密码将被允许修改之前的天数(0 表示“可在任何时间修改”)

:系统将强制用户修改为新密码之前的天数(1 表示“永远都不能修改”)

:密码过期之前,用户将被警告过期的天数(-1 表示“没有警告”)

:密码过期之后,系统自动禁用帐户的天数(-1 表示“永远不会禁用”)

:该帐户被禁用的天数(-1 表示“该帐户被启用”)

:保留供将来使用

在hash值那一栏,两个"$"符号之间的数字代表不同的加密算法,系统不同所使用的算法也不尽相同。第二个"$"符号之后的字符串就是salt值,加密的时候需要用到(好像是取每八个字符的每个字符的低7个bit位(取得的结果为01串)),然后得到8*7=56个bit位,然后用一定的规则反复加密(我不知道是什么规则),返回指向包含13个可打印的ASCII码字符([a-zA-Z0-9./])的指针,通常取前两位作为salt位,

数字和所使用的加密算法对应关系:

1: MD5 ,(22位)

2a: Blowfish, 只在有一部分linux分支中使用的加密方法

5: SHA-256  (43位)

6: SHA-512   (86位)

后面两种加密算法只在glibc2.7版本之后才支持。

第3个"$"后面的字符串就是加密后的密文了(应该是截取了加密密文中的某一段)

以下是crypt文档的部分内容:

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

       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)

       So   $5$salt$encrypted   is   an   SHA-256   encoded    password    and
       $6$salt$encrypted is an SHA-512 encoded one.

       "salt" stands for the up to 16 characters following "$id$" in the salt.
       The encrypted part of the password string is the actual computed  pass‐
       word.  The size of this string is fixed:

       MD5     | 22 characters
       SHA-256 | 43 characters
       SHA-512 | 86 characters

       The  characters  in  "salt"  and  "encrypted"  are  drawn  from the set
       [a–zA–Z0–9./].  In the MD5 and SHA implementations the  entire  key  is
       significant (instead of only the first 8 bytes in DES).