SHA-256 Execution time is less than HmacSHA256 in java

294 Views Asked by At

I want to compare sha256 and "Hmac with sha256" execution time.

 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);
    }

the result is: SHaTime:489589 and macTime:180070, as I know caculating sha256 is a part of caculating Hmac; so it should take less time. can any one help me with it please?

0

There are 0 best solutions below