Last active
August 2, 2016 22:46
-
-
Save nealey/dcddfdf83cea21bf71e0d9f109c8c9af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
""" | |
Python implementation of "The Bubble Babble Binary Data Encoding" (draft-huima-01) | |
Please note that this is not a cryptographic hash, | |
it can be reversed. | |
""" | |
class BubbleBabble: | |
consonants = 'bcdfghklmnprstvz' | |
vowels = 'aeiouy' | |
def __init__(self): | |
self.seed = 1 | |
self.out = ["x"] | |
self.even = True | |
def update(self, buf): | |
for c in buf: | |
if self.even: | |
self.out.append(self.vowels[(((c >> 6) & 3) + self.seed) % 6]) | |
self.out.append(self.consonants[(c >> 2) & 15]) | |
self.out.append(self.vowels[((c & 3) + (self.seed // 6)) % 6]) | |
self.seed = (self.seed * 5) + (c * 7) | |
else: | |
self.seed = (self.seed + c) % 36 | |
self.out.append(self.consonants[(c >> 4) & 15]) | |
self.out.append('-') | |
self.out.append(self.consonants[(c & 15)]) | |
self.even = not self.even | |
return self | |
def digest(self): | |
out = self.out[:] | |
if self.even: | |
out.append(self.vowels[self.seed % 6]) | |
out.append('x') | |
out.append(self.vowels[self.seed // 6]) | |
out.append('x') | |
return ''.join(out) | |
def digest(buf): | |
return BubbleBabble().update(buf).digest() | |
def test(): | |
assert digest(b'') == 'xexax' | |
assert digest(b'1234567890') == 'xesef-disof-gytuf-katof-movif-baxux' | |
assert digest(b'Pineapple') == 'xigak-nyryk-humil-bosek-sonax' | |
if __name__ == '__main__': | |
import sys | |
test() | |
buf = sys.stdin.buffer.read() | |
print(digest(buf)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment