Skip to content

Commit

Permalink
Compression method comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lafncow committed May 9, 2017
1 parent 26b3692 commit 6ad5c71
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions humanhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,24 @@ def compress(bytes, target):
"""

length = len(bytes)
# If there are less than the target number bytes, the input bytes will be returned
if target >= length:
return bytes

# Split `bytes` into `target` segments.
# Split `bytes` evenly into `target` segments
# Each segment will be composed of `seg_size` bytes, rounded down for some segments
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.

# Use a simple XOR checksum-like function for compression
for i, byte in enumerate(bytes):
# Divide the byte index by the segment size to determine which segment to place it in
# 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
Expand Down

0 comments on commit 6ad5c71

Please sign in to comment.