| 1 | package Bitcoin::Crypto::Base58; | ||||||||||
| 2 | 32 | $Bitcoin::Crypto::Base58::VERSION = '4.005'; | |||||||||
| 3 | 65 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 65 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 65 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 65 | ▶ | use Crypt::Misc qw(encode_b58b decode_b58b); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 65 | ▶ | use Types::Common -sigs, -types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 65 | ▶ | use Bitcoin::Crypto::Util::Internal qw(hash256); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 65 | ▶ | use Bitcoin::Crypto::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 65 | ▶ | use Bitcoin::Crypto::Types -types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 27 | our @EXPORT_OK = qw( | |||||||||
| 15 | encode_base58 | ||||||||||
| 16 | encode_base58check | ||||||||||
| 17 | decode_base58 | ||||||||||
| 18 | decode_base58check | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | 27 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); | |||||||||
| 22 | |||||||||||
| 23 | 27 | my $CHECKSUM_SIZE = 4; | |||||||||
| 24 | |||||||||||
| 25 | sub verify_checksum | ||||||||||
| 26 | 155 | ▶ | { | ||||||||
|
Subroutine
verify_checksum: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 27 | 155 | my ($decoded) = @_; | |||||||||
| 28 | 155 | my $encoded_val = substr $decoded, 0, -$CHECKSUM_SIZE; | |||||||||
| 29 | 155 | my $checksum = substr $decoded, -$CHECKSUM_SIZE; | |||||||||
| 30 | 155 | return unpack('a' . $CHECKSUM_SIZE, hash256($encoded_val)) eq $checksum; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 27 | signature_for encode_base58 => ( | |||||||||
| 34 | positional => [ByteStr], | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | sub encode_base58 | ||||||||||
| 38 | 221 | ▶ | { | ||||||||
|
Subroutine
encode_base58: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 254 | my ($bytes) = @_; | |||||||||
| 40 | |||||||||||
| 41 | 254 | return encode_b58b($bytes); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 27 | signature_for encode_base58check => ( | |||||||||
| 45 | positional => [ByteStr], | ||||||||||
| 46 | ); | ||||||||||
| 47 | |||||||||||
| 48 | sub encode_base58check | ||||||||||
| 49 | 221 | ▶ | { | ||||||||
|
Subroutine
encode_base58check: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 254 | my ($bytes) = @_; | |||||||||
| 51 | |||||||||||
| 52 | 254 | my $checksum = pack('a' . $CHECKSUM_SIZE, hash256($bytes)); | |||||||||
| 53 | 254 | return encode_base58($bytes . $checksum); | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 27 | signature_for decode_base58 => ( | |||||||||
| 57 | positional => [Str], | ||||||||||
| 58 | ); | ||||||||||
| 59 | |||||||||||
| 60 | sub decode_base58 | ||||||||||
| 61 | 163 | ▶ | { | ||||||||
|
Subroutine
decode_base58: called CC=2 SCAR=6.9
| |||||||||||
| 62 | 201 | my ($base58encoded) = @_; | |||||||||
| 63 | |||||||||||
| 64 | 201 | my $decoded = decode_b58b($base58encoded); | |||||||||
| 65 | 201 | ▶ | Bitcoin::Crypto::Exception::Base58InputFormat->raise( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 'illegal characters in base58 string' | ||||||||||
| 67 | ) unless defined $decoded; | ||||||||||
| 68 | |||||||||||
| 69 | 198 | return $decoded; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 27 | signature_for decode_base58check => ( | |||||||||
| 73 | positional => [Str], | ||||||||||
| 74 | ); | ||||||||||
| 75 | |||||||||||
| 76 | sub decode_base58check | ||||||||||
| 77 | 157 | ▶ | { | ||||||||
|
Subroutine
decode_base58check: called CC=2 SCAR=6.9
| |||||||||||
| 78 | 162 | my ($base58encoded) = @_; | |||||||||
| 79 | |||||||||||
| 80 | 162 | my $decoded = decode_base58($base58encoded); | |||||||||
| 81 | 160 | ▶ | Bitcoin::Crypto::Exception::Base58InputChecksum->raise( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 'incorrect base58check checksum' | ||||||||||
| 83 | ) unless verify_checksum($decoded); | ||||||||||
| 84 | |||||||||||
| 85 | 155 | return substr $decoded, 0, -$CHECKSUM_SIZE; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 27 | 1; | |||||||||
| 89 | |||||||||||
| 90 | __END__ | ||||||||||