File Coverage

blib/lib/Encode/Base58.pm
Criterion Covered Total %
statement 27 27 100.0
branch 1 2 50.0
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 33 36 91.6


line stmt bran cond sub pod time code
1             package Encode::Base58;
2              
3 2     2   2323 use strict;
  2         5  
  2         85  
4 2     2   128 use 5.008_001;
  2         10  
  2         127  
5             our $VERSION = '0.01';
6              
7 2     2   12 use base qw(Exporter);
  2         4  
  2         565  
8             our @EXPORT = qw( encode_base58 decode_base58 );
9              
10             my @alpha = qw(
11             1 2 3 4 5 6 7 8 9
12             a b c d e f g h i
13             j k m n o p q r s
14             t u v w x y z A B
15             C D E F G H J K L
16             M N P Q R S T U V
17             W X Y Z
18             );
19              
20             my $i = 0;
21             my %alpha = map { $_ => $i++ } @alpha;
22              
23             sub encode_base58 {
24 500     500 0 1010 my $num = shift;
25              
26 500 50       1861 return $alpha[0] if $num == 0;
27              
28 500         955 my $res = '';
29 500         952 my $base = @alpha;
30              
31 500         1632 while ($num > 0) {
32 3000         4103 my $remain = $num % $base;
33 3000         4954 $num = int($num / $base);
34 3000         34886 $res = $alpha[$remain] . $res;
35             }
36              
37 500         1936 return $res;
38             }
39              
40             sub decode_base58 {
41 500     500 0 944 my $str = shift;
42              
43 500         859 my $decoded = 0;
44 500         654 my $multi = 1;
45 500         1956 my $base = @alpha;
46              
47 500         1802 while (length $str > 0) {
48 3000         4631 my $digit = chop $str;
49 3000         7908 $decoded += $multi * $alpha{$digit};
50 3000         7525 $multi *= $base;
51             }
52              
53 500         2300 return $decoded;
54             }
55              
56             1;
57             __END__