aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaldhar H. Vyas <jaldhar@braincells.com>2023-05-11 18:09:45 -0400
committerJaldhar H. Vyas <jaldhar@braincells.com>2023-05-11 18:09:45 -0400
commit50d208c809608bde0a601e6accd34d91dd0df260 (patch)
treed4fdf85d23dde51e6ab54e6ec89c9b4da4f3afae
parent35bd37bf4e900cc315a73a4f216c3b8018a1f027 (diff)
downloadperlweeklychallenge-club-50d208c809608bde0a601e6accd34d91dd0df260.tar.gz
perlweeklychallenge-club-50d208c809608bde0a601e6accd34d91dd0df260.tar.bz2
perlweeklychallenge-club-50d208c809608bde0a601e6accd34d91dd0df260.zip
Challenge 16 Problem 2 plus blog by Jaldhar H. Vyas.
-rw-r--r--challenge-016/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-016/jaldhar-h-vyas/perl5/ch-2.pl37
-rwxr-xr-xchallenge-016/jaldhar-h-vyas/perl6/ch-2.p633
3 files changed, 71 insertions, 0 deletions
diff --git a/challenge-016/jaldhar-h-vyas/blog.txt b/challenge-016/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..b3a6af9f47
--- /dev/null
+++ b/challenge-016/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2023/05/perl_weekly_challenge_week_16.html \ No newline at end of file
diff --git a/challenge-016/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-016/jaldhar-h-vyas/perl5/ch-2.pl
new file mode 100755
index 0000000000..f83ec5c488
--- /dev/null
+++ b/challenge-016/jaldhar-h-vyas/perl5/ch-2.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+use Digest::SHA qw/ sha256 /;
+
+sub fromBase58 {
+ my ($address) = @_;
+
+ my %digits;
+ @digits{( 1 .. 9, 'A' .. 'H', 'J' .. 'N', 'P' .. 'Z', 'a' .. 'k',
+ 'm' .. 'z')} = 0 .. 57;
+
+ my @result;
+
+ for my $c ( map { $digits{$_} } split //, $address ) {
+ for (my $j = 25; $j--; ) {
+ $c += 58 * ($result[$j] // 0);
+ $result[$j] = $c % 256;
+ $c /= 256;
+ }
+ }
+
+ return @result;
+}
+
+sub isValidBitcoinAddress {
+ my ($address) = @_;
+
+ my @bytes = fromBase58($address);
+ my $decoded = sha256(sha256 pack 'C*', @bytes[0..20]);
+ my $checksum = pack 'C*', @bytes[21..24];
+
+ return $checksum eq substr $decoded, 0, 4;
+}
+
+say isValidBitcoinAddress(shift) ? q{} : 'in', 'valid';
diff --git a/challenge-016/jaldhar-h-vyas/perl6/ch-2.p6 b/challenge-016/jaldhar-h-vyas/perl6/ch-2.p6
new file mode 100755
index 0000000000..8a10ee17f7
--- /dev/null
+++ b/challenge-016/jaldhar-h-vyas/perl6/ch-2.p6
@@ -0,0 +1,33 @@
+#!/usr/bin/perl6
+use Digest::SHA256::Native;
+
+sub fromBase58($address) {
+ my %digits = ( 1 .. 9, 'A' .. 'H', 'J' .. 'N', 'P' .. 'Z', 'a' .. 'k',
+ 'm' .. 'z').flat Z=> 0 .. 57;
+
+ my @result;
+
+ for $address.comb.map({ %digits{$_} }) -> $digit {
+ my $c = $digit;
+ for 24 ... 0 -> $j {
+ $c += (58 * (@result[$j] // 0));
+ @result[$j] = $c % 256;
+ $c div= 256;
+ }
+ }
+
+ return @result;
+}
+
+sub isValidBitcoinAddress($address) {
+ my @bytes = fromBase58($address);
+ my $decoded = sha256(sha256(Blob.new(@bytes[0..20])));
+ my $checksum = Blob.new(@bytes[21..24]);
+ return $checksum eqv $decoded.subbuf(0, 4);
+}
+
+sub MAIN (
+ $address #= a bitcoin address to validate.
+) {
+ say isValidBitcoinAddress($address) ?? q{} !! 'in', 'valid';
+} \ No newline at end of file