我想比较 sha256 和“Hmac with sha256”的执行时间。
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
// TODO code application logic here
byte[] data = {0,3,0,6,0,0,8,0,9,0,0,8,0,0,5,0};
MessageDigest md = MessageDigest.getInstance("SHA-256");
long SHaStart = System.nanoTime();
md.digest(data);
long SHaTime = System.nanoTime() - SHaStart;
System.out.println("SHaTime:"+SHaTime);
}
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
// TODO code application logic here
byte[] keyBytes = new byte[]{0,1,2,3,4,5,6,7,8 ,9,10,11,12,13,14,15};
String algorithm = "RawBytes";
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, algorithm);
javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA256");
mac.init(secretKey);
byte[] data = {0,3,0,6,0,0,8,0,9,0,0,8,0,0,5,0};
long macStart = System.nanoTime();
mac.doFinal(data);
long MAcTime = System.nanoTime() - macStart;
System.out.println("macTime:"+MAcTime);
}
结果是:SHATime:489589 和 macTime:180070,据我所知,计算 sha256 是计算 Hmac 的一部分;所以应该花更少的时间。有人可以帮我吗?