Programmatically building htpasswd(以编程方式构建 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);
?>
以下是有关格式的更多信息:
Here's more information on the format:
http://httpd.apache.org/docs/2.2/misc/password_encryptions.html
这篇关于以编程方式构建 htpasswd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以编程方式构建 htpasswd


- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01