发布网友 发布时间:2022-04-23 12:55
共1个回答
热心网友 时间:2023-05-15 18:01
public static void main(String[] args) {
String pass = "123456";
byte[] data;
byte[] salt = new byte[4];
byte[] digest1 = new byte[20];
byte[] tmpptr;
int i, j;
int len = 4 + pass.length();
// c语言中打开linux文件中的/dev/urandom产生随机数
// java这里模拟了
Random random = new Random();
salt[0] = (byte) random.nextInt(128);
salt[1] = (byte) random.nextInt(128);
salt[2] = (byte) random.nextInt(128);
salt[3] = (byte) random.nextInt(128);
System.out.print("Salt:");
for (i = 0; i < 4; i++) {
System.out.print(Integer.toHexString(salt[i]) + " ");
}
System.out.println();
data = new byte[len];
for (i = 0; i < 4; i++) {
data[i] = salt[i];
}
char[] passArr = pass.toCharArray();
for (i = 4; i < len; i++) {
data[i] = (byte) passArr[i - 4];
}
System.out.print("\nData:");
for (i = 0; i < len; i++) {
System.out.print(Integer.toHexString(data[i]) + " ");
}
System.out.println();
digest1 = sha1(data);
for (i = 0; i < 20; i++) {
System.out.print(Integer.toHexString(digest1[i]) + " ");
}
System.out.println();
data = digest1;
for (j = 0; j < 10000; j++) {
tmpptr = sha1(data);
data = tmpptr;
}
for (i = 0; i < 20; i++) {
System.out.print(Integer.toHexString(data[i]) + " ");
}
System.out.println();
}
public static byte[] sha1(byte[] origin) {
try {
MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
return mdTemp.digest(origin);
} catch (Exception e) {
return null;
}
}
大体上是这样