summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/Digest/BubbleBabble.pm
blob: 10a954d0588f7b623c23890566391d3e5bebc8f7 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package Digest::BubbleBabble;
use strict;

use Exporter;
use vars qw( @EXPORT_OK @ISA $VERSION );
@ISA = qw( Exporter );
@EXPORT_OK = qw( bubblebabble );

$VERSION = '0.01';

use vars qw( @VOWELS @CONSONANTS );
@VOWELS = qw( a e i o u y );
@CONSONANTS = qw( b c d f g h k l m n p r s t v z x );

sub bubblebabble {
    my %param = @_;
    my @dgst = map ord, split //, $param{Digest};
    my $dlen = length $param{Digest};

    my $seed = 1;
    my $rounds = ($dlen / 2) + 1;
    my $retval = 'x';
    for my $i (0..$rounds-1) {
        if ($i+1 < $rounds || $dlen % 2) {
            my $idx0 = ((($dgst[2 * $i] >> 6) & 3) + $seed) % 6;
            my $idx1 = ($dgst[2 * $i] >> 2) & 15;
            my $idx2 = (($dgst[2 * $i] & 3) + $seed / 6) % 6;
            $retval .= $VOWELS[$idx0] . $CONSONANTS[$idx1] . $VOWELS[$idx2];
            if ($i+1 < $rounds) {
                my $idx3 = ($dgst[2 * $i + 1] >> 4) & 15;
                my $idx4 = $dgst[2 * $i + 1] & 15;
                $retval .= $CONSONANTS[$idx3] . '-' . $CONSONANTS[$idx4];
                $seed = ($seed * 5 + $dgst[2 * $i] * 7 +
                        $dgst[2 * $i + 1]) % 36;
            }
        }
        else {
            my $idx0 = $seed % 6;
            my $idx1 = 16;
            my $idx2 = $seed / 6;
            $retval .= $VOWELS[$idx0] . $CONSONANTS[$idx1] . $VOWELS[$idx2];
        }
    }
    $retval .= 'x';
    $retval;
}

1;
__END__

=head1 NAME

Digest::BubbleBabble - Create bubble-babble fingerprints

=head1 SYNOPSIS

    use Digest::BubbleBabble qw( bubblebabble );
    use Digest::SHA1 qw( sha1 );

    my $fingerprint = bubblebabble( Digest => sha1($message) );

=head1 DESCRIPTION

I<Digest::BubbleBabble> takes a message digest (generated by
either of the MD5 or SHA-1 message digest algorithms) and creates
a fingerprint of that digest in "bubble babble" format.
Bubble babble is a method of representing a message digest
as a string of "real" words, to make the fingerprint easier
to remember. The "words" are not necessarily real words, but
they look more like words than a string of hex characters.

Bubble babble fingerprinting is used by the SSH2 suite
(and, consequently, by I<Net::SSH::Perl>, the Perl SSH
implementation) to display easy-to-remember key fingerprints.
The key (a DSA or RSA key) is converted into a textual form,
digested using I<Digest::SHA1>, and run through I<bubblebabble>
to create the key fingerprint.

=head1 USAGE

I<Digest::BubbleBabble> conditionally exports one function called
I<bubblebabble>; to import the function you must choose to
import it, like this:

    use Digest::BubbleBabble qw( bubblebabble );

=head2 bubblebabble( Digest => $digest )

Currently takes only one pair of arguments, the key of
which must be I<Digest>, the value of which is the actual
message digest I<$digest>. You should generate this message
digest yourself using either I<Digest::MD5> of I<Digest::SHA1>.

Returns the bubble babble form of the digest.

=head1 AUTHOR & COPYRIGHTS

Benjamin Trott, ben@rhumba.pair.com

Except where otherwise noted, Digest::BubbleBabble is Copyright
2001 Benjamin Trott. All rights reserved. Digest::BubbleBabble is
free software; you may redistribute it and/or modify it under
the same terms as Perl itself.

=cut