File Coverage

blib/lib/STIX/Common/Hashes.pm
Criterion Covered Total %
statement 40 44 90.9
branch 11 16 68.7
condition n/a
subroutine 11 13 84.6
pod 3 3 100.0
total 65 76 85.5


line stmt bran cond sub pod time code
1             package STIX::Common::Hashes;
2              
3 4     4   2038 use 5.010001;
  4         18  
4 4     4   32 use strict;
  4         8  
  4         178  
5 4     4   25 use warnings;
  4         8  
  4         253  
6 4     4   29 use utf8;
  4         8  
  4         38  
7              
8 4     4   195 use Cpanel::JSON::XS;
  4         8  
  4         447  
9 4     4   1456 use Types::Standard qw(Str);
  4         230971  
  4         41  
10              
11 4     4   9267 use Moo;
  4         14031  
  4         24  
12 4     4   4654 use namespace::autoclean;
  4         30846  
  4         31  
13              
14             extends 'STIX::Object';
15              
16 4         2518 use constant PROPERTIES => (qw[
17             md5
18             sha_1
19             sha_256
20             sha_512
21             sha3_256
22             sha3_512
23             ssdeep
24             tlsh
25 4     4   448 ]);
  4         12  
26              
27             around BUILDARGS => sub {
28              
29             my ($orig, $class, %params) = @_;
30             my %hashes = map { _normalize_hash_type($_) => $params{$_} } keys %params;
31              
32             return \%hashes;
33              
34             };
35              
36             sub _normalize_hash_type {
37 19     19   49 my $type = shift;
38 19         98 $type =~ s/-/_/g;
39 19         125 return lc $type;
40             }
41              
42             has md5 => (is => 'rw', isa => Str);
43             has sha_1 => (is => 'rw', isa => Str);
44             has sha_256 => (is => 'rw', isa => Str);
45             has sha_512 => (is => 'rw', isa => Str);
46             has sha3_256 => (is => 'rw', isa => Str);
47             has sha3_512 => (is => 'rw', isa => Str);
48             has ssdeep => (is => 'rw', isa => Str);
49             has tlsh => (is => 'rw', isa => Str);
50              
51             sub TO_JSON {
52              
53 3392     3392 1 14210 my $self = shift;
54 3392         5234 my $hashes = {};
55              
56 3392 100       79236 $hashes->{'md5'} = $self->md5 if ($self->md5);
57 3392 100       78417 $hashes->{'sha-1'} = $self->sha_1 if ($self->sha_1);
58 3392 100       72875 $hashes->{'sha-256'} = $self->sha_256 if ($self->sha_256);
59 3392 50       141032 $hashes->{'sha-512'} = $self->sha_512 if ($self->sha_512);
60 3392 50       73399 $hashes->{'sha3-256'} = $self->sha3_256 if ($self->sha3_256);
61 3392 50       71107 $hashes->{'sha3-512'} = $self->sha3_512 if ($self->sha3_512);
62 3392 50       68593 $hashes->{'ssdeep'} = $self->ssdeep if ($self->ssdeep);
63 3392 50       71238 $hashes->{'tlsh'} = $self->tlsh if ($self->tlsh);
64              
65 3392         46406 return $hashes;
66              
67             }
68              
69 0     0 1   sub to_hash { shift->TO_JSON }
70              
71             sub to_string {
72              
73 0     0 1   my $self = shift;
74              
75 0           my $json = Cpanel::JSON::XS->new->utf8->canonical->allow_nonref->allow_unknown->allow_blessed->convert_blessed
76             ->stringify_infnan->escape_slash(0)->allow_dupkeys->pretty;
77              
78 0           return $json->encode($self->TO_JSON);
79              
80             }
81              
82             1;
83              
84             =encoding utf-8
85              
86             =head1 NAME
87              
88             STIX::Common::Hashes - Hashes type
89              
90             =head1 SYNOPSIS
91              
92             use STIX::Common::Hashes;
93              
94             my $hashes = STIX::Common::Hashes->new(md5 => '...', sha_1 => '...');
95              
96              
97             =head1 DESCRIPTION
98              
99             The Hashes type represents one or more cryptographic hashes, as a special set of
100             key/value pairs. Accordingly, the name of each hashing algorithm MUST be specified
101             as a key in the dictionary and MUST identify the name of the hashing algorithm
102             used to generate the corresponding value.
103              
104             =head2 PROPERTIES
105              
106             =over
107              
108             =item md5
109              
110             =item sha_1
111              
112             =item sha_256
113              
114             =item sha_512
115              
116             =item sha3_256
117              
118             =item sha3_512
119              
120             =item ssdeep
121              
122             =item tlsh
123              
124              
125             =back
126              
127             =head2 HELPERS
128              
129             =over
130              
131             =item $hashes->TO_JSON
132              
133             Encode the object in JSON.
134              
135             =item $hashes->to_hash
136              
137             Return the object HASH.
138              
139             =item $hashes->to_string
140              
141             Encode the object in JSON.
142              
143             =back
144              
145              
146             =head1 SUPPORT
147              
148             =head2 Bugs / Feature Requests
149              
150             Please report any bugs or feature requests through the issue tracker
151             at L.
152             You will be notified automatically of any progress on your issue.
153              
154             =head2 Source Code
155              
156             This is open source software. The code repository is available for
157             public review and contribution under the terms of the license.
158              
159             L
160              
161             git clone https://github.com/giterlizzi/perl-STIX.git
162              
163              
164             =head1 AUTHOR
165              
166             =over 4
167              
168             =item * Giuseppe Di Terlizzi
169              
170             =back
171              
172              
173             =head1 LICENSE AND COPYRIGHT
174              
175             This software is copyright (c) 2024 by Giuseppe Di Terlizzi.
176              
177             This is free software; you can redistribute it and/or modify it under
178             the same terms as the Perl 5 programming language system itself.
179              
180             =cut