package com.yhx.utils;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class WPA2Hasher {
static SecretKeyFactory pbkdf2WithHmacSHA1;
static {
try {
pbkdf2WithHmacSHA1 = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public static String generate(String ssid,String password) {
try {
return toHex(pbkdf2WithHmacSHA1.generateSecret(
new PBEKeySpec(password.toCharArray(), ssid.getBytes(), 4096, 256)).getEncoded());
}catch(Exception ex) {return "ERROR";}
}
private static String toHex(byte[] array) {
try {
String hex = new BigInteger(1,array).toString(16);
int paddingLength = (array.length * 2) - hex.length();
if(paddingLength > 0) {
return String.format("%0" + paddingLength + "d", 0) + hex;
}else {
return hex;
}
}catch(Exception ex) {}
return "WARN: Unknown error occured";
}
public static void main(String[] args)throws Exception {
long start = System.currentTimeMillis();
ExecutorService executorService = Executors.newCachedThreadPool();
CountDownLatch countDownLatch = new CountDownLatch(100);
for(int i = 0; i < 1000; i++) {
int k=i;
executorService.execute(()->{
for(int t = 0; t < 10; t++) {
generate("1", k+"");
}
countDownLatch.countDown();
});
}
countDownLatch.await();
long end = System.currentTimeMillis();
System.out.println(end-start);
}
}