How to Add a New Hash Function
Contents
- Introduction to the Library
- Basic support package (com.kryptel.bslx)
- Kryptel API Commons package (com.kryptel)
- Cipher package (com.kryptel.cipher)
- Compressor package (com.kryptel.compressor)
- Exceptions package (com.kryptel.exceptions)
- Hash function package (com.kryptel.hash_function)
- MAC function package (com.kryptel.mac)
- Key-related functions (com.kryptel.key)
- Silver Key engine (com.kryptel.silver_key)
- Kryptel encrypted storage (com.kryptel.storage)
Adding a New Hash Function
Class com.kryptel.hash_function.HashBase lets you easily add a new hash function to the library. The first step you need to do is to create a new CID (component ID). Add a new record to the com.kryptel.Guids class. Please remember that CIDs 11056249-400A-4461-BD5E-xxxxxxxxxxxx are reserved.
static public final UUID CID_MY_HASH_FUNCTION = UUID.fromString("95B249E2-95E9-4447-A2C2-DD6B76833486");
Now declare your hash function class by extending the com.kryptel.hash_function.HashBase class.
final class MyHashFunction extends HashBase {
Declare a static constant componentID containing the CID of your hash function:
static final UUID componentID = CID_MY_HASH_FUNCTION;
Declare a class constructor to initialize the HashFunctionInfo structure describing your hash function, and default hash function parameters:
MyHashFunction(long capabilities) { super(capabilities); DEFAULT_HASH_SIZE = 32; // 256 bits DEFAULT_PASSES = 1; DEFAULT_SCHEME = 1; hashFunctionInfo = new HashFunctionInfo( new int[] { 16, 20, 32 }, // Hash sizes new int[] { DEFAULT_PASSES }, // Passes new String[] { "Standard" } ); hashSize = DEFAULT_HASH_SIZE; hashPasses = DEFAULT_PASSES; hashScheme = DEFAULT_SCHEME; // Other construction code }
Implement IKryptelComponent methods, which HashBase does not implement.
public UUID ComponentID() { return componentID; } public String ComponentName() { return "My Cool Cipher"; }
The next step is adding the actual hash function implementation, i.e. overriding three abstract functions InitImpl, HashImpl, and DoneImpl:
protected void InitImpl() protected void HashImpl(byte[] buffer, int start, int size) protected byte[] DoneImpl()
If the hash function supports several hash sizes, passes, or schemes, the implementation functions should examine the variables hashSize, hashPasses, and hashScheme to get the currently set values. Keep in mind that schemes are numbered starting from 1.
The last step is adding a reference to the new hash function to com.kryptel.hash_function.ComponentLoader class. Add this code
if (cid.equals(CID_MY_HASH_FUNCTION)) return new MyHashFunction(capabilities);
to the CreateComponent function and the following line
if ((MyHashFunction.componentType & mask) != 0) uidList.add(MyHashFunction.componentID);
to the GetComponentList function.
Your new hash function component is now ready.