Welcome, guest | Sign In | My Account | Store | Cart

This recipe allows messages to be encoded in "Bubble Babble" format. It also includes an additional function to encode message digests (sha, md5) in this same format.

Python, 63 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import sha
import md5

VOWELS = list('aeiouy')
CONSONANTS = list('bcdfghklmnprstvzx')

def bubble_babble(message):
    """Encode a message in bubblebabble format.                                                                
                                                                                                               
    The Bubble Babble Binary Data Encoding                                                                     
    ftp://ftp.ietf.org/ietf-mail-archive/secsh/2001-08.mail                                                    
                                                                                                               
    The Bubble Babble Encoding encodes arbitrary binary data into                                              
    pseudowords that are more natural to humans and that can be                                                
    pronounced relatively easily.                                                                              
                                                                                                               
    Code is an adapted from the following Perl module: Digest::BubbleBabble v0.01                              
    http://search.cpan.org/~btrott/Digest-BubbleBabble-0.01/BubbleBabble.pm                                    
                                                                                                               
    """

    seed  = 1
    mlen = len(message)
    mval = [ord(str(x)) for x in message]
    rounds = mlen//2 + 1
    encparts = ['x']
    eextend = encparts.extend
    for i in xrange(rounds):
        if (i+1 < rounds) or (mlen % 2 != 0):
            imval2i = int(mval[2*i])
            idx0 = (((imval2i >> 6) & 3) + seed) % 6
            idx1 = (imval2i >> 2) & 15
            idx2 = ((imval2i & 3) + seed//6) % 6
            eextend([VOWELS[idx0],CONSONANTS[idx1],VOWELS[idx2]])
            if (i+1 < rounds):
                imval2i1 = int(mval[2*i + 1])
                idx3 = (imval2i1 >> 4) & 15
                idx4 = imval2i1 & 15
                eextend([CONSONANTS[idx3],'-',CONSONANTS[idx4]])
                seed = (seed*5 + imval2i*7 + imval2i1) % 36
        else:
            idx0 = seed % 6
            idx1 = 16
            idx2 = seed//6
            eextend([VOWELS[idx0],CONSONANTS[idx1],VOWELS[idx2]])
    eextend(['x'])
    encoded = ''.join(encparts)
    return encoded


def digestbb(message, algo=sha):
    """Compute the digest of a message in Bubble Babble format."""
    mdigest = algo.new()
    mdigest.update(message)
    return bubble_babble(mdigest.digest())


if __name__ == '__main__':
    assert bubble_babble('') == 'xexax'
    assert bubble_babble('1234567890') == 'xesef-disof-gytuf-katof-movif-baxux'
    assert bubble_babble('Pineapple') == 'xigak-nyryk-humil-bosek-sonax'
    assert digestbb('foo',algo=sha) == 'xedov-vycir-hopof-zofot-radoh-tofyt-gezuf-sikus-dotet-pydif-faxux'
    assert digestbb('foo',algo=md5) == 'xorar-takyt-rufys-davuh-suruv-zinog-zifos-genet-moxix'

This recipe is useful in creating a message digest that is relatively easy to remember and communicate to others. The "words" comprising the encoded digest are not necessarily real words, but they look more like words than a string of hex characters.

According to the memo "The Bubble Babble Binary Data Encoding" at the following site: ftp://ftp.ietf.org/ietf-mail-archive/secsh/2001-08.mail ,

"The Bubble Babble Encoding encodes arbitrary binary data into pseudowords that are more natural to humans and that can be pronounced relatively easily."

The code in this recipe is adapted from the following Perl module: Digest::BubbleBabble v0.01 http://search.cpan.org/~btrott/Digest-BubbleBabble-0.01/BubbleBabble.pm

I have attempted to "improve" the original Perl implementation throught the use of several Python idioms, but I am sure further improvements are possible.

2 comments

Oren Tirosh 18 years, 11 months ago  # | flag

Just use .digest()! Why not use .digest() instead of getting the .hexdigest() and converting it with a2b_hex?

gyro funch (author) 18 years, 11 months ago  # | flag

Indeed. Thanks. I've updated the recipe.

Created by gyro funch on Tue, 3 May 2005 (PSF)
Python recipes (4591)
gyro funch's recipes (6)

Required Modules

Other Information and Tasks