Чтобы построить из bullet 5 из превосходного ответа Sahil Muthoo , ниже приведен более глубокий взгляд на исходный код.
По умолчанию метод update
просто добавляет входной байтмассив до текущего tempArray
абстрактного класса MessageDigestSpi
.
Класс MessageDigest
расширяет класс MessageDigestSpi
.Затем вызывается MessageDigest.update
, вызывается метод MessageDigestSpi.engineUpdate
, который можно найти при исследовании исходного кода:
MessageDigest.java ( исходный код )
196: /**
197: * Updates the digest with the byte.
...
200: */
201: public void update(byte input)
202: {
203: engineUpdate(input);
204: }
205:
206: /**
207: * Updates the digest with the bytes from the array starting from the
208: * specified offset and using the specified length of bytes.
209: *
210: * @param input
211: * bytes to update the digest with.
212: * @param offset
213: * the offset to start at.
214: * @param len
215: * length of the data to update with.
216: */
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
- это абстрактный метод, который должен быть реализован путем расширения классов, как показано ниже:
MessageDigestSpi.java ( исходный код )
42: /**
43: * Updates this {@code MessageDigestSpi} using the given {@code byte}.
44: *
45: * @param input
46: * the {@code byte} to update this {@code MessageDigestSpi} with.
47: * @see #engineReset()
48: */
49: protected abstract void engineUpdate(byte input);
50: /**
51: * Updates this {@code MessageDigestSpi} using the given {@code byte[]}.
52: *
53: * @param input
54: * the {@code byte} array.
55: * @param offset
56: * the index of the first byte in {@code input} to update from.
57: * @param len
58: * the number of bytes in {@code input} to update from.
59: * @throws IllegalArgumentException
60: * if {@code offset} or {@code len} are not valid in respect to
61: * {@code input}.
62: */
63: protected abstract void engineUpdate(byte[] input, int offset, int len);
64: /**
65: * Updates this {@code MessageDigestSpi} using the given {@code input}.
66: *
67: * @param input
68: * the {@code ByteBuffer}.
69: */
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: }