HEAT-Crypto-X25519
view release on metacpan or search on metacpan
lib/HEAT/Crypto.pm view on Meta::CPAN
package HEAT::Crypto;
use strict;
use warnings;
use Carp;
use HEAT::Crypto::X25519;
use Crypt::Mode::CBC;
use Exporter qw(import);
our @EXPORT_OK = qw(hash heat_key get_private_key keygen priv_to_pub_key
shared_key sign verify encrypt decrypt account_id random_bytes tohex unhex);
our $VERSION = '0.04';
my $cbc = Crypt::Mode::CBC->new('AES');
*hash = \&HEAT::Crypto::X25519::hash;
sub tohex($)
{
return undef unless defined $_[0];
lib/HEAT/Crypto.pm view on Meta::CPAN
croak('undefined key spec');
} elsif (length $spec == HEAT::Crypto::X25519::KEYSIZE) {
return $spec;
} elsif ($spec =~ /^[[:xdigit:]]{64}$/) {
return unhex($spec);
} else {
croak('invalid key spec: %q', $spec);
}
}
sub get_private_key($)
{
my $spec = shift;
if ($spec =~ /^([a-z]{3,12}( |\Z)){12}$/) {
return HEAT::Crypto::X25519::keyhash($spec);
} else {
return heat_key($spec);
}
}
sub priv_to_pub_key($)
{
my $p = get_private_key($_[0]);
my $r = HEAT::Crypto::X25519::keygen($p);
return tohex($r->{p});
}
sub account_id($)
{
my $k = heat_key($_[0]);
my $h = hash($k);
my ($id, $t1, $t2) = (0);
lib/HEAT/Crypto.pm view on Meta::CPAN
return {
p => tohex($r->{p}),
s => tohex($r->{s}),
k => tohex($r->{k}),
};
}
sub shared_key($$)
{
my $k = get_private_key($_[0]);
my $p = heat_key($_[1]);
return tohex(HEAT::Crypto::X25519::shared($k, $p));
}
sub sign($$)
{
my $k = get_private_key($_[0]);
my $msg = $_[1];
return tohex(HEAT::Crypto::X25519::sign($k, $msg));
}
sub verify($$$)
{
my ($s, $m) = @_;
my $k = heat_key($_[2]);
unless (defined $s) {
lib/HEAT/Crypto.pm view on Meta::CPAN
=item keygen( $seed );
Generate a new key pair. It returns a hash with 3 values:
{
p => <public key bytes>,
k => <private key bytes>,
s => <signing key bytes>,
}
=item shared_key( $private_key, $public_key );
Compute shared secret.
Returns the key as a hexadecimal string.
=item sign( $private_key, $message );
Sign message with the private key.
Returns the signature as a hexadecimal string.
=item verify( $signature, $message, $public_key );
Verifies the message signature against the public key.
Returns 1 on success.
lib/HEAT/Crypto.pm view on Meta::CPAN
In array context it returns the encryption nonce, initialization vector and
cypher text. In scalar context it concatenates them.
=item decrypt( $data, $key );
Decrypts data with the given key. Data is expected to be returned by encrypt();
It returns the decrypted data on success. This function might die in case of errors.
=item priv_to_pub_key( $private_key )
Derives the public key from the private key.
Returns the public key as a hexadecimal string.
=item account_id( $public_key )
Derives the account ID from the public key.
Returns an integer.
( run in 0.312 second using v1.01-cache-2.11-cpan-a5abf4f5562 )