Package fr.cryptohash
Class DigestEngine
java.lang.Object
fr.cryptohash.DigestEngine
- All Implemented Interfaces:
Digest
- Direct Known Subclasses:
BLAKE224,BLAKE256,BLAKE384,BLAKE512,BMW224,BMW256,BMW384,BMW512,CubeHash224,CubeHash256,CubeHash384,CubeHash512,ECHO224,ECHO256,ECHO384,ECHO512,Groestl224,Groestl256,Groestl384,Groestl512,HAVAL128_3,HAVAL128_4,HAVAL128_5,HAVAL160_3,HAVAL160_4,HAVAL160_5,HAVAL192_3,HAVAL192_4,HAVAL192_5,HAVAL224_3,HAVAL224_4,HAVAL224_5,HAVAL256_3,HAVAL256_4,HAVAL256_5,HMAC,JH224,JH256,JH384,JH512,Keccak224,Keccak256,Keccak384,Keccak512,Luffa224,Luffa256,Luffa384,Luffa512,MD2,MD4,MD5,PANAMA,RadioGatun32,RadioGatun64,RIPEMD,RIPEMD128,RIPEMD160,SHA0,SHA1,SHA224,SHA256,SHA384,SHA512,SHAvite224,SHAvite256,SHAvite384,SHAvite512,SIMD224,SIMD256,SIMD384,SIMD512,Tiger,Tiger2,Whirlpool,Whirlpool0,Whirlpool1
public abstract class DigestEngine extends Object implements Digest
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)=============================
- Version:
- $Revision: 229 $
- Author:
- Thomas Pornin <thomas.pornin@cryptolog.com>
-
Constructor Summary
Constructors Constructor Description DigestEngine()Instantiate the engine. -
Method Summary
Modifier and Type Method Description protected DigestcopyState(DigestEngine dest)This function copies the internal buffering state to some other instance of a class extendingDigestEngine.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.intdigest(byte[] buf, int offset, int len)Finalize the current hash computation and store the hash value in the provided output buffer.protected abstract voiddoInit()This function is called at object creation time; the implementation should use it to perform initialization tasks.protected abstract voiddoPadding(byte[] buf, int off)Perform the final padding and store the result in the provided buffer.protected abstract voidengineReset()Reset the hash algorithm state.protected intflush()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 longgetBlockCount()Get the "block count": this is the number of times theprocessBlock(byte[])method has been invoked for the current hash operation.protected intgetInternalBlockLength()Get the internal block length.protected abstract voidprocessBlock(byte[] data)Process one block of data.voidreset()Reset the object: this makes it suitable for a new hash computation.voidupdate(byte input)Insert one more input data byte.voidupdate(byte[] input)Insert some more bytes.voidupdate(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, waitMethods inherited from interface fr.cryptohash.Digest
copy, getBlockLength, getDigestLength, toString
-
Constructor Details
-
DigestEngine
public DigestEngine()Instantiate the engine.
-
-
Method Details
-
engineReset
protected abstract void engineReset()Reset the hash algorithm state. -
processBlock
protected abstract void processBlock(byte[] data)Process one block of data.- Parameters:
data- the data block
-
doPadding
protected abstract void doPadding(byte[] buf, int off)Perform the final padding and store the result in the provided buffer. This method shall callflush()and thenupdate(byte)with the appropriate padding data in order to get the full input data.- Parameters:
buf- the output bufferoff- the output offset
-
doInit
protected abstract void doInit()This function is called at object creation time; the implementation should use it to perform initialization tasks. After this method is called, the implementation should be ready to process data or meaningfully honour calls such asDigest.getDigestLength(). -
digest
public byte[] digest()Description copied from interface:DigestFinalize the current hash computation and return the hash value in a newly-allocated array. The object is resetted. -
digest
public byte[] digest(byte[] input)Description copied from interface:DigestInput some bytes, then finalize the current hash computation and return the hash value in a newly-allocated array. The object is resetted. -
digest
public int digest(byte[] buf, int offset, int len)Description copied from interface:DigestFinalize the current hash computation and store the hash value in the provided output buffer. Thelenparameter contains the maximum number of bytes that should be written; no more bytes than the natural hash function output length will be produced. Iflenis smaller than the natural hash output length, the hash output is truncated to its firstlenbytes. The object is resetted. -
reset
public void reset()Description copied from interface:DigestReset the object: this makes it suitable for a new hash computation. The current computation, if any, is discarded. -
update
public void update(byte input)Description copied from interface:DigestInsert one more input data byte. -
update
public void update(byte[] input)Description copied from interface:DigestInsert some more bytes. -
update
public void update(byte[] input, int offset, int len)Description copied from interface:DigestInsert some more bytes. -
getInternalBlockLength
protected int getInternalBlockLength()Get the internal block length. This is the length (in bytes) of the array which will be passed as parameter toprocessBlock(byte[]). The default implementation of this method callsDigest.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.- Returns:
- the internal block length (in bytes)
-
flush
protected final int flush()Flush internal buffers, so that less than a block of data may at most be upheld.- Returns:
- the number of bytes still unprocessed after the flush
-
getBlockBuffer
protected final byte[] getBlockBuffer()Get a reference to an internal buffer with the same size than a block. The contents of that buffer are defined only immediately after a call toflush(): ifflush()return the valuen, then the firstnbytes of the array returned by this method are thenbytes of input data which are still unprocessed. The values of the remaining bytes are undefined and may be altered at will.- Returns:
- a block-sized internal buffer
-
getBlockCount
protected long getBlockCount()Get the "block count": this is the number of times theprocessBlock(byte[])method has been invoked for the current hash operation. That counter is incremented after the call toprocessBlock(byte[]).- Returns:
- the block count
-
copyState
This function copies the internal buffering state to some other instance of a class extendingDigestEngine. It returns a reference to the copy. This method is intended to be called by the implementation of theDigest.copy()method.- Parameters:
dest- the copy- Returns:
- the value
dest
-