File Coverage

blib/lib/Crypt/Random/Provider/rand.pm
Criterion Covered Total %
statement 32 34 94.1
branch 4 6 66.6
condition 7 14 50.0
subroutine 8 9 88.8
pod 0 3 0.0
total 51 66 77.2


line stmt bran cond sub pod time code
1             ##
2             ## Copyright (c) 1998-2025, Vipul Ved Prakash. All rights reserved.
3             ## This code is free software; you can redistribute it and/or modify
4             ## it under the same terms as Perl itself.
5              
6 3     3   23 use strict;
  3         6  
  3         117  
7 3     3   16 use warnings;
  3         5  
  3         224  
8             package Crypt::Random::Provider::rand;
9 3     3   18 use Math::Pari qw(pari2num);
  3         5  
  3         23  
10 3     3   430 use Crypt::URandom qw/urandom/;
  3         8  
  3         1350  
11              
12             our $VERSION = '1.57';
13              
14             sub new {
15              
16 13     13 0 3262 my ($class, %params) = @_;
17 13   33 55   92 my $self = { Source => $params{Source} || sub { Crypt::URandom::urandom($_[0]) } };
  55         129  
18 13         137 return bless $self, $class;
19              
20             }
21              
22             sub get_data {
23              
24 13     13 0 32 my ($self, %params) = @_;
25 13 50       27 $self = {} unless ref $self;
26              
27 13         17 my $size = $params{Size};
28 13   50     56 my $skip = $params{Skip} || $$self{Skip} || '';
29 13         23 my $q_skip = quotemeta($skip);
30              
31 13 50 66     41 if ($size && ref $size eq "Math::Pari") {
32 0         0 $size = pari2num($size);
33             }
34              
35 13   66     46 my $bytes = $params{Length} || (int($size / 8) + 1);
36              
37 13   33 0   28 my $source = $$self{Source} || sub { Crypt::URandom::urandom($_[0]) };
  0         0  
38            
39 13         24 my($r, $read, $rt) = ('', 0);
40 13         24 while ($read < $bytes) {
41 55         114 $rt = &$source($bytes - $read);
42 55 100       2180 $rt =~ s/[$q_skip]//g if $skip;
43 55         115 $r .= $rt;
44 55         136 $read = length $r;
45             }
46              
47 13         67 $r;
48              
49             }
50              
51              
52             sub available {
53              
54 5     5 0 75 return 1;
55              
56             }
57              
58              
59             1;
60