Calculating Your Own MAC

Once again, constructing the input stream is a matter of providing a message digest. In this example, weve again turned off the digest input stream after reading the string object in the file. Turning off the stream is strictly required in this case. We want to make sure that the digest we calculate is computed only over the string object and not the stored byte array that is, the stored message digest.

11.4 Implementing a MessageDigest Class

If you want to write your own security provider, you have the option of creating your own message digest engine. Typically, youd do this because you want to ensure that a particular algorithm like SHA is available regardless of who the default security provider is; if you have a mathematics background, its conceivable that you might want to implement your own algorithm. In order to implement a message digest algorithm, you must provide a concrete subclass of the MessageDigestSpi class. That means providing a body for each of the following methods: protected abstract void engineUpdatebyte input protected abstract void engineUpdatebyte[] input, int offset, int len Add the given bytes to the data over which the digest will be calculated. Note that there is no method in this list that accepts simply an array of bytes; the updatebyte[] b method in the base class simply uses an offset of and a length equal to the entire array. protected abstract byte[] engineDigest Calculate the digest over the accumulated data, resetting the internal state of the object afterwards. Note that there is no corresponding method that accepts an array of bytes as an argument; the digest method in the base class simply calls the engineUpdate method if needed before calling the engineDigest method. protected int engineDigestbyte buf[], int offset, int len Calculate the digest, placing the output into the buf array starting at the given offset and proceeding for len bytes and returning the length of the calculated digest. The default implementation of this method simply calls the engineDigest method and then copies the result into buf . The buffer passed to this method always has sufficient length to hold the digest since if the buffer had been too short the digest method itself would have thrown an exception. protected abstract void engineReset Reset the internal state of the engine, discarding all accumulated data and resetting the algorithm to an initial condition. protected int engineGetDigestLength Return the digest length that is supported by this implementation. Unlike most of the protected methods in this class, this method is not abstract; it does not need to be overridden. However, the default implementation simply returns 0. If is returned by this method, the getDigestLength method attempts to create a clone of the digest object, calculate its digest, and return the length of the calculated digest. If a digest implementation does not override this method and does not implement the Cloneable interface, the getDigestLength method will not operate correctly. Each of these methods corresponds to a public method of the MessageDigest class, with the name of the public method preceded by the word engine. The public methods that do not have a corresponding method