---+ ECDH
The purpose here is to be able to encrypt/decrypt data using key pairs derived from secp256k1.
{ my $root = CBitcoin::CBHD->generate("my magic seed! 123456789012345678901234567890");
# my ($private_key,$public_key) = ( # CBitcoin::CBHD::picocoin_offset_private_key($root->privatekey,"hello mother.") # ,CBitcoin::CBHD::picocoin_offset_public_key($root->publickey,"hello mother.") # );
my ($private_key,$public_key) = (
$root->privatekey
,$root->publickey
);
my $plaintext1 = 'Please encrypt me!';
open(my $fh1,'<',\$plaintext1);
my $readsub1 = sub{
my ($xref,$n) = @_;
return read($fh1,$$xref,$n);
};
my $ciphertext1 = '';
my $writesub1 = sub{
my ($xref) = @_;
$ciphertext1 .= $$xref;
return length($$xref);
};
my $header = CBitcoin::CBHD::encrypt($public_key,$readsub1,$writesub1);
########### decrypt##############
my $ciphertext2 = $ciphertext1;
open(my $fh2,'<',\$ciphertext2);
my $readsub2 = sub{
my ($xref,$n) = @_;
return read($fh2,$$xref,$n);
};
my $plaintext2 = '';
my $writesub2 = sub{
my ($xref) = @_;
$plaintext2 .= $$xref;
return length($$xref);
};
my $success = CBitcoin::CBHD::decrypt($private_key,$header,$readsub2,$writesub2);
ok($success,'did decryption work?');
ok($plaintext1 eq $plaintext2, 'did we get back the plain text?');
my $newstuff = CBitcoin::CBHD::picocoin_offset_private_key($root->privatekey,"hello mother.");
my ($priv,$pub) = (substr($newstuff,0,32),substr($newstuff,32));
my $pub2 = CBitcoin::CBHD::picocoin_offset_public_key($root->publickey,"hello mother.");
ok($pub eq $pub2, 'Is it possible to make a new private/public EC_Key using an arbitrary string as an offset?');
}