Vickie D. Weigleb
4 years ago
committed by
Brooke L. Rediker
11 changed files with 62 additions and 285 deletions
-
3.gitmodules
-
6_data/timelog.yml
-
86encode.pm
-
1lib
-
133misc.pm
-
2notes.md
-
50seed.pm
-
12signatures.pm
-
12t/blockaddr.t
-
2t/hashcash.t
-
40t/run.sh
@ -1,3 +1,6 @@ |
|||
[submodule "websocketd"] |
|||
path = websocketd |
|||
url = http://holoGit.ml.ipns.localhost:8080/wpreisser/websocketd.git |
|||
[submodule "lib"] |
|||
path = lib |
|||
url = https://hologit-ml.ipns.dweb.link/michelc/toychain/blockchain-lib.git |
@ -1,86 +0,0 @@ |
|||
# |
|||
# 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 $ |
|||
|
@ -1,133 +0,0 @@ |
|||
#!/usr/bin/perl |
|||
|
|||
# |
|||
# Intent: |
|||
# re-usable routines ... |
|||
# |
|||
# Note: |
|||
# This work has been done during my time at Doctor I·T |
|||
# |
|||
# -- Copyright drit, 2021 -- |
|||
# |
|||
package misc; |
|||
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; |
|||
|
|||
# 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 jsonify { |
|||
use JSON qw(encode_json); |
|||
my $obj = shift; |
|||
my $json = encode_json( $obj ); # /!\ keys are note sorted ! |
|||
return $json; |
|||
} |
|||
sub yamlify { |
|||
use YAML::Syck qw(Dump); |
|||
my $obj = shift; |
|||
my $yml = Dump($obj); |
|||
return $yml; |
|||
} |
|||
# ----------------------------------------------------------------------- |
|||
sub khash { # keyed hash |
|||
use Crypt::Digest qw(); |
|||
my $alg = shift; |
|||
my $data = join'',@_; |
|||
my $msg = Crypt::Digest->new($alg) or die $!; |
|||
$msg->add($data); |
|||
my $hash = $msg->digest(); |
|||
return $hash; |
|||
} |
|||
# ----------------------------------------------------------------------- |
|||
sub version { |
|||
#y ($atime,$mtime,$ctime) = (lstat($_[0]))[8,9,10]; |
|||
my @times = sort { $a <=> $b } (lstat($_[0]))[9,10]; # ctime,mtime |
|||
my $vtime = $times[-1]; # biggest time... |
|||
my $version = &rev($vtime); |
|||
|
|||
if (wantarray) { |
|||
my $shk = &get_shake(160,$_[0]); |
|||
print "$_[0] : shk:$shk\n" if $dbug; |
|||
my $pn = unpack('n',substr($shk,-4)); # 16-bit |
|||
my $build = &word($pn); |
|||
return ($version, $build); |
|||
} else { |
|||
return sprintf '%g',$version; |
|||
} |
|||
} |
|||
# ----------------------------------------------------------------------- |
|||
sub rev { |
|||
my ($sec,$min,$hour,$mday,$mon,$yy,$wday,$yday) = (localtime($_[0]))[0..7]; |
|||
my $rweek=($yday+&fdow($_[0]))/7; |
|||
my $rev_id = int($rweek) * 4; |
|||
my $low_id = int(($wday+($hour/24)+$min/(24*60))*4/7); |
|||
my $revision = ($rev_id + $low_id) / 100; |
|||
return (wantarray) ? ($rev_id,$low_id) : $revision; |
|||
} |
|||
# ----------------------------------------------------------------------- |
|||
sub fdow { |
|||
my $tic = shift; |
|||
use Time::Local qw(timelocal); |
|||
## 0 1 2 3 4 5 6 7 |
|||
#y ($sec,$min,$hour,$day,$mon,$year,$wday,$yday) |
|||
my $year = (localtime($tic))[5]; my $yr4 = 1900 + $year ; |
|||
my $first = timelocal(0,0,0,1,0,$yr4); |
|||
our $fdow = (localtime($first))[6]; |
|||
#printf "1st: %s -> fdow: %s\n",&hdate($first),$fdow; |
|||
return $fdow; |
|||
} |
|||
# ----------------------------------------------------------------------- |
|||
sub get_shake { # use shake 256 because of ipfs' minimal length of 20Bytes |
|||
use Crypt::Digest::SHAKE; |
|||
my $len = shift; |
|||
local *F; open F,$_[0] or do { warn qq{"$_[0]": $!}; return undef }; |
|||
#binmode F unless $_[0] =~ m/\.txt/; |
|||
my $msg = Crypt::Digest::SHAKE->new(256); |
|||
$msg->addfile(*F); |
|||
my $digest = $msg->done(($len+7)/8); |
|||
return $digest; |
|||
} |
|||
# ----------------------------------------------------------------------- |
|||
sub word { # 20^4 * 6^3 words (25bit worth of data ...) |
|||
use integer; |
|||
my $n = $_[0]; |
|||
my $vo = [qw ( a e i o u y )]; # 6 |
|||
my $cs = [qw ( b c d f g h j k l m n p q r s t v w x z )]; # 20 |
|||
my $str = ''; |
|||
if (1 && $n < 26) { |
|||
$str = chr(ord('a') +$n%26); |
|||
} else { |
|||
$n -= 6; |
|||
while ($n >= 20) { |
|||
my $c = $n % 20; |
|||
$n /= 20; |
|||
$str .= $cs->[$c]; |
|||
#print "cs: $n -> $c -> $str\n"; |
|||
$c = $n % 6; |
|||
$n /= 6; |
|||
$str .= $vo->[$c]; |
|||
#print "vo: $n -> $c -> $str\n"; |
|||
|
|||
} |
|||
if ($n > 0) { |
|||
$str .= $cs->[$n]; |
|||
} |
|||
return $str; |
|||
} |
|||
} |
|||
# ----------------------------------------------------------------------- |
|||
1; # $Source: /my/perl/modules/misc.pm $ |
|||
|
@ -1,50 +0,0 @@ |
|||
# |
|||
# Intent: |
|||
# entropy routines grasping source intention ! |
|||
# |
|||
# 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 seed; |
|||
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 setseed { |
|||
our $seed = undef; |
|||
# ecf7fa3d : 15min |
|||
$seed = ($_[0]) ? srand($_[0]) : srand(); |
|||
printf "seed: %08x\n",$seed; |
|||
return $seed; |
|||
} |
|||
# ----------------------------------------------------------------------- |
|||
sub rand64 { # /!\ NOT Cryptographycally safe |
|||
my $i1 = int(rand(0xFFFF_FFFF)); |
|||
my $i2 = int(rand(0xFFFF_FFFF)); |
|||
my $q = $i1 <<32 | $i2; |
|||
printf "i1: %08x\n",$i1; |
|||
printf "i2: %08x\n",$i2; |
|||
printf "rnd64: %s\n",unpack'H*',pack'Q',$q; |
|||
return $q; |
|||
} |
|||
# ----------------------------------------------------------------------- |
|||
1; # $Source: /my/perl/modules/pseudorandom.pm $ |
@ -1,12 +0,0 @@ |
|||
#!/usr/bin/perl |
|||
|
|||
|
|||
use Crypt::OpenSSL::ECDSA; |
|||
$sig = Crypt::OpenSSL::ECDSA::ECDSA_do_sign( $digest, $eckey ); |
|||
$r = $sig->get_r; |
|||
$s = $sig->get_s; |
|||
|
|||
$sig = Crypt::OpenSSL::ECDSA::ECDSA_SIG->new(); |
|||
$sig->set_r($r); |
|||
$sig->set_s($s); |
|||
Crypt::OpenSSL::ECDSA::ECDSA_do_verify( $digest, $sig, $eckey ); |
@ -0,0 +1,40 @@ |
|||
# |
|||
|
|||
intent="testing all subroutines ..." |
|||
|
|||
if echo -n "\e" | grep -q -e 'e'; then |
|||
e="-e" # bash needs a -e ! |
|||
fi |
|||
# colors : [see also](https://en.wikipedia.org/wiki/ANSI_escape_code) |
|||
nc=$(echo -n $e "\e[0m") |
|||
|
|||
export SITE='..'; |
|||
cp -p test.log test.log.1 |
|||
# savestdout and stderr |
|||
exec 3>&1 4>&2 |
|||
exec > test.log |
|||
exec 2>&1 |
|||
echo "--- ${0##*/}" |
|||
for tc in *.t; do |
|||
echo "tc: $tc" |
|||
perl $tc |
|||
echo '---' |
|||
done |
|||
echo '...' |
|||
|
|||
# restore stdout and stderr |
|||
exec 1>&3 2>&4 3>&- 4>&- |
|||
|
|||
ipfs add -n -q test.log |
|||
diff test.log test.log.1 |
|||
if [ $? = 0 ]; then |
|||
green=$(echo -n $e "\e[1;32m") |
|||
echo "${green}PASS${nc}" |
|||
else |
|||
red=$(echo -n $e "\e[31m") |
|||
echo "${red}FAIL${nc}" |
|||
exit 252 |
|||
fi |
|||
|
|||
|
|||
true; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue