我在htpasswd中使用"强制MD5加密密码"来生成实例’123’的哈希我得到:
使用htpasswd:123 => $ apr1 $ kaTbKaLO $ ewJXRZAKpjaxK4thy2jOp /
使用MD5摘要:123 => 202cb962ac59075b964b07152d234b70
参照官网
编写 Java代码
String user = "root";
String realm = "";
String password = "123456";
byte b[] = java.security.MessageDigest.getInstance("MD5").digest( (user + ":" + realm + ":" + password ).getBytes());
java.math.BigInteger bi = new java.math.BigInteger(1, b);
String s = bi.toString(16);
while (s.length() < 32)
s = "0" + s;
// String s is the encrypted password
System.out.println(s);
String pwd = "{SHA}" + new sun.misc.BASE64Encoder().encode(java.security.MessageDigest.getInstance("SHA1").digest(password.getBytes()));
System.out.println(pwd);
//最终这个可行
//等比 htpasswd -c /www/server/nginx/conf/auth-conf root
String enPasswd = Md5Crypt.apr1Crypt(password.getBytes());
String htpasswdContent = user + ":" + enPasswd;
System.out.println(htpasswdContent);