To get an excellent answer from article 5 of Sahil Muthoo , below is a deeper look at the source code.
By default, the update method simply adds an input byte array to the current tempArray of the MessageDigestSpi abstract class.
The MessageDigest class extends the MessageDigestSpi class. Then MessageDigest.update is called the MessageDigestSpi.engineUpdate method, which can be found by examining the source code:
MessageDigest.java ( source )
196: 201: public void update(byte input) 202: { 203: engineUpdate(input); 204: } 205: 206: 217: public void update(byte[] input, int offset, int len) 218: { 219: engineUpdate(input, offset, len); 220: } ... 227: public void update(byte[] input) 228: { 229: engineUpdate(input, 0, input.length); 230: } ... 238: public void update (ByteBuffer input) 239: { 240: engineUpdate (input); 241: }
MessageDigestSpi.engineUpdate is an abstract method that must be implemented by extending classes, as shown below:
MessageDigestSpi.java ( source )
42: 49: protected abstract void engineUpdate(byte input); 50: 63: protected abstract void engineUpdate(byte[] input, int offset, int len); 64: 70: protected void engineUpdate(ByteBuffer input) { 71: if (!input.hasRemaining()) { 72: return; 73: } 74: byte[] tmp; 75: if (input.hasArray()) { 76: tmp = input.array(); 77: int offset = input.arrayOffset(); 78: int position = input.position(); 79: int limit = input.limit(); 80: engineUpdate(tmp, offset+position, limit - position); 81: input.position(limit); 82: } else { 83: tmp = new byte[input.limit() - input.position()]; 84: input.get(tmp); 85: engineUpdate(tmp, 0, tmp.length); 86: } 87: }
source share