Following is a code that will encrypts the user String :
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.No
To build off of bullet 5 from Sahil Muthoo's excellent answer, below is an deeper look into the source code.
By default, the update
method simply appends the 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 method MessageDigestSpi.engineUpdate
is called, which can be found from investigating the source code:
MessageDigest.java (source code)
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
is an abstract method that must be implemented by extending classes as seen below:
MessageDigestSpi.java (source code)
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: }