File Coverage

blib/lib/Crypt/HSXKPasswd/RNG/Basic.pm
Criterion Covered Total %
statement 57 57 100.0
branch n/a
condition n/a
subroutine 16 16 100.0
pod 0 2 0.0
total 73 75 97.3


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