原文出處: http://ly50247.appspot.com/2010/12/24/shadow_hash.html
linux的/etc/shadow是專門用於存放用戶口令的,以前一直聽說口令是用md5加密(其實是hash)過的,但里連的格式和md5sum生成的並不一樣。可以查看下/etc/shadow,對該文件不了解的話可以上網查詢。
下面來看第二列(這個是我隨便生成的,並不是系統的):
$1$eCeLr51L$/1EEtFr8iQ.TySiJKHQRQ/
可以看出格式比較奇怪,里邊有三個$,還有./等東西,而md5sum生成的hash類似這樣
1
$
echo
-n password|md5sum
2
5f4dcc3b5aa765d61d8327deb882cf99 -
是十六進制串。顯然shadow不是將口令直接md5sum得到的。其余可以 man 3 crypt 了解一下,內容太多,只貼一部分:
-------------
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)
-------------
基本可以了解格式了。
也就是說hash方法是可以選擇的,只有$1$就是MD5,而且產生這個還需要一個salt,它是一個8字節的字符串,hash是根據它和口令一樣生成的,而shadow里這個salt是隨機生成的。
可以寫個程序試驗一下:
01
#define _XOPEN_SOURCE
02
#include <stdio.h>
03
#include <unistd.h>
04
int
main(
int
argc,
char
*argv[])
05
{
06
char
key[20] =
"password"
;
07
char
salt[20] =
"$1$eCeLr51L33"
;
08
printf
(
"%s\n"
, crypt(key, salt));
09
10
return
0;
11
}
其余最開始的 $1$eCeLr51L$/1EEtFr8iQ.TySiJKHQRQ/ 就是用它生成的。shadow用的基本也是這個方法,只是那個salt不是確定的,可以用mkpasswd進行生成。
它雖然說是md5 Method,但和md5sum並不相同,二者也不可以互相轉換。