Source

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

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

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

use namespace::clean;
#use overload
#	q[""]    => 'to_string',
#	q[bool]  => sub { 1 },
#	fallback => 1;
use constant LENGTH => 72;

BEGIN {
	$RDF::Crypt::Role::ToString::AUTHORITY = 'cpan:TOBYINK';
	$RDF::Crypt::Role::ToString::VERSION   = '0.002';
}

sub to_string
{
	my ($self, $title) = @_;
	$title //= ref $self;
	
	my $str;
	$str .= ('=' x LENGTH) . "\n";
	$str .= $title . "\n";
	if ($self->can('private_key'))
	{
		$str .= $self->_key_to_string($self->private_key, 'Private Key');
	}
	if ($self->can('public_keys'))
	{
		my @keys = @{ $self->public_keys || [] };
		$str .= $self->_key_to_string($keys[$_], "Public Key $_") for 0 .. $#keys;
	}
	$str .= ('=' x LENGTH) . "\n";
	return $str;
}

sub _key_to_string
{
	my ($self, $key, $title) = @_;
	my $str;
	$str .= ('-' x LENGTH) . "\n";
	$str .= $title . "\n";
	$str .= $key->get_public_key_string;
	return $str;
}

1;