2011-09-13 66 views
0

我想要生成一個唯一的字符串/ ID給出另一個相對較大的字符串(由目錄路徑名組成),想到使用crypt函數。然而,這並不像預期的那樣工作,很可能是因爲我無法理解。perl crypt函數需要幫助

這裏的代碼&輸出:

#!/usr/bin/perl 

print "Enter a string:"; 
chomp(my $string = <STDIN>); 

my $encrypted_string = crypt($string,'di'); 

print "\n the encrypted string is:$encrypted_string"; 

輸出:

$ perl crypt_test 
Enter a string:abcdefghi 

the encrypted string is:dipcn0ADeg0Jc 
$ 
$ perl crypt_test 
Enter a string:abcdefgh 

the encrypted string is:dipcn0ADeg0Jc 
$ 
$ 
$ perl crypt_test 
Enter a string:abcde 

the encrypted string is:diGyhSp4Yvj4M 
$ 

我不明白爲什麼它返回相同的加密字符串前兩個字符串和不同的第三個。請注意鹽對所有人都是一樣的。

+0

如果你想唯一的字符串,嘗試數據:: UUID。從Mat的回答可以看出,crypt()函數是古老而有限的。 – frezik

回答

1

crypt(3)函數僅考慮所述輸入字符串的第一個8個字符:

通過爲各鍵的前八個字符的最低7位,獲得56位的密鑰。這個56位密鑰用於重複加密一個常量字符串(通常是一個字符串,全爲零的字符串)。返回的值指向加密密碼,一系列13個可打印的ASCII字符(前兩個字符代表鹽本身)。

所以你看到的是通過設計 - 從perlfunc

crypt PLAINTEXT,SALT 
     Creates a digest string exactly like the crypt(3) function in the C library 
+0

謝謝地毯,我很愚蠢! – ernesto