Anonymous user
Bitcoin/public point to address: Difference between revisions
→{{header|Perl}}: complete rewrite. Shorter, faster, less data munging
(Added Haskell version) |
(→{{header|Perl}}: complete rewrite. Shorter, faster, less data munging) |
||
Line 299:
=={{header|Perl}}==
Here we'll use the standard Digest::SHA module, and the CPAN-available Crypt::RIPEMD160 and Encode::Base58::GMP.
<lang perl>
use Crypt::RIPEMD160;
use Digest::SHA qw(sha256);
use Encode::Base58::GMP;
sub public_point_to_address {▼
my $ec = '04' . join '', @_; # EC: concat x and y to one string and prepend '04' magic value
my @byte = map { hex } unpack('(a2)65', $ec); # transform the hex values to list of decimal values
▲sub public_point_to_address {
my $octets = pack 'C*', @byte; # actually build the octet stream
my $hash = chr(0) . Crypt::RIPEMD160->hash(sha256
my $hex = '0x' . join '', # build hex value of hash and checksum
map { sprintf("%02X", ord) }
($hash.$checksum) =~ /./gs;
▲ my $hash = Crypt::RIPEMD160->hash(sha256 join '', map { chr } @byte);
return '1' . sprintf "%32s", encode_base58($hex, 'bitcoin'); # Do the Base58 encoding, prepend "1"
▲ my $checksum = substr sha256(sha256 chr(0).$hash), 0, 4;
}
'50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352',▼
'2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6'▼
;
▲50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352
▲2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6
</lang>
{{out}}
|