Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modified PasswordUtils to use a StringBuilder #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 95 additions & 95 deletions src/main/java/com/shzlw/poli/util/PasswordUtils.java
Original file line number Diff line number Diff line change
@@ -1,118 +1,118 @@
package com.shzlw.poli.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.util.Base64;
import java.util.UUID;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public final class PasswordUtils {

private PasswordUtils() {}

public static synchronized String getUniqueId() {
return uuidToBase64(UUID.randomUUID().toString());
}

private static String uuidToBase64(String str) {
UUID uuid = UUID.fromString(str);
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return Base64.getUrlEncoder().withoutPadding().encodeToString(bb.array());
private PasswordUtils() {}

public static synchronized String getUniqueId() {
return uuidToBase64(UUID.randomUUID().toString());
}

private static String uuidToBase64(String str) {
UUID uuid = UUID.fromString(str);
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return Base64.getUrlEncoder().withoutPadding().encodeToString(bb.array());
}

public static String getMd5Hash(String s) {
String rt = "";
try {
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
m.update(s.getBytes());
byte[] digest = m.digest();
BigInteger bigInt = new BigInteger(1, digest);
StringBuilder hashtext = new StringBuilder(bigInt.toString(16));
while (hashtext.length() < 32) {
hashtext.append("0" + hashtext);
}
return hashtext.toString();
} catch (Exception e) {
e.printStackTrace();
}

public static String getMd5Hash(String s) {
String rt = "";
try {
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
m.update(s.getBytes());
byte[] digest = m.digest();
BigInteger bigInt = new BigInteger(1, digest);
String hashtext = bigInt.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
} catch(Exception e) {
e.printStackTrace();
}
return rt;
return rt;
}

public static String encrypt(String value) {
return encrypt(value, Constants.ENCRYPT_IV, Constants.ENCRYPT_KEY);
}

public static String decrypt(String value) {
return decrypt(value, Constants.ENCRYPT_IV, Constants.ENCRYPT_KEY);
}

public static String encrypt(String value, String initVector, String key) {
String rt = "";
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
rt = new String(Base64.getEncoder().encodeToString(encrypted));
} catch (Exception e) {
}

public static String encrypt(String value) {
return encrypt(value, Constants.ENCRYPT_IV, Constants.ENCRYPT_KEY);
}
return rt;
}

public static String decrypt(String value) {
return decrypt(value, Constants.ENCRYPT_IV, Constants.ENCRYPT_KEY);
}
public static String decrypt(String val, String initVector, String key) {
String rt = "";
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

public static String encrypt(String value, String initVector, String key) {
String rt = "";
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
rt = new String(Base64.getEncoder().encodeToString(encrypted));
} catch (Exception e) {
}

return rt;
byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(val));
rt = new String(decrypted);
} catch (Exception e) {
}

public static String decrypt(String val, String initVector, String key) {
String rt = "";
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(val));
rt = new String(decrypted);
} catch (Exception e) {
}
return rt;
}

public static String getEncryptedPassword(String password) {
String p = password == null ? "" : password;
return encrypt(Constants.PASSWORD_SALT + p);
return rt;
}

public static String getEncryptedPassword(String password) {
String p = password == null ? "" : password;
return encrypt(Constants.PASSWORD_SALT + p);
}

public static String getDecryptedPassword(String password) {
String p = decrypt(password);
int saltLength = Constants.PASSWORD_SALT.length();
return p.substring(saltLength);
}

public static String padOrTrimTo16(String str) {
String s = str;
if (str == null) {
s = "";
}

public static String getDecryptedPassword(String password) {
String p = decrypt(password);
int saltLength = Constants.PASSWORD_SALT.length();
return p.substring(saltLength);
if (s.length() == 16) {
return s;
} else if (s.length() > 16) {
return s.substring(0, 16);
}

public static String padOrTrimTo16(String str) {
String s = str;
if (str == null) {
s = "";
}

if (s.length() == 16) {
return s;
} else if (s.length() > 16) {
return s.substring(0, 16);
}

int l = 16 - s.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < l; i++) {
sb.append("0");
}
sb.append(s);
return sb.toString();
int l = 16 - s.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < l; i++) {
sb.append("0");
}
sb.append(s);
return sb.toString();
}
}