Source

p5-rdf-crypt / lib / RDF / Crypt / Role / WithPrivateKey.pm

Full commit
package RDF::Crypt::Role::WithPrivateKey;

use 5.010;
use Any::Moose 'Role';

use Crypt::OpenSSL::RSA qw[];
use File::Slurp qw[slurp];

use namespace::clean;

BEGIN {
	$RDF::Crypt::Role::WithPrivateKey::AUTHORITY = 'cpan:TOBYINK';
	$RDF::Crypt::Role::WithPrivateKey::VERSION   = '0.001';
}

has private_key => (
	is        => 'ro',
	isa       => 'Crypt::OpenSSL::RSA',
	required  => 1,
);

sub new_from_file
{
	my ($class, $key_file) = @_;
	return $class->new_from_string(scalar slurp($key_file));
}

sub new_from_string
{
	my ($class, $key_string) = @_;
	my $key = Crypt::OpenSSL::RSA->new_private_key($key_string);
	return $class->new_from_privkey($key);
}

sub new_from_privkey
{
	my ($class, $key) = @_;
	$key->use_pkcs1_padding;
	# OpenSSL command-line tool defaults to this...
	$key->use_md5_hash;	
	$class->new(private_key => $key);
}

1;