The Mac class itself is not data — it stores data and updates it when update and doFinal .
Currently, you only call init with the key - then you need to call update (optional), and then doFinal . When you call doFinal , this will return the byte[] actual hash. Currently, we cannot see any data that you want to use, but your sample will be modified as follows:
Mac mac = Mac.getInstance("HmacSHA1"); mac.init(key); mac.update(data1); mac.update(data2); mac.update(data3); // Etc... byte[] hash = mac.doFinal(); String base64Hash = Base64.getEncoder().encodeToString(hash);
You can also call doFinal data transfer - if you have only one piece of data for the hash, it means that you do not need to call update :
Mac mac = Mac.getInstance("HmacSHA1"); mac.init(key); byte[] hash = mac.doFinal(data); String base64Hash = Base64.getEncoder().encodeToString(hash);
source share