Skip to content

Commit

Permalink
Signer: Add a helper function for loading a private key file
Browse files Browse the repository at this point in the history
This keeps the key type determination logic in one place.
  • Loading branch information
dev-aaront-org committed Nov 14, 2024
1 parent 5e58fc0 commit 02c2f59
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/Mail/DKIM/Signer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,9 @@ sub init {
$self->{'Algorithm'} = 'rsa-sha1';
}

my $type = 'rsa'; # default
$type = 'ed25519' if ( $self->{'Algorithm'} =~ /^ed25519/ );

if ( defined $self->{KeyFile} ) {
$self->{Key} ||=
Mail::DKIM::PrivateKey->load( File => $self->{KeyFile},
Type => $type );
load_private_key( $self->{KeyFile}, $self->{Algorithm} );
}

unless ( $self->{'Method'} ) {
Expand Down Expand Up @@ -322,10 +318,7 @@ sub finish_body {
|| $self->{Key}
|| $self->{KeyFile};
if ( defined($key) && !ref($key) ) {
my $type = 'rsa'; # default
$type = 'ed25519' if ( $signature->algorithm =~ /^ed25519/ );
$key = Mail::DKIM::PrivateKey->load( File => $key,
Type => $type );
$key = load_private_key( $key, $signature->algorithm );
}
$key
or die "no key available to sign with\n";
Expand All @@ -342,6 +335,17 @@ sub finish_body {
}
}

# Load a private key file for the given algorithm.
sub load_private_key {
my $key_file = shift;
my $algorithm = shift;

my $type = 'rsa'; # default
$type = 'ed25519' if ( $algorithm =~ /^ed25519/ );

return Mail::DKIM::PrivateKey->load( File => $key_file, Type => $type );
}

=head1 METHODS
=head2 PRINT()
Expand Down

0 comments on commit 02c2f59

Please sign in to comment.