Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revised Compression Method #2

Merged
merged 5 commits into from
May 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions humanhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import operator
import uuid as uuidlib
import math
import sys

if sys.version_info.major == 3:
Expand Down Expand Up @@ -103,7 +104,7 @@ def humanize_list(self, hexdigest, words=4):

>>> digest = '60ad8d0d871b6095808297'
>>> HumanHasher().humanize_list(digest)
['sodium', 'magnesium', 'nineteen', 'hydrogen']
['equal', 'monkey', 'lake', 'beryllium']
"""
# Gets a list of byte values between 0-255.
bytes_ = map(lambda x: int(x, 16),
Expand All @@ -122,11 +123,11 @@ def humanize(self, hexdigest, words=4, separator='-'):

>>> digest = '60ad8d0d871b6095808297'
>>> HumanHasher().humanize(digest)
'sodium-magnesium-nineteen-hydrogen'
'equal-monkey-lake-beryllium'
>>> HumanHasher().humanize(digest, words=6)
'hydrogen-pasta-mississippi-august-may-lithium'
'sodium-magnesium-nineteen-william-alanine-nebraska'
>>> HumanHasher().humanize(digest, separator='*')
'sodium*magnesium*nineteen*hydrogen'
'equal*monkey*lake*beryllium'
"""
# Map the compressed byte values through the word list.
return separator.join(self.humanize_list(hexdigest, words))
Expand All @@ -139,31 +140,38 @@ def compress(bytes_, target):

>>> bytes_ = [96, 173, 141, 13, 135, 27, 96, 149, 128, 130, 151]
>>> list(HumanHasher.compress(bytes_, 4))
[205, 128, 156, 96]
[64, 145, 117, 21]

Attempting to compress a smaller number of bytes to a larger number is
an error:
If there are less than the target number bytes, return input bytes

>>> HumanHasher.compress(bytes_, 15) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: Fewer input bytes than requested output
>>> list(HumanHasher.compress(bytes_, 15)) # doctest: +ELLIPSIS
[96, 173, 141, 13, 135, 27, 96, 149, 128, 130, 151]
"""

bytes_list = list(bytes_)

length = len(bytes_list)
if target > length:
raise ValueError("Fewer input bytes than requested output")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, why did you remove this check?

I might flip this check back in the next version.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this check to make the function more generous in what it accepts. I changed the comments to reflect the change. My thinking is that the purpose of compression is to ensure that there are no more than the target number of bytes, which is satisfied if target > length. So now if target > length, the original bytes are returned since there is no compression needed.

I do understand that this is a different behavior. The impact on end users is that hashing small inputs will not cause an error, which I think is more intuitive. Another solution would be to pad input bytes up to the target number, but this would lead to padded final outputs.

For example:

digest = '010203'

# Old method
humanhash.humanize(digest, 6)
Traceback (most recent call last):
...
ValueError: Fewer input bytes than requested output

# New method
humanhash.humanize(digest, 6)
'alabama-alanine-alaska'

# Padding method
humanhash.humanize(digest, 6)
'alabama-alanine-alaska-ack-ack-ack'

It's your call, I'm happy to help if I can. :)


# Split `bytes` into `target` segments.
seg_size = length // target
segments = [bytes_list[i * seg_size:(i + 1) * seg_size]
for i in range(target)]
# Catch any left-over bytes in the last segment.
segments[-1].extend(bytes_list[target * seg_size:])

return map(checksum, segments)
# If there are less than the target number bytes, return input bytes
if target >= length:
return bytes_

# Split `bytes` evenly into `target` segments
# Each segment hashes `seg_size` bytes, rounded down for some
seg_size = float(length) / float(target)
# Initialize `target` number of segments
segments = [0] * target
seg_num = 0

# Use a simple XOR checksum-like function for compression
for i, byte in enumerate(bytes_list):
# Divide the byte index by the segment size to assign its segment
# Floor to create a valid segment index
# Min to ensure the index is within `target`
seg_num = min(int(math.floor(i / seg_size)), target-1)
# Apply XOR to the existing segment and the byte
segments[seg_num] = operator.xor(segments[seg_num], byte)

return segments

def uuid(self, **params):

Expand Down