Skip to content
Permalink
Browse files
Adding better documentation and a README guide to the project
  • Loading branch information
James Shuttleworth committed Sep 30, 2021
1 parent cbfd473 commit 67ffb7c92a98f52945dcdd2ac21851ee2b04c2a1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 17 deletions.
@@ -2,3 +2,4 @@
/src/__pycache__/
/rt-venv/
/src/answer.py
/html/
@@ -0,0 +1,55 @@
# Pearson Rainbow Table Generator

## Pearson Hash Function

The chosen hash function can create hashes of arbitrary length. Here,
we're using just two bytes to limit the size of tables required, but
the same methods would apply for larger hashes. You just need more
time and space.

## What's in the files?

### `src/pearson.py`

Here there are just two functions, and the only one of interest is
`hashN`.

This function, given a string (the argument called `message`) and a
target number of bytes (`nBytes`), creates a hash of that string that
will be exactly `nBytes` long.

You can use `asHex` if you want to turn bytes into nicely printable
hex pairs.

There's also a list of numbers. This is used to pseudo-randomise
bytes. The list is 256 items long, so if you take a byte and swap it
for the byte with value N you find in the list at location N, you have
somehting that looks random. By swaping bytes and including an ofset,
the apparent randomisation is increased because the same byte with a
different offset is not replaced with the same new value. [^1]

### `src/rMenu.py`

This is a simple menu system so you can use and test your work.

You're welcome to read it, but I don't recommend looking for good rpactice here. It's nothing more than a way to quickly access the rainbow table generation and querying funcitons.

It also lets you save and load tables, so you can save regenerating them constantly.

### `src/rainbow_generator.py`

This is the key file for the project.

Most of the work is done. The functions provided are:

- `targetHashFunction(value)` takes a value and returns the hash. It wraps the `hashN` function and adds some counting so we can later see how many times it was called and how many collisions[^2] were encountered.

- `makeGuess(...)` takes a hash value and creates a new input guess. It uses the pseudorandom number generator to select from the digits we have set as valid. It also limits the length of any guess between a given minimum and maximum.



## Footnotes

[^1]: That was a very basic explanation. The Cryptography module will get into this stuff more. Like, ensuring this "confusion", and how this kind of shuffling (effectively an [S-box](https://en.wikipedia.org/wiki/S-box) is used in more cryptosystems.

[^2]: How many times we produce a hash that has already been seen. Each time we have a collision, we are at a point where we add no value to the chain, since we already have a way to reverse this hash. Note that the values that produce the hash might not be the same (what we usually mean by a hash collision) but in a rainbow table, could be produced by accidentally generating the same guess twice in the table.
@@ -1 +1,2 @@
simple-term-menu
pdoc3
@@ -1,17 +1,16 @@

table=[27, 128, 105, 153, 231, 42, 147, 187, 20, 224, 154, 202, 39, 8, 7, 226, 46, 109, 35, 229, 104, 116, 0, 99, 121, 125, 122, 38, 5, 55, 160, 23, 30, 240, 157, 146, 102, 115, 189, 86, 158, 82, 176, 95, 2, 126, 124, 239, 103, 106, 54, 72, 123, 43, 241, 230, 161, 171, 73, 249, 3, 61, 78, 221, 223, 44, 174, 181, 156, 19, 74, 225, 214, 69, 197, 204, 71, 162, 219, 234, 4, 247, 248, 98, 66, 186, 205, 101, 93, 151, 75, 193, 167, 179, 208, 194, 94, 40, 235, 77, 62, 144, 76, 32, 100, 119, 152, 237, 107, 33, 142, 18, 88, 172, 163, 182, 227, 58, 199, 250, 50, 165, 228, 253, 24, 14, 45, 139, 64, 140, 213, 245, 215, 164, 97, 236, 89, 243, 159, 12, 168, 84, 37, 173, 141, 180, 177, 192, 134, 90, 110, 222, 191, 255, 129, 232, 188, 118, 87, 57, 21, 196, 242, 49, 47, 155, 195, 148, 92, 149, 15, 11, 96, 132, 170, 131, 1, 203, 135, 207, 127, 210, 178, 190, 51, 67, 220, 63, 211, 79, 185, 217, 13, 85, 68, 120, 34, 206, 22, 9, 201, 150, 56, 212, 29, 60, 183, 130, 200, 113, 36, 81, 218, 233, 244, 41, 26, 91, 216, 83, 112, 111, 48, 65, 25, 10, 108, 246, 136, 28, 133, 166, 145, 70, 117, 80, 31, 137, 251, 175, 209, 17, 6, 169, 184, 53, 114, 254, 138, 198, 16, 252, 238, 59, 143, 52]

#TODO: use byte strings

def hash8(message: str) -> str:
"""Pearson hashing."""
hash = len(message) % 256
for i in message:
hash = table[hash ^ ord(i)]
return ""+chr(hash)
def hashN(message: str, nBytes: int) -> str:
""" Return the Pearson Hash of a given message, with the given number of bytes.
Arguments:
def hashN(message: str, nBytes: int) -> str:
message -- the sting to be hashed
nBytes -- the number of bytes the hash should have
"""
retval=0

for j in range(nBytes-1):
@@ -23,19 +22,17 @@ def hashN(message: str, nBytes: int) -> str:

retval = ((retval << 8) | h)


# I think I shouldn't need to do it this way
# b= bytes("".join(chr((retval>>(8*i))&255) for i in range(nBytes-1)).encode())
# while len(b)<2:
# b=bytes("\x00".encode())+b
b=retval.to_bytes(2,"big")
return b






def asHex(v):
""" Return a string containing formatted hex digit pairs (bytes) such as "1C F2".
Arguments:
v -- sequence of bytes to be formatted
"""
return " ".join("{:02x}".format(c) for c in v).upper()


@@ -185,4 +185,4 @@ if __name__=="__main__":

## Ignore this:

joiIjmiPg4vcbp6Ius9thojGzWeLhID9e4ufmt0jyoWP3WesmIDNI8qKm8t8maubwGzGzY3GboODosthjZmGgi+HhIDiaoTBg893poiAgmyCjJzdap7E1KQvys3OymqImImTSYuBncsFys3OjmaMzYrLbZ+K1I5riIrT9VLgzc6OL56MjMJq15aTpC/Kzc7IYJjNh45mhM2cz2GNiMbCaoTFjcZug4O92m6YmZ2HJtDnzo4vys3Oji+Di87KaoiYiZQvmKmLzHqN0MyMBcrNzo4vys3O3m6Dn9+TJ4mFj8dhuZmP3HuZtofzI4KMncZJn4ONhmyCjIfAXJ6MnNp8sYSzhybgzc6OL8rNzo5sn5+cy2GevY/Hfdedj8d92+fOji/Kzc6OL4OLzspqiJiJlC+YqYvMeo3G0917mMWN232YiIDaX4uEnIcFys3Oji/Kzc7IYJjNhI5mhM2cz2GNiMbNZ4uEgOJqhIqaxibQ586OL8rNzo4vys3OjmGPlZrpeo+enZNon4id3Umfg42GbJ+fnMthnr2Px32x3LOCZcbNg8dhpoiAk2KDg6LLYcbNg893poiAk2KLlaLLYcbNjcZumJ6L2jKJhY/cfI+Zx6Qvys3Oji/Kzc6OL8qOm9x9j4Oa/m6Dn9OGYY+Vmul6j56dgmeLnoboeoSOxsBqkpmp22qZnseHBcrNzo4vys3Oji/KzYfIL46IjNto0M2c6mqImImFMpmZnIZs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