Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Support SSL connections to randomness sources #1

Merged
merged 1 commit into from about 2 years ago

4 participants

Mark Allen David Cantrell Olivier Mengué Mark Allen
Mark Allen

Both random.org and fourmilab.ch have supported SSL connections for a while. This patch integrates support for SSL connections as the default, but allows a user to revert to plaintext if he/she chooses to do so.

David Cantrell DrHyde merged commit 1e0a6b9 into from April 12, 2012
David Cantrell DrHyde closed this April 12, 2012
David Cantrell
Owner

Thanks very much!

David Cantrell
Owner
Mark Allen

Awesome. Thanks for the merge.

Olivier Mengué

@DrHyde: it woud be even easier for you (you would get more patches as pull requests) if the users of your module knew that your code is hosted here.
So you should advertise your repository location in META.yml.
Also it would be useful to advertise your GitHub account on your MetaCPAN profile.

David Cantrell
Owner
DrHyde commented May 03, 2012

I've tried twice now to get access to my metacpan profile. It doesn't work.

Olivier Mengué
dolmen commented May 03, 2012

It works for for me.
I would like to help you to fix this problem. Would it be possible to discuss that interactively? Maybe on IRC, #metacpan on irc.perl.org?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 12, 2012
Mark Allen Support SSL connections to randomness sources 59f4f24
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 31 additions and 10 deletions. Show diff stats Hide diff stats

  1. 41  lib/Net/Random.pm
41  lib/Net/Random.pm
@@ -22,8 +22,10 @@ my $ua = LWP::UserAgent->new(
22 22
 
23 23
 %randomness = (
24 24
     'fourmilab.ch' => { pool => [], retrieve => sub {
25  
-        my $response = $ua->get(
26  
-	    'http://www.fourmilab.ch/cgi-bin/uncgi/Hotbits?nbytes=1024&fmt=hex'
  25
+        my $ssl = shift;
  26
+        my $response = $ua->get( 
  27
+            ($ssl ? 'https' : 'http') .
  28
+	    '://www.fourmilab.ch/cgi-bin/uncgi/Hotbits?nbytes=1024&fmt=hex'
27 29
 	);
28 30
 	unless($response->is_success) {
29 31
 	    warn "Net::Random: Error talking to fourmilab.ch\n";
@@ -37,19 +39,28 @@ my $ua = LWP::UserAgent->new(
37 39
 	map { map { hex } /(..)/g } grep { /^[0-9A-F]+$/ } split(/\s+/, $content);
38 40
     } },
39 41
     'random.org'   => { pool => [], retrieve => sub {
  42
+        my $ssl = shift;
40 43
         my $response = $ua->get(
41  
-	    'http://random.org/cgi-bin/randbyte?nbytes=1024&format=hex'
  44
+            ($ssl ? 'https' : 'http') .
  45
+	    '://random.org/cgi-bin/randbyte?nbytes=1024&format=hex'
42 46
 	);
43  
-	if(!$response->is_success) {
  47
+
  48
+    if ( ! $response->is_success ) {
44 49
 	    warn "Net::Random: Error talking to random.org\n";
45 50
             return ();
46  
-	}
47  
-
  51
+    }
  52
+    
48 53
 	$response = $response->content();
  54
+
49 55
 	if($response =~ /quota/i) {
50 56
 	    warn("Net::Random: random.org ran out of randomness for us\n");
51 57
 	    return ();
52 58
 	}
  59
+    # Old scripts *always* return 200, so look for 'Error:'
  60
+	elsif($response =~ /Error:/) {
  61
+	    warn "Net::Random: Server error while talking to random.org\n";
  62
+            return ();
  63
+	}
53 64
 
54 65
 	map { hex } split(/\s+/, $response);
55 66
     } }
@@ -60,7 +71,7 @@ sub _recharge {
60 71
     my $self = shift;
61 72
     $randomness{$self->{src}}->{pool} = [
62 73
         @{$randomness{$self->{src}}->{pool}},
63  
-        &{$randomness{$self->{src}}->{retrieve}}
  74
+        &{$randomness{$self->{src}}->{retrieve}}($self->{ssl})
64 75
     ];
65 76
 }
66 77
 
@@ -86,8 +97,8 @@ Net::Random - get random data from online sources
86 97
 =head1 OVERVIEW
87 98
 
88 99
 The two sources of randomness above correspond to
89  
-L<http://www.fourmilab.ch/cgi-bin/uncgi/Hotbits?nbytes=1024&fmt=hex> and
90  
-L<http://random.org/cgi-bin/randbyte?nbytes=1024&format=hex>.  We always
  100
+L<https://www.fourmilab.ch/cgi-bin/uncgi/Hotbits?nbytes=1024&fmt=hex> and
  101
+L<https://random.org/cgi-bin/randbyte?nbytes=1024&format=hex>.  We always
91 102
 get chunks of 1024 bytes at a time, storing it in a pool which is used up
92 103
 as and when needed.  The pool is shared between all objects using the
93 104
 same randomness source.  When we run out of randomness we go back to the
@@ -120,9 +131,14 @@ is 2^32-1, the largest value that can be stored in a 32-bit int, or
120 131
 0xFFFFFFFF.  The range between min and max can not be greater than
121 132
 0xFFFFFFFF either.
122 133
 
  134
+You may also set 'ssl' to 0 if you wish to retrieve data using plaintext
  135
+(or outbound SSL is prohibited in your network environment for some reason)
  136
+
123 137
 Currently, the only valid values of 'src' are 'fourmilab.ch' and
124 138
 'random.org'.
125 139
 
  140
+
  141
+
126 142
 =cut
127 143
 
128 144
 sub new {
@@ -130,10 +146,15 @@ sub new {
130 146
 
131 147
     exists($params{min}) or $params{min} = 0;
132 148
     exists($params{max}) or $params{max} = 255;
  149
+    exists($params{ssl}) or $params{ssl} = 1;
  150
+
  151
+    require LWP::Protocol::https or die "LWP::Protocol::https required for SSL connections"
  152
+        if ( $params{ssl} );
  153
+
133 154
 
134 155
     die("Bad parameters to Net::Random->new():\n".Dumper(\@_)) if(
135 156
         (grep {
136  
-            $_ !~ /^(src|min|max)$/
  157
+            $_ !~ /^(src|min|max|ssl)$/
137 158
         } keys %params) ||
138 159
 	!exists($params{src}) ||
139 160
 	$params{src} !~ /^(fourmilab\.ch|random\.org)$/ ||
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.