問(wèn)題描述
是否有一種編程方式來(lái)構(gòu)建 htpasswd 文件,而不依賴(lài)于操作系統(tǒng)特定的函數(shù)(即 exec()
、passthru()
)?
Is there a programmatic way to build htpasswd files, without depending on OS specific functions (i.e. exec()
, passthru()
)?
推薦答案
.httpasswd 文件只是具有特定格式的文本文件,具體取決于指定的散列函數(shù).如果您使用的是 MD5,它們看起來(lái)像這樣:
.httpasswd files are just text files with a specific format depending on the hash function specified. If you are using MD5 they look like this:
foo:$apr1$y1cXxW5l$3vapv2yyCXaYz8zGoXj241
那是登錄名、冒號(hào)、,$apr1$、鹽和 1000 次 md5 編碼為 base64.如果您選擇 SHA1,它們看起來(lái)像這樣:
That's the login, a colon, ,$apr1$, the salt and 1000 times md5 encoded as base64. If you select SHA1 they look like this:
foo:{SHA}BW6v589SIg3i3zaEW47RcMZ+I+M=
這是登錄名、冒號(hào)、字符串 {SHA} 和用 base64 編碼的 SHA1 哈希.
That's the login, a colon, the string {SHA} and the SHA1 hash encoded with base64.
如果您的語(yǔ)言實(shí)現(xiàn)了 MD5 或 SHA1 和 base64,您可以像這樣創(chuàng)建文件:
If your language has an implementation of either MD5 or SHA1 and base64 you can just create the file like this:
<?php
$login = 'foo';
$pass = 'pass';
$hash = base64_encode(sha1($pass, true));
$contents = $login . ':{SHA}' . $hash;
file_put_contents('.htpasswd', $contents);
?>
以下是有關(guān)格式的更多信息:
Here's more information on the format:
http://httpd.apache.org/docs/2.2/misc/password_encryptions.html
這篇關(guān)于以編程方式構(gòu)建 htpasswd的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!