|
@@ -10,8 +10,8 @@ public class SHA256Util {
|
|
|
private static final String algorithm = "SHA-256";
|
|
|
private static final String saltAlgorithm = "SHA1PRNG";
|
|
|
|
|
|
- public static String encrypt(String text) {
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
+ public static String encrypt(String text) throws RuntimeException {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
try {
|
|
|
MessageDigest md = MessageDigest.getInstance(algorithm);
|
|
|
md.update(text.getBytes());
|
|
@@ -20,25 +20,48 @@ public class SHA256Util {
|
|
|
sb.append(String.format("%02x", b & 0xFF));
|
|
|
}
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
- return "";
|
|
|
+ throw new RuntimeException();
|
|
|
}
|
|
|
return sb.toString();
|
|
|
}
|
|
|
|
|
|
- public static String getSalt() {
|
|
|
+ public static String encrypt2(String planText) {
|
|
|
+ try {
|
|
|
+ MessageDigest md = MessageDigest.getInstance("SHA-256");
|
|
|
+ md.update(planText.getBytes());
|
|
|
+ byte byteData[] = md.digest();
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ for (int i = 0; i < byteData.length; i++) {
|
|
|
+ sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
|
|
|
+ }
|
|
|
+ StringBuffer hexString = new StringBuffer();
|
|
|
+ for (int i = 0; i < byteData.length; i++) {
|
|
|
+ String hex = Integer.toHexString(0xff & byteData[i]);
|
|
|
+ if (hex.length() == 1) {
|
|
|
+ hexString.append('0');
|
|
|
+ }
|
|
|
+ hexString.append(hex);
|
|
|
+ }
|
|
|
+ return hexString.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getSalt() throws RuntimeException {
|
|
|
String salt;
|
|
|
try {
|
|
|
SecureRandom random = SecureRandom.getInstance(saltAlgorithm);
|
|
|
- byte[] bytes = new byte[16];
|
|
|
- random.nextBytes(bytes);
|
|
|
- salt = new String(Base64.getEncoder().encode(bytes));
|
|
|
+ byte[] data = new byte[20];
|
|
|
+ random.nextBytes(data);
|
|
|
+ salt = new String(Base64.getEncoder().encode(data));
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
- return "1234567890";
|
|
|
+ throw new RuntimeException();
|
|
|
}
|
|
|
return salt;
|
|
|
}
|
|
|
|
|
|
- public static String encrypt(String text, String salt) {
|
|
|
+ public static String encrypt(String text, String salt) throws RuntimeException {
|
|
|
String saltText = text + salt;
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
try {
|
|
@@ -49,7 +72,7 @@ public class SHA256Util {
|
|
|
sb.append(String.format("%02x", b & 0xFF));
|
|
|
}
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
- return "";
|
|
|
+ throw new RuntimeException();
|
|
|
}
|
|
|
return sb.toString();
|
|
|
}
|