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內容如下
當訪問該目錄下的文件時,則會彈出要求輸入用戶名和密碼的輸入框。輸入正確的用戶名和密碼后就能正常訪問。
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