Bitcoin/address validation: Difference between revisions

added perl solution
m (actually RIPEMD-160 is not needed for address validation)
(added perl solution)
Line 4:
 
You can use a digest library for [[SHA-256]].
 
==Perl==
<lang perl>our @b58 = qw{
1 2 3 4 5 6 7 8 9
A B C D E F G H J K L M N P Q R S T U V W X Y Z
a b c d e f g h i j k m n o p q r s t u v w x y z
};
our %b58 = map { $b58[$_] => $_ } 0 .. 57;
our $b58 = qr/[@{[join '', @b58]}]/x;
 
sub decode {
use bigint; use integer;
shift =~ m/$b58\Z/p ? $b58{${^MATCH}} + 58*decode(${^PREMATCH}) : 0
}
 
sub check_bitcoin_address {
use Digest::SHA qw(sha256 sha256_hex);
my $value = my $decode = decode my $addr = shift;
my @hash; for (1 .. 25) { push @hash, $value % 256; $value /= 256 }
@hash = reverse @hash;
my $checksum = join '', map { sprintf "%02x", $_ } @hash[21..24];
die unless $checksum eq
substr sha256_hex(sha256 pack 'C*', @hash[0..20]), 0, 8;
}</lang>
 
==Unix shell==
1,935

edits