SHA-256执行时间小于Java中的HmacSHA256

时间:2018-12-23 16:31:13

标签: java sha256 jce message-authentication-code

我想比较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的一部分;因此应该花费更少的时间。有人可以帮我吗?

0 个答案:

没有答案