import java.security.MessageDigest;
public class HashUtils {
public static String getMD5(String source) {
return getMD5(source.getBytes());
}
public static String getMD5(byte[] source) {
try {
return getHash(source, "MD5");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getSHA1(String source) {
return getSHA1(source.getBytes());
}
public static String getSHA1(byte[] source) {
try {
return getHash(source, "SHA1");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getSHA256(String source) {
return getSHA256(source.getBytes());
}
public static String getSHA256(byte[] source) {
try {
return getHash(source, "SHA-256");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getSHA512(String source) {
return getSHA512(source.getBytes());
}
public static String getSHA512(byte[] source) {
try {
return getHash(source, "SHA-512");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getHash(byte[] content, String algorithm) throws Exception {
String s;
char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
messageDigest.update(content);
byte tmp[] = messageDigest.digest();
char str[] = new char[tmp.length * 2];
int k = 0;
for (int i = 0; i < tmp.length; i++) {
byte byte0 = tmp[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
s = new String(str);
return s;
}
}
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class RSAUtil {
public static final String ALGORITHM = "RSA";
public static final String DEFAULT_CHARSET = "UTF8";
public static final int DEFAULT_KEY_SIZE = 2048;
public static java.security.KeyPair generatorKeyPair() throws Exception {
return generatorKeyPair(DEFAULT_KEY_SIZE);
}
public static java.security.KeyPair generatorKeyPair(int keySize) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(keySize);
keyPairGenerator.generateKeyPair();
return keyPairGenerator.genKeyPair();
}
public static String encrypt(String content, String key) throws Exception {
return encrypt(content, key, DEFAULT_CHARSET);
}
public static String encrypt(String content, String key, String charset) throws Exception {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(key));
KeyFactory factory = KeyFactory.getInstance(ALGORITHM);
PublicKey publicKey = factory.generatePublic(keySpec);
return Base64.getEncoder().encodeToString(BaseEncryptUtils.crypt(content.getBytes(charset), publicKey, Cipher.ENCRYPT_MODE, ALGORITHM));
}
public static String decrypt(String content, String key) throws Exception {
return decrypt(content, key, DEFAULT_CHARSET);
}
public static String decrypt(String content, String key, String charset) throws Exception {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(key));
KeyFactory factory = KeyFactory.getInstance(ALGORITHM);
PrivateKey privateKey = factory.generatePrivate(keySpec);
byte[] byteContent = Base64.getDecoder().decode(content);
return new String(BaseEncryptUtils.crypt(byteContent, privateKey, Cipher.DECRYPT_MODE, ALGORITHM), charset);
}
}
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;
public class AESUtils {
public static String DEFAULT_CHARSET = "UTF8";
public static int DEFAULT_KEY_SIZE = 128;
public static String ALGORITHM = "AES";
public static String generatorKey(String seed) throws Exception {
return generatorKey(seed, DEFAULT_KEY_SIZE);
}
public static String generatorKey(String seed, int keySize) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = new SecureRandom(seed.getBytes());
keyGenerator.init(keySize, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] encodeFormat = secretKey.getEncoded();
return Base64.getEncoder().encodeToString(encodeFormat);
}
public static String encrypt(String content, String key) throws Exception {
return encrypt(content, key, DEFAULT_CHARSET);
}
public static String encrypt(String content, String key, String charset) throws Exception {
return Base64.getEncoder().encodeToString(crypt(content.getBytes(charset), key, Cipher.ENCRYPT_MODE));
}
public static String decrypt(String content, String key) throws Exception {
return decrypt(content, key, DEFAULT_CHARSET);
}
public static String decrypt(String content, String key, String charset) throws Exception {
byte[] byteContent = Base64.getDecoder().decode(content);
return new String(crypt(byteContent, key, Cipher.DECRYPT_MODE), charset);
}
private static byte[] crypt(byte[] content, String key, int mode) throws Exception {
byte[] encodeFormat = Base64.getDecoder().decode(key);
SecretKeySpec keySpec = new SecretKeySpec(encodeFormat, ALGORITHM);
return BaseEncryptUtils.crypt(content, keySpec, mode, ALGORITHM);
}
}
HashUtils.getSHA256(user.getPassword())