| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | |
| 6 | use Test::More; |
| 7 | use Test::Exception; |
| 8 | |
| 9 | use Crypt::Util; |
| 10 | |
| 11 | my ( $c, $fallback_digest, $fallback_mac ); |
| 12 | |
| 13 | BEGIN { |
| 14 | $c = Crypt::Util->new; |
| 15 | |
| 16 | $fallback_digest = eval { $c->fallback_digest }; |
| 17 | plan skip_all => "Couldn't load any digest" if $@; |
| 18 | |
| 19 | $fallback_mac = eval { $c->fallback_mac }; |
| 20 | plan skip_all => "Couldn't load any mac" if $@; |
| 21 | |
| 22 | plan 'no_plan'; |
| 23 | } |
| 24 | |
| 25 | my $string = "magic moose"; |
| 26 | |
| 27 | my $hash = $c->digest_string( string => $string ); |
| 28 | |
| 29 | ok( |
| 30 | eval { |
| 31 | $c->verify_hash( |
| 32 | hash => $hash, |
| 33 | string => $string, |
| 34 | ); |
| 35 | }, |
| 36 | "verify digest", |
| 37 | ); |
| 38 | |
| 39 | ok( !$@, "no error" ) || diag $@; |
| 40 | |
| 41 | ok( |
| 42 | eval { |
| 43 | !$c->verify_hash( |
| 44 | hash => $hash, |
| 45 | string => "some other string", |
| 46 | ); |
| 47 | }, |
| 48 | "verify bad digest", |
| 49 | ); |
| 50 | |
| 51 | ok( !$@, "no error" ) || diag $@; |
| 52 | |
| 53 | throws_ok { |
| 54 | $c->verify_hash( |
| 55 | hash => $hash, |
| 56 | string => "some other string", |
| 57 | fatal => 1, |
| 58 | ), |
| 59 | } qr/verification failed/, "verify_hash with fatal => 1"; |
| 60 | |
| 61 | { |
| 62 | my $mac_1 = $c->mac_digest_string( string => "foo", key => "moose" ); |
| 63 | my $mac_2 = $c->mac_digest_string( string => "foo", key => "elk" ); |
| 64 | |
| 65 | cmp_ok( $mac_1, "ne", $mac_2, "mac hashes are ne with different keys" ); |
| 66 | } |
| 67 | |
| 68 | { |
| 69 | my $mac_1 = $c->mac_digest_string( string => "foo", key => "moose" ); |
| 70 | my $mac_2 = $c->mac_digest_string( string => "bar", key => "moose" ); |
| 71 | |
| 72 | cmp_ok( $mac_1, "ne", $mac_2, "mac hashes are ne with different messages" ); |
| 73 | } |
| 74 | |
| 75 | { |
| 76 | my $mac_1 = $c->mac_digest_string( string => "foo", key => "moose" ); |
| 77 | my $mac_2 = $c->mac_digest_string( string => "foo", key => "moose" ); |
| 78 | |
| 79 | is( $mac_1, $mac_2, "mac hashes are eq when the same" ); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | SKIP: { |
| 84 | eval { require Digest::MD5 }; |
| 85 | skip "Digest::MD5 couldn't be loaded", 3 if $@; |
| 86 | skip "Digest::MD5 is the only fallback", 3 if $fallback_digest eq "SHAMD5"; |
| 87 | |
| 88 | my $md5_hash = $c->digest_string( |
| 89 | digest => "MD5", |
| 90 | string => $string, |
| 91 | ); |
| 92 | |
| 93 | cmp_ok( $md5_hash, "ne", $hash, "$fallback_digest hash ne MD5 hash" ); |
| 94 | |
| 95 | ok( |
| 96 | !$c->verify_hash( |
| 97 | hash => $md5_hash, |
| 98 | string => $string, |
| 99 | ), |
| 100 | "verification fails without same digest", |
| 101 | ); |
| 102 | |
| 103 | ok( |
| 104 | $c->verify_hash( |
| 105 | hash => $md5_hash, |
| 106 | string => $string, |
| 107 | digest => "MD5", |
| 108 | ), |
| 109 | "verification succeeds when MD5", |
| 110 | ); |
| 111 | } |
| 112 |