以编程方式构建htpasswd [英] Programmatically building htpasswd
问题描述
是否有一种编程方式来生成 htpasswd 文件,而无需依赖于操作系统的特定功能(即exec()
,passthru()
)?
Is there a programmatic way to build htpasswd files, without depending on OS specific functions (i.e. exec()
, passthru()
)?
推荐答案
.httpasswd文件只是具有特定格式的文本文件,具体取决于指定的哈希函数.如果您使用的是MD5,则它们如下所示:
.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
这是登录名,冒号,$ apr1 $,盐和1000倍的md5编码为base64.如果您选择SHA1,它们将如下所示:
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=
这是登录名,冒号,字符串{SHA}和以base64编码的SHA1哈希.
That's the login, a colon, the string {SHA} and the SHA1 hash encoded with base64.
如果您的语言具有MD5或SHA1和base64的实现,则可以像这样创建文件:
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);
?>
有关格式的更多信息:
http://httpd.apache.org/docs/2.2/misc/password_encryptions.html
这篇关于以编程方式构建htpasswd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!