Apache Htpasswd生成和验证密码

摘要:
OpenSSL命令--passwd目的:passwd命令计算密钥的哈希值或列表中每个密钥的哈希值。用法:opensslpasswd[-crypt][-1][-apr1][-saltstring][-info][-stdin][-overide][-quiet][-table]{password}选项描述:-crypt:生成标准unix密码密文-4月1日:Apache md5密码密文。password:要处理的键值。示例:opensslpasswd crypt saltxx密码打印xxj31ZMTZzkVA。

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 newpassword: 
Adding password for user fdipzone

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

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

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

2.修改密码

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

3.增加用户

htpasswd .pass guest
New password: 
Re-type newpassword: 
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

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

Apache Htpasswd生成和验证密码第2张

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

免责声明:文章转载自《Apache Htpasswd生成和验证密码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇1032. 挖掘机技术哪家强(20)Android程序开发学习笔记系列——基础篇(附源码)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Eclipse利用Maven快速上手搭建MyBatis

一、what is maven? Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的项目管理工具软件。 Maven 除了以程序构建能力为特色之外,还提供高级项目管理工具。由于 Maven 的缺省构建规则有较高的可重用性,所以常常用两三行 Maven 构建脚本就可以构建简单的项目。由于 Maven 的面向项目的方法,许多...

CNUTCon2018-ApacheSkyWalkingV6.0可定制开源

Apache SkyWalking V6.0可定制开源APM 文章转载自博客:https://www.infoq.cn/article/LSZsFZuAhpT1zlZGPSLd?utm_source=related_read&utm_medium=article 微服务的盛行推动了链路追踪和应用性能监控系统的发展,在国外几款优秀的商业应用性能监控系...

PHP环境搭建

php环境分为两种:wanmplanmp PHP官网  w: windows 系统l: linux 系统a: apache 服务器n: nginx 服务器m: mysql 数据库p: php 服务器端的脚本语言 安装环境分为两种:集成环境 一键安装 集成环境 phpStudy wampserver apmserver xampp linux phpStu...

基于HttpClient实现网络爬虫~以百度新闻为例

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/40891791       基于HttpClient4.5实现网络爬虫请訪问这里:http://blog.csdn.net/xiaojimanman/article/details/53178307       在曾经的工作中,实现过简...

HBASE的预分区设计

hbase生成分区种子 1 package com.spdbccc.edm.storm.common; 2 3 import java.util.List; 4 5 import org.apache.commons.lang3.StringUtils; 6 import org.apache.hadoop.hbase.u...

Maven Android使用一

 Maven的坐标包括:groupId、artifactId、version、packaging、classifier。 groupId:定义当前maven项目隶属的实际项目; artifactId:定义实际项目中的一个maven项目(模块); version:定义当前所处的模板; packaging:定义maven项目的打包方式;   依赖对应的元素De...