File Coverage

blib/lib/Encode/Base58/BigInt.pm
Criterion Covered Total %
statement 35 35 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 46 48 95.8


line stmt bran cond sub pod time code
1             package Encode::Base58::BigInt;
2 3     3   3334 use strict;
  3         5  
  3         107  
3 3     3   16 use warnings;
  3         6  
  3         127  
4              
5             our $VERSION = '0.03';
6 3     3   8976 use Math::BigInt;
  3         91992  
  3         21  
7 3     3   54990 use Carp;
  3         8  
  3         308  
8              
9 3     3   19 use base qw(Exporter);
  3         8  
  3         2023  
10             our @EXPORT = qw( encode_base58 decode_base58 );
11              
12             # except 0 O D / 1 l I
13             my $chars = [qw(
14             1 2 3 4 5 6 7 8 9
15             a b c d e f g h i
16             j k m n o p q r s
17             t u v w x y z A B
18             C D E F G H J K L
19             M N P Q R S T U V
20             W X Y Z
21             )];
22              
23             my $reg = qr/^[@{[ join "", @$chars ]}]+$/;
24              
25             my $map = do {
26             my $i = 0;
27             +{ map { $_ => $i++ } @$chars };
28             };
29              
30             sub encode_base58 {
31 510     510 0 868 my ($num) = @_;
32 510 100       1068 return $chars->[0] unless $num;
33              
34 509         2000 $num = Math::BigInt->new($num);
35              
36 509         24563 my $res = '';
37 509         915 my $base = @$chars;
38              
39 509         1760 while ($num->is_pos) {
40 3078         112187 my ($quo, $rem) = $num->bdiv($base);
41 3078         537059 $res = $chars->["$rem"] . $res;
42             }
43              
44 509         19825 $res;
45             }
46              
47             sub decode_base58 {
48 519     519 0 886 my $str = shift;
49 519         1172 $str =~ tr/0OlI/DD11/;
50 519 100       4202 $str =~ $reg or croak "Invalid Base58";
51              
52 517         1971 my $decoded = Math::BigInt->new(0);
53 517         45261 my $multi = Math::BigInt->new(1);
54 517         14129 my $base = @$chars;
55              
56 517         1310 while (length $str > 0) {
57 3098         252916 my $digit = chop $str;
58 3098         7907 $decoded->badd($multi->copy->bmul($map->{$digit}));
59 3098         504682 $multi->bmul($base);
60             }
61              
62 517         62393 "$decoded";
63             }
64              
65             1;
66             __END__