|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectfr.cryptohash.DigestEngine
public abstract class DigestEngine
This class is a template which can be used to implement hash functions. It takes care of some of the API, and also provides an internal data buffer whose length is equal to the hash function internal block length.
Classes which use this template MUST provide a working Digest.getBlockLength()
method even before initialization (alternatively,
they may define a custom getInternalBlockLength()
which does
not call Digest.getBlockLength()
. The Digest.getDigestLength()
should
also be operational from the beginning, but it is acceptable that it
returns 0 while the doInit()
method has not been called
yet.
==========================(LICENSE BEGIN)============================ Copyright (c) 2007-2010 Projet RNRT SAPHIR Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ===========================(LICENSE END)=============================
Constructor Summary | |
---|---|
DigestEngine()
Instantiate the engine. |
Method Summary | |
---|---|
protected Digest |
copyState(DigestEngine dest)
This function copies the internal buffering state to some other instance of a class extending DigestEngine . |
byte[] |
digest()
Finalize the current hash computation and return the hash value in a newly-allocated array. |
byte[] |
digest(byte[] input)
Input some bytes, then finalize the current hash computation and return the hash value in a newly-allocated array. |
int |
digest(byte[] buf,
int offset,
int len)
Finalize the current hash computation and store the hash value in the provided output buffer. |
protected abstract void |
doInit()
This function is called at object creation time; the implementation should use it to perform initialization tasks. |
protected abstract void |
doPadding(byte[] buf,
int off)
Perform the final padding and store the result in the provided buffer. |
protected abstract void |
engineReset()
Reset the hash algorithm state. |
protected int |
flush()
Flush internal buffers, so that less than a block of data may at most be upheld. |
protected byte[] |
getBlockBuffer()
Get a reference to an internal buffer with the same size than a block. |
protected long |
getBlockCount()
Get the "block count": this is the number of times the processBlock(byte[]) method has been invoked for the
current hash operation. |
protected int |
getInternalBlockLength()
Get the internal block length. |
protected abstract void |
processBlock(byte[] data)
Process one block of data. |
void |
reset()
Reset the object: this makes it suitable for a new hash computation. |
void |
update(byte input)
Insert one more input data byte. |
void |
update(byte[] input)
Insert some more bytes. |
void |
update(byte[] input,
int offset,
int len)
Insert some more bytes. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface fr.cryptohash.Digest |
---|
copy, getBlockLength, getDigestLength, toString |
Constructor Detail |
---|
public DigestEngine()
Method Detail |
---|
protected abstract void engineReset()
protected abstract void processBlock(byte[] data)
data
- the data blockprotected abstract void doPadding(byte[] buf, int off)
flush()
and then update(byte)
with the appropriate padding
data in order to get the full input data.
buf
- the output bufferoff
- the output offsetprotected abstract void doInit()
Digest.getDigestLength()
.
public byte[] digest()
Digest
digest
in interface Digest
Digest
public byte[] digest(byte[] input)
Digest
digest
in interface Digest
input
- the input data
Digest
public int digest(byte[] buf, int offset, int len)
Digest
len
parameter
contains the maximum number of bytes that should be written;
no more bytes than the natural hash function output length will
be produced. If len
is smaller than the natural
hash output length, the hash output is truncated to its first
len
bytes. The object is resetted.
digest
in interface Digest
buf
- the output bufferoffset
- the output offset within outbuf
len
- the requested hash output length (in bytes)
outbuf
Digest
public void reset()
Digest
reset
in interface Digest
Digest
public void update(byte input)
Digest
update
in interface Digest
input
- the input byteDigest
public void update(byte[] input)
Digest
update
in interface Digest
input
- the data bytesDigest
public void update(byte[] input, int offset, int len)
Digest
update
in interface Digest
input
- the data bufferoffset
- the data offset in inbuf
len
- the data length (in bytes)Digest
protected int getInternalBlockLength()
processBlock(byte[])
. The default implementation of this
method calls Digest.getBlockLength()
and returns the same
value. Overriding this method is useful when the advertised
block length (which is used, for instance, by HMAC) is
suboptimal with regards to internal buffering needs.
protected final int flush()
protected final byte[] getBlockBuffer()
flush()
: if
flush()
return the value n
, then the
first n
bytes of the array returned by this method
are the n
bytes of input data which are still
unprocessed. The values of the remaining bytes are
undefined and may be altered at will.
protected long getBlockCount()
processBlock(byte[])
method has been invoked for the
current hash operation. That counter is incremented
after the call to processBlock(byte[])
.
protected Digest copyState(DigestEngine dest)
DigestEngine
.
It returns a reference to the copy. This method is intended
to be called by the implementation of the Digest.copy()
method.
dest
- the copy
dest
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |