File Coverage

blib/lib/Mail/DKIM/Algorithm/ed25519_sha256.pm
Criterion Covered Total %
statement 36 36 100.0
branch 4 6 66.6
condition n/a
subroutine 10 10 100.0
pod 2 4 50.0
total 52 56 92.8


line stmt bran cond sub pod time code
1             package Mail::DKIM::Algorithm::ed25519_sha256;
2 14     14   94 use strict;
  14         27  
  14         440  
3 14     14   70 use warnings;
  14         28  
  14         571  
4             our $VERSION = '1.20230630'; # VERSION
5             # ABSTRACT: ed25519 sha256 algorithm class
6              
7             # Copyright 2005-2006 Messiah College. All rights reserved.
8             # Jason Long
9              
10             # Copyright (c) 2004 Anthony D. Urso. All rights reserved.
11             # This program is free software; you can redistribute it and/or
12             # modify it under the same terms as Perl itself.
13              
14 14     14   72 use base 'Mail::DKIM::Algorithm::Base';
  14         29  
  14         1299  
15 14     14   88 use Carp;
  14         28  
  14         781  
16 14     14   96 use MIME::Base64;
  14         29  
  14         686  
17 14     14   81 use Digest::SHA;
  14         27  
  14         4348  
18              
19             sub init_digests {
20 8     8 0 20 my $self = shift;
21              
22             # initialize a SHA-256 Digest
23 8         55 $self->{header_digest} = new Digest::SHA(256);
24 8         130 $self->{body_digest} = new Digest::SHA(256);
25             }
26              
27             sub sign {
28 2     2 1 5 my $self = shift;
29 2 50       7 croak 'wrong number of arguments' unless ( @_ == 1 );
30 2         4 my ($private_key) = @_;
31              
32 2         13 my $digest = $self->{header_digest}->digest;
33 2         7 my $signature = $private_key->sign_digest( 'SHA-256', $digest );
34              
35 2         16 return encode_base64( $signature, '' );
36             }
37              
38             sub verify {
39 3     3 1 10 my $self = shift;
40 3 50       8 croak 'wrong number of arguments' unless ( @_ == 0 );
41              
42 3         14 my $base64 = $self->signature->data;
43 3         11 my $public_key = $self->signature->get_public_key;
44              
45 3         24 my $digest = $self->{header_digest}->digest;
46 3         13 my $sig = decode_base64($base64);
47              
48 3 100       14 return unless $public_key->verify_digest( 'SHA-256', $digest, $sig );
49 2         20 return $self->check_body_hash;
50             }
51              
52             sub wants_pre_signature_headers {
53 6     6 0 18 return 1;
54             }
55              
56             1;
57              
58             __END__