Apache Htpasswd生成和验证密码

Assuming you create the password using the following command and "myPassword" as the password
  htpasswd -c /usr/local/apache/passwd/passwords username搜索

  This will create a file that looks like
  username:$apr1$sr15veBe$cwxJZHTVLHBkZKUoTHV.k.

  The $apr1$ is the hashing method, sr15veBe is the salt, and the last string is the hashed password. You can validate it using openssl using
  openssl passwd -apr1 -salt sr15veBe myPassword

  which will output
  $apr1$sr15veBe$cwxJZHTVLHBkZKUoTHV.k.

  A pipeline which you could use would be:
  username="something"
htpasswd -c /usr/local/apache/passwd/passwords $username
****Enter password:****

salt=$($(cat passwords | cut -d$ -f3)
password=$(openssl passwd -apr1 -salt $salt)
****Enter password:****

grep -q $username:$password passwords
if [ $? -eq 0 ]
then echo "password is valid"
else
echo "password is invalid"
fi
You may need to change your openssl command, as Apache's htpasswd command crypts slightly differently on each system.

 

htpasswd 是apache的小工具,在apache安装目录bin下可找到。

 

Usage:
    htpasswd [-cmdpsD] passwordfile username
    htpasswd -b[cmdpsD] passwordfile username password

    htpasswd -n[mdps] username
    htpasswd -nb[mdps] username password
 -c  创建一个新的加密密码文件
 -n  不更新文件,显示结果
 -m  使用MD5加密密码
 -d  使用CRYPT加密密码(默认)
 -p  不加密密码
 -s  使用SHA加密密码
 -b  直接在命令行输入密码,而不是提示后再输入密码
 -D  删除用户
在Windows, NetWare与 TPF系统,'-m' 是默认的密码加密方式。
在所有其他系统,'-p'可能不能使用。

1.生成加密密码文件

htpasswd -c .pass fdipzone
New password: 
Re-type new password: 
Adding password for user fdipzone

这样就生成了一个使用CRYPT加密密码的文件 

以下命令可以生成用md5加密密码的文件,b参数的作用是在命令行直接把密码输入,这样就不需要等提示输入密码再输入了。

htpasswd -cmb .pass fdipzone 123456
Adding password for user fdipzone

2.修改密码

htpasswd .pass fdipzone
New password: 
Re-type new password: 
Updating password for user fdipzone

3.增加用户

htpasswd .pass guest
New password: 
Re-type new password: 
Adding password for user guest

4.删除用户

htpasswd -D .pass guest
Deleting password for user guest

如果使用p参数,不加密密码,系统会提示Warning,因此为了安全最好不要使用p参数。

 

htpasswd -cp .pass fdipzone
Warning: storing passwords as plain text might just not work on this platform.

 

.htaccess 调用加密密码文件控制访问权限

 

 

首先 Allowoverride 需要设置为 AuthConfig 或 All

然后在需要控制访问的目录下增加.htaccess

 

.htaccess内容如下

 

AuthType "Basic"
AuthName "Password Required" #提示文字
AuthUserFile "/home/fdipzone/sites/pass/.pass" #加密密码的文件
Require valid-user

 

当访问该目录下的文件时,则会弹出要求输入用户名和密码的输入框。输入正确的用户名和密码后就能正常访问。

 

 

tips:为了安全,加密的密码文件请不要放在线上用户可以访问的路径。

 

OpenSSL命令---passwd

用途:

passwd命令计算一个密钥的哈希值或者计算列表中的每个密钥的hash值。

用法:

 

openssl passwd [-crypt] [-1] [-apr1] [-salt  string] [-in file] [-stdin] [-noverify] [-quiet] [-table] {password}

 

选项说明:

-crypt:生成标准的unix口令密文。默认选项。

-1:用MD5基于BSD的密钥算法。

-apr1:Apache md5口令密文。

-salt  string:用指定的字符串填充。当从终端读取一个密钥时,则填充它。

-in file:从指定的文件中读取密钥。

-stdin:从stdin中读取密钥。

-noverify:当从终端读取口令时不去验证它。

-quiet:当命令行提供的密钥是缩短了的,则不输出警告。

-table:用户输入的口令和结果用缩进隔开。

password:需要处理的密钥值。

实例:

openssl passwd -crypt -salt xx password  

打印出xxj31ZMTZzkVA。

 

openssl passwd -1 -salt xxxxxxxx password

 

打印出($1$xxxxxxxx$UYCIxa628.9qXjpQCjM4a.)。

 

openssl passwd -apr1 -salt xxxxxxxx password

 

打印出<$apr1$xxxxxxxx$dxHfLAsjHkDRmG83UXe8K0。

 

 

https://zhidao.baidu.com/question/372107922550340204.html

http://blog.csdn.net/fdipzone/article/details/41020045 

http://blog.csdn.net/as3luyuan123/article/details/14917959

 

posted @ 2018-01-04 23:07  昨日微风  阅读(4102)  评论(1编辑  收藏  举报