algorithms.hash_tables

Hash Chain

A hash chain is the successive application of a cryptographic hash function to a piece of data. In computer security, a hash chain is a method to produce many one-time keys from a single key or password. [Wikipedia]

class HashChain(bucket_count)[source]

Bases: object

Class HashChain realisation

Examples

>>> hash_chain = HashChain(5)
>>> hash_chain.add("world")
>>> hash_chain.add("HellO")
>>> hash_chain.check(4)
HellO world
>>> hash_chain.find("World")
no
>>> hash_chain.find("world")
yes
>>> hash_chain.delete("world")
>>> hash_chain.check(4)
HellO
>>> hash_chain.delete("HellO")
>>> hash_chain.add("luck")
>>> hash_chain.add("GooD")
>>> hash_chain.check(2)
GooD luck

Explanation: The ASCII code of ’w’ is 119, for ’o’ it is 111, for ’r’ it is 114, for ’l’ it is 108, and for ’d’ it is 100. Thus, h(“world”) = 4. It turns out that the hash value of “HellO“ is also 4. We always insert in the beginning of the chain, so after adding “world” and then “HellO” in the same chain index 4, first goes “HellO” and then goes “world”. Of course, “World” is not found, and “world” is found, because the strings are case-sensitive, and the codes of ’W’ and ’w’ are different. After deleting “world”, only “HellO” is found in the chain 4. Similarly to “world” and “HellO”, after adding “luck” and “GooD” to the same chain 2, first goes “GooD” and then “luck”.

add(data)[source]

Add data to hash table

Parameters:data – string data
check(idx)[source]

Check hash_chain by index

Parameters:idx – index in hash
Returns:String chain separated by space
delete(data)[source]

Delete data from hash table

Parameters:data – string data
find(data)[source]

Find data in hash table

Parameters:data – string data
Returns:True if found, False if not