File Coverage

blib/lib/Crypt/HSXKPasswd/RNG/Math_Random_Secure.pm
Criterion Covered Total %
statement 58 59 98.3
branch 1 2 50.0
condition n/a
subroutine 16 16 100.0
pod 0 2 0.0
total 75 79 94.9


line stmt bran cond sub pod time code
1             package Crypt::HSXKPasswd::RNG::Math_Random_Secure;
2              
3 3     3   14 use parent Crypt::HSXKPasswd::RNG;
  3         6  
  3         19  
4              
5             # import required modules
6 3     3   128 use strict;
  3         4  
  3         52  
7 3     3   9 use warnings;
  3         5  
  3         62  
8 3     3   17 use Carp; # for nicer 'exception' handling for users of the module
  3         4  
  3         190  
9 3     3   18 use Fatal qw( :void open close binmode ); # make builtins throw exceptions on failure
  3         6  
  3         22  
10 3     3   4541 use English qw( -no_match_vars ); # for more readable code
  3         3  
  3         17  
11 3     3   1167 use Readonly; # for truly constant constants
  3         4  
  3         208  
12 3     3   17 use Type::Params qw( compile ); # for parameter validation with Type::Tiny objects
  3         5  
  3         40  
13 3     3   745 use Crypt::HSXKPasswd::Types qw( :types ); # for custom type checking
  3         6  
  3         35  
14 3     3   6290 use Crypt::HSXKPasswd::Helper; # exports utility functions like _error & _warn
  3         4  
  3         213  
15              
16             # set things up for using UTF-8
17 3     3   62 use 5.016; # min Perl for good UTF-8 support, implies feature 'unicode_strings'
  3         14  
18 3     3   17 use Encode qw(encode decode);
  3         5  
  3         186  
19 3     3   17 use utf8;
  3         5  
  3         22  
20             binmode STDOUT, ':encoding(UTF-8)';
21              
22             # try import Data::Entropy::Algorithms
23             Readonly my $_CAN_MATH_RANDOM_SECURE => eval{
24             require Math::Random::Secure; # 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   258 use version; our $VERSION = qv('1.2');
  3         5  
  3         23  
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::Math_Random_Secure
50             # Arguments : NONE
51             # Throws : Croaks on invalid invocation and invalid args, or if the module
52             # Math::Random::Secure is not available
53             # Notes :
54             # See Also :
55             sub new{
56 9     9 0 14 my $class = shift;
57            
58             # validate the args
59 9         32 _force_class($class);
60            
61             # croak if Math::Random::Secure is not avaialble
62 9 50       30 unless($_CAN_MATH_RANDOM_SECURE){
63 0         0 _error('This RNG module requires Math::Random::Secure, which is not installed');
64             }
65            
66             # bless and return an empty object
67 9         55 my $instance = {};
68 9         22 bless $instance, $class;
69 9         39 return $instance;
70             }
71              
72             #
73             # --- Public Instance functions -----------------------------------------------
74             #
75              
76             #####-SUB-#####################################################################
77             # Type : INSTANCE
78             # Purpose : Override the parent random_numbers() function and generate
79             # random numbers between 0 and 1.
80             # Returns : An array of numbers between 0 and 1
81             # Arguments : 1) the number of random numbers needed to produce 1 password.
82             # Throws : NOTHING
83             # Notes : This function will return the number of random numbers needed
84             # for a single password.
85             # See Also :
86             sub random_numbers{
87 13     13 0 30 my @args = @_;
88 13         31 my $self = shift @args;
89 13         50 _force_instance($self);
90            
91             # validate args
92 13         34 state $args_check = compile(PositiveInteger);
93 13         785 my ($num) = $args_check->(@args);
94            
95             # generate the random numbers
96 13         107 my @ans = ();
97 13         14 my $num_to_generate = $num;
98 13         34 while($num_to_generate > 0){
99 121         214 push @ans, Math::Random::Secure::rand();
100 121         302372 $num_to_generate--;
101             }
102            
103             # return the random numbers
104 13         113 return @ans;
105             }
106              
107             1; # because Perl is just a little bit odd :)