File Coverage

blib/lib/SBOM/CycloneDX/Hash.pm
Criterion Covered Total %
statement 27 27 100.0
branch 1 2 50.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 39 40 97.5


line stmt bran cond sub pod time code
1             package SBOM::CycloneDX::Hash;
2              
3 2     2   2169 use 5.010001;
  2         8  
4 2     2   15 use strict;
  2         5  
  2         72  
5 2     2   12 use warnings;
  2         4  
  2         133  
6 2     2   14 use utf8;
  2         5  
  2         15  
7              
8 2     2   104 use SBOM::CycloneDX::Enum;
  2         5  
  2         140  
9              
10 2     2   15 use Types::Standard qw(Enum StrMatch);
  2         4  
  2         20  
11              
12 2     2   6071 use Moo;
  2         6  
  2         18  
13 2     2   1066 use namespace::autoclean;
  2         6  
  2         22  
14              
15             extends 'SBOM::CycloneDX::Base';
16              
17             my %ALGO_LENGTH = (
18             'MD5' => 32,
19             'SHA-1' => 40,
20             'SHA-256' => 64,
21             'SHA-384' => 96,
22             'SHA-512' => 128,
23             'SHA3-256' => 64,
24             'SHA3-384' => 96,
25             'SHA3-512' => 128,
26             'BLAKE2b-256' => 64,
27             'BLAKE2b-384' => 96,
28             'BLAKE2b-512' => 128,
29             'BLAKE3' => 64,
30             'Streebog-256' => 64,
31             'Streebog-512' => 128,
32             );
33              
34             has alg => (
35             is => 'rw',
36             isa => Enum [SBOM::CycloneDX::Enum->values('HASH_ALGORITHM')],
37             coerce => sub { uc($_[0]) },
38             required => 1
39             );
40              
41             has content => (
42             is => 'rw',
43             isa => StrMatch [qr{^([a-fA-F0-9]{32}|[a-fA-F0-9]{40}|[a-fA-F0-9]{64}|[a-fA-F0-9]{96}|[a-fA-F0-9]{128})$}],
44             required => 1,
45             trigger => 1
46             );
47              
48             sub _trigger_content {
49 8 50   8   858 Carp::croak 'Invalid hash length' if $ALGO_LENGTH{$_[0]->alg} != length($_[0]->content);
50             }
51              
52             sub TO_JSON {
53              
54 104     104 1 273 my $self = shift;
55              
56 104         2017 my $json = {alg => $self->alg, content => $self->content};
57              
58 104         3626 return $json;
59              
60             }
61              
62             1;
63              
64             =encoding utf-8
65              
66             =head1 NAME
67              
68             SBOM::CycloneDX::Hash - Hash
69              
70             =head1 SYNOPSIS
71              
72             SBOM::CycloneDX::Hash->new();
73              
74              
75             =head1 DESCRIPTION
76              
77             L provides the hash object.
78              
79             =head2 METHODS
80              
81             L inherits all methods from L
82             and implements the following new ones.
83              
84             =over
85              
86             =item SBOM::CycloneDX::Hash->new( %PARAMS )
87              
88             Properties:
89              
90             =over
91              
92             =item * C, The algorithm that generated the hash value.
93              
94             =item * C, The value of the hash.
95              
96             =back
97              
98             =item $hash->_trigger_content
99              
100             =item $hash->alg
101              
102             =item $hash->content
103              
104             =back
105              
106              
107             =head1 SUPPORT
108              
109             =head2 Bugs / Feature Requests
110              
111             Please report any bugs or feature requests through the issue tracker
112             at L.
113             You will be notified automatically of any progress on your issue.
114              
115             =head2 Source Code
116              
117             This is open source software. The code repository is available for
118             public review and contribution under the terms of the license.
119              
120             L
121              
122             git clone https://github.com/giterlizzi/perl-SBOM-CycloneDX.git
123              
124              
125             =head1 AUTHOR
126              
127             =over 4
128              
129             =item * Giuseppe Di Terlizzi
130              
131             =back
132              
133              
134             =head1 LICENSE AND COPYRIGHT
135              
136             This software is copyright (c) 2025-2026 by Giuseppe Di Terlizzi.
137              
138             This is free software; you can redistribute it and/or modify it under
139             the same terms as the Perl 5 programming language system itself.
140              
141             =cut