基于MD5 的双重加密
package com.goods.springbootmybatisgoods.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
public static String md5(String source) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] inputBytes = source.getBytes();
byte[] md5Bytes = md.digest(inputBytes);
StringBuilder sb = new StringBuilder();
for (byte b : md5Bytes) {
sb.append(String.format("%02x", b));
}
String md5Hash = sb.toString();
System.out.println("MD5 Hash: " + md5Hash);
return md5Hash;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static String md5(String source, String salt) {
try {
byte[] saltByte = salt.getBytes();
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] passwordWithSalt = concatenateByteArrays(source.getBytes(), saltByte);
byte[] md5Bytes = md.digest(passwordWithSalt);
StringBuilder sb = new StringBuilder();
for (byte b : md5Bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return source;
}
private static byte[] concatenateByteArrays(byte[] a, byte[] b) {
byte[] result = new byte[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}
public static String encodePassword(String password, String salt) {
return md5(md5(password, salt));
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87