merge
This commit is contained in:
@@ -11,7 +11,7 @@ import java.security.MessageDigest;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* 说明:散列算法(哈希算法)
|
||||
* 说明:散列算法/摘要算法
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
public class Digests {
|
||||
@@ -20,6 +20,20 @@ public class Digests {
|
||||
private static SecureRandom random = new SecureRandom();
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Digests.class);
|
||||
|
||||
/**
|
||||
* 生成随机的Byte[]作为salt.
|
||||
*
|
||||
* @param numBytes byte数组的大小
|
||||
*/
|
||||
public static byte[] generateSalt(int numBytes) {
|
||||
if (numBytes <= 0) {
|
||||
throw new UtilException("numBytes argument must be a positive integer (1 or larger)");
|
||||
}
|
||||
byte[] bytes = new byte[numBytes];
|
||||
random.nextBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行sha1散列.
|
||||
*/
|
||||
@@ -36,7 +50,7 @@ public class Digests {
|
||||
}
|
||||
|
||||
/**
|
||||
* 对字符串进行散列, 支持md5与sha1算法.
|
||||
* 对字符串进行加密, 支持md5与sha1算法.
|
||||
*/
|
||||
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
|
||||
try {
|
||||
@@ -57,21 +71,7 @@ public class Digests {
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机的Byte[]作为salt.
|
||||
*
|
||||
* @param numBytes byte数组的大小
|
||||
*/
|
||||
public static byte[] generateSalt(int numBytes) {
|
||||
if (numBytes <= 0) {
|
||||
throw new UtilException("numBytes argument must be a positive integer (1 or larger)");
|
||||
}
|
||||
byte[] bytes = new byte[numBytes];
|
||||
random.nextBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对文件进行md5散列.
|
||||
* 对文件进行md5摘要.
|
||||
*/
|
||||
public static byte[] md5(InputStream input) {
|
||||
return digest(input, MD5);
|
||||
|
||||
@@ -42,7 +42,6 @@ public class EncodeUtils {
|
||||
* Base64编码.
|
||||
*/
|
||||
public static String base64Encode(byte[] input) {
|
||||
// return new String(org.apache.commons.codec.binary.Base64.encodeBase64(input));
|
||||
return Base64.getEncoder().encodeToString(input);
|
||||
}
|
||||
|
||||
@@ -51,7 +50,6 @@ public class EncodeUtils {
|
||||
* Base64编码, URL安全(将Base64中的URL非法字符如+,/=转为其他字符, 见RFC3548).
|
||||
*/
|
||||
public static String base64UrlSafeEncode(byte[] input) {
|
||||
// return Base64.encodeBase64URLSafeString(input);
|
||||
return java.util.Base64.getUrlEncoder().encodeToString(input);
|
||||
}
|
||||
|
||||
@@ -59,7 +57,6 @@ public class EncodeUtils {
|
||||
* Base64解码.
|
||||
*/
|
||||
public static byte[] base64Decode(String input) {
|
||||
// return Base64.decodeBase64(input);
|
||||
return java.util.Base64.getDecoder().decode(input);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,8 +32,7 @@ public class IpUtil {
|
||||
|| "unknown".equalsIgnoreCase(ip)) {
|
||||
// 获取的IP实际上是代理服务器的地址,并不是客户端的IP地址
|
||||
ip = request.getRemoteAddr();
|
||||
if (ip.equals("127.0.0.1")
|
||||
|| ip.equals("0:0:0:0:0:0:0:1")) {
|
||||
if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
|
||||
// 根据网卡取本机配置的IP
|
||||
InetAddress inet = null;
|
||||
try {
|
||||
|
||||
@@ -27,7 +27,7 @@ public class PassWordUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入未加密密码,如果为空,随机生成, 传入盐值, 如果为空,随机生成。 返回密码,盐值,和加密后的密码
|
||||
* 传入未加密密码,如果为空,随机生成。传入盐值, 如果为空,随机生成。返回密码,盐值,和加密后的密码
|
||||
*
|
||||
* @param password
|
||||
* @return
|
||||
@@ -46,8 +46,7 @@ public class PassWordUtil {
|
||||
salt = EncodeUtils.hexDecode(pwdSalt);
|
||||
}
|
||||
// 密码加密
|
||||
byte[] hashPassword = Digests.sha1(password.getBytes(), salt,
|
||||
HASH_INTERATIONS);
|
||||
byte[] hashPassword = Digests.sha1(password.getBytes(), salt, HASH_INTERATIONS);
|
||||
str[0] = password;
|
||||
str[1] = EncodeUtils.hexEncode(salt);
|
||||
str[2] = EncodeUtils.hexEncode(hashPassword);
|
||||
|
||||
@@ -61,7 +61,6 @@ public class RSAUtil {
|
||||
* 从字符串中加载公钥
|
||||
*
|
||||
* @param publicKeyStr 公钥数据字符串
|
||||
* @throws Exception 加载公钥时产生的异常
|
||||
*/
|
||||
public static RSAPublicKey loadPublicKey(String publicKeyStr) {
|
||||
try {
|
||||
@@ -110,11 +109,10 @@ public class RSAUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 功能描述: 从字符串中加载私钥
|
||||
* 功能描述:从字符串中加载私钥
|
||||
*
|
||||
* @param privateKeyStr
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static RSAPrivateKey loadPrivateKey(String privateKeyStr) {
|
||||
try {
|
||||
@@ -141,12 +139,22 @@ public class RSAUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* 默认加密方式
|
||||
*
|
||||
* @param data 明文数据
|
||||
* @return 密文
|
||||
*/
|
||||
public static String encrypt(String data) {
|
||||
return encrypt(loadPublicKey(), data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密过程
|
||||
*
|
||||
* @param publicKey 公钥 明文数据
|
||||
* @return
|
||||
* @throws Exception 加密过程中的异常信息
|
||||
* @param publicKey 公钥
|
||||
* @param data 明文数据
|
||||
* @return 密文
|
||||
*/
|
||||
public static String encrypt(RSAPublicKey publicKey, String data) {
|
||||
if (publicKey == null) {
|
||||
@@ -175,22 +183,22 @@ public class RSAUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* 默认加密方式
|
||||
/**
|
||||
* 功能描述:默认解密方式
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
* @param data 密文
|
||||
* @return 明文
|
||||
*/
|
||||
public static String encrypt(String data) {
|
||||
return encrypt(loadPublicKey(), data);
|
||||
public static String decrypt(String data) {
|
||||
return decrypt(loadPrivateKey(), data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密过程
|
||||
*
|
||||
* @param privateKey 私钥 密文数据
|
||||
* @param privateKey 私钥
|
||||
* @param data 密文数据
|
||||
* @return 明文
|
||||
* @throws Exception 解密过程中的异常信息
|
||||
*/
|
||||
public static String decrypt(RSAPrivateKey privateKey, String data) {
|
||||
if (privateKey == null) {
|
||||
@@ -222,16 +230,6 @@ public class RSAUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 功能描述: 默认解密方式
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
public static String decrypt(String data) {
|
||||
return decrypt(loadPrivateKey(), data);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String en = testJiami();// 密文
|
||||
String de = testJiemi(en);// 明文
|
||||
|
||||
Reference in New Issue
Block a user