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, you’d 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, it’s 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 engineUpdate(byte input)protected abstract void engineUpdate(byte[] 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
update(byte[] 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 theengineUpdate( )
method if needed before calling theengineDigest( )
method.
- protected int engineDigest(byte buf[], int offset, int len)
Calculate the digest, placing the output into the
buf
array (starting at the givenoffset
and proceeding ...
Get Java Security, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.