What does MessageDigest.update (byte []) do?

What exactly does this do? I tried to find it, but found nothing.

Reason for request: I want to include SALT byte[] in the value, which is then hashed. So it should be done like this (Pseudocode):

 MessageDigest.update(SALT); MessageDigest.update(value); digestValue = MessageDigest.digest(); // Where SALT, value and digestValue are array bytes, byte[] 

SALT this add both SALT and value to the final digest, or should I combine both variables into one and then update once?

I could not find the answer for this in any documentation, any clarifications would be appreciated.

Thanks, greetings.

+4
source share
1 answer

MessageDigest is statefull, calls to MessageDigest.update(byte[] input) accumulate digest updates until we call MessageDigest.digest . Run this test to make sure:

  MessageDigest md1 = MessageDigest.getInstance("MD5"); md1.update(new byte[] {1, 2}); md1.update(new byte[] {3, 4}); System.out.println(Arrays.toString(md1.digest())); MessageDigest md2 = MessageDigest.getInstance("MD5"); md2.update(new byte[] {1, 2, 3, 4}); System.out.println(Arrays.toString(md2.digest())); 

Output

 [8, -42, -64, 90, 33, 81, 42, 121, -95, -33, -21, -99, 42, -113, 38, 47] [8, -42, -64, 90, 33, 81, 42, 121, -95, -33, -21, -99, 42, -113, 38, 47] 
+5
source

Source: https://habr.com/ru/post/1492687/


All Articles