File Coverage

blib/lib/Crypt/HSXKPasswd/RNG/Data_Entropy.pm
Criterion Covered Total %
statement 41 59 69.4
branch 0 2 0.0
condition n/a
subroutine 14 16 87.5
pod 0 2 0.0
total 55 79 69.6


line stmt bran cond sub pod time code
1             package Crypt::HSXKPasswd::RNG::Data_Entropy;
2              
3 3     3   22 use parent Crypt::HSXKPasswd::RNG;
  3         7  
  3         31  
4              
5             # import required modules
6 3     3   271 use strict;
  3         8  
  3         94  
7 3     3   17 use warnings;
  3         7  
  3         111  
8 3     3   14 use Carp; # for nicer 'exception' handling for users of the module
  3         6  
  3         244  
9 3     3   17 use Fatal qw( :void open close binmode ); # make builtins throw exceptions on failure
  3         19  
  3         24  
10 3     3   4726 use English qw( -no_match_vars ); # for more readable code
  3         6  
  3         28  
11 3     3   1345 use Readonly; # for truly constant constants
  3         6  
  3         187  
12 3     3   17 use Type::Params qw( compile ); # for parameter validation with Type::Tiny objects
  3         5  
  3         47  
13 3     3   781 use Crypt::HSXKPasswd::Types qw( :types ); # for custom type checking
  3         7  
  3         35  
14 3     3   7911 use Crypt::HSXKPasswd::Helper; # exports utility functions like _error & _warn
  3         7  
  3         332  
15              
16             # set things up for using UTF-8
17 3     3   100 use 5.016; # min Perl for good UTF-8 support, implies feature 'unicode_strings'
  3         20  
18 3     3   16 use Encode qw(encode decode);
  3         12  
  3         198  
19 3     3   18 use utf8;
  3         6  
  3         28  
20             binmode STDOUT, ':encoding(UTF-8)';
21              
22             # try import Data::Entropy::Algorithms
23             Readonly my $_CAN_DATA_ENTROPY => eval{
24             require Data::Entropy::Algorithms; # for random number generation
25             } || 0;
26              
27             # Copyright (c) 2015, Bart Busschots T/A Bartificer Web Solutions All rights
28             # reserved.
29             #
30             # Code released under the FreeBSD license (included in the POD at the bottom of
31             # HSXKPasswd.pm)
32              
33             #
34             # --- Constants ---------------------------------------------------------------
35             #
36              
37             # version info
38 3     3   320 use version; our $VERSION = qv('1.2');
  3         6  
  3         20  
39              
40             # utility variables
41             Readonly my $_CLASS => __PACKAGE__;
42              
43             #
44             # --- Constructor -------------------------------------------------------------
45             #
46              
47             #####-SUB-#####################################################################
48             # Type : CONSTRUCTOR (CLASS)
49             # Returns : An object of type Crypt::HSXKPasswd::RNG::Data_Entropy
50             # Arguments : NONE
51             # Throws : Croaks on invalid invocation and invalid args, or if the module
52             # Data::Entropy::Algorithms is not available
53             # Notes :
54             # See Also :
55             sub new{
56 0     0 0   my $class = shift;
57 0           _force_class($class);
58            
59             # croak if Data::Entropy::Algorithms is not avaialble
60 0 0         unless($_CAN_DATA_ENTROPY){
61 0           _error('This RNG module requires Data::Entropy::Algorithms, which is not installed');
62             }
63            
64             # bless and return an empty object
65 0           my $instance = {};
66 0           bless $instance, $class;
67 0           return $instance;
68             }
69              
70             #
71             # --- Public Instance functions -----------------------------------------------
72             #
73              
74             #####-SUB-#####################################################################
75             # Type : INSTANCE
76             # Purpose : Override the parent random_numbers() function and generate
77             # random numbers between 0 and 1.
78             # Returns : An array of numbers between 0 and 1
79             # Arguments : 1) the number of random numbers needed to produce 1 password.
80             # Throws : NOTHING
81             # Notes : This function will return the number of random numbers needed
82             # for a single password.
83             # See Also :
84             sub random_numbers{
85 0     0 0   my @args = @_;
86 0           my $self = shift @args;
87 0           _force_instance($self);
88            
89             # validate args
90 0           state $args_check = compile(PositiveInteger);
91 0           my ($num) = $args_check->(@args);
92            
93             # generate the random numbers
94 0           my @ans = ();
95 0           my $num_to_generate = $num;
96 0           while($num_to_generate > 0){
97 0           push @ans, Data::Entropy::Algorithms::rand();
98 0           $num_to_generate--;
99             }
100            
101             # return the random numbers
102 0           return @ans;
103             }
104              
105             1; # because Perl is just a little bit odd :)