You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.2 KiB
86 lines
2.2 KiB
#
|
|
# Intent:
|
|
# provide encoding routines
|
|
#
|
|
# Note:
|
|
# This work has been done during my time at Doctor I·T
|
|
#
|
|
# -- Copyright drit, 2021 --
|
|
#
|
|
BEGIN { if (-e $ENV{SITE}.'/lib') { use lib $ENV{SITE}.'/lib'; } }
|
|
#
|
|
package encode;
|
|
require Exporter;
|
|
@ISA = qw(Exporter);
|
|
# Subs we export by default.
|
|
@EXPORT = qw();
|
|
# Subs we will export if asked.
|
|
#@EXPORT_OK = qw(nickname);
|
|
@EXPORT_OK = grep { $_ !~ m/^_/ && defined &$_; } keys %{__PACKAGE__ . '::'};
|
|
|
|
use strict;
|
|
use misc qw(version);
|
|
|
|
# The "use vars" and "$VERSION" statements seem to be required.
|
|
use vars qw/$dbug $VERSION/;
|
|
# ----------------------------------------------------
|
|
our $VERSION = sprintf "%d.%02d", q$Revision: 0.0 $ =~ /: (\d+)\.(\d+)/;
|
|
my ($State) = q$State: Exp $ =~ /: (\w+)/; our $dbug = ($State eq 'dbug')?1:0;
|
|
# ----------------------------------------------------
|
|
$VERSION = &version(__FILE__) unless ($VERSION ne '0.00');
|
|
|
|
sub mbase58 {
|
|
my $mh = sprintf'Z%s',&encode_base58f(@_);
|
|
return $mh;
|
|
}
|
|
sub mbase36 {
|
|
my $mh = sprintf'K%s',uc&encode_base36(@_);
|
|
return $mh;
|
|
}
|
|
sub mbase16 {
|
|
my $mh = sprintf'f%s',unpack'H*',@_;
|
|
return $mh;
|
|
}
|
|
|
|
|
|
sub encode_base36 {
|
|
use Math::BigInt;
|
|
use Math::Base36 qw();
|
|
my $n = Math::BigInt->from_bytes(shift);
|
|
my $k36 = Math::Base36::encode_base36($n,@_);
|
|
#$k36 =~ y,0-9A-Z,A-Z0-9,;
|
|
return $k36;
|
|
}
|
|
sub decode_base36 {
|
|
use Math::BigInt;
|
|
use Math::Base36 qw();
|
|
#$k36 = uc($_[0])
|
|
#$k36 =~ y,A-Z0-9,0-9A-Z;
|
|
my $n = Math::Base36::decode_base36($_[0]);
|
|
my $bin = Math::BigInt->new($n)->as_bytes();
|
|
return $bin;
|
|
}
|
|
|
|
sub encode_base58f { # flickr
|
|
use Math::BigInt;
|
|
use Encode::Base58::BigInt qw();
|
|
my $bin = join'',@_;
|
|
my $bint = Math::BigInt->from_bytes($bin);
|
|
my $h58 = Encode::Base58::BigInt::encode_base58($bint);
|
|
# $h58 =~ tr/a-km-zA-HJ-NP-Z/A-HJ-NP-Za-km-z/; # btc
|
|
return $h58;
|
|
}
|
|
sub decode_base58f {
|
|
use Math::BigInt;
|
|
use Encode::Base58::BigInt qw();
|
|
my $s = $_[0];
|
|
#$s =~ tr/A-HJ-NP-Za-km-zIO0l/a-km-zA-HJ-NP-ZiooL/; # btc
|
|
$s =~ tr/IO0l/iooL/; # forbidden chars
|
|
my $bint = Encode::Base58::BigInt::decode_base58($s);
|
|
my $bin = Math::BigInt->new($bint)->as_bytes();
|
|
return $bin;
|
|
}
|
|
|
|
# -----------------------------------------------------------------------
|
|
1; # $Source: /my/perl/modules/encode.pm $
|
|
|