-
Notifications
You must be signed in to change notification settings - Fork 9
/
gen-ip-validator.pl
57 lines (47 loc) · 1.12 KB
/
gen-ip-validator.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl
#***********************************************************************
#
# gen-ip-validator.pl
#
# Generate a random number used to confirm IP address header.
#
# * This program may be distributed under the terms of the GNU General
# * Public License, Version 2.
#
#***********************************************************************
use constant HAS_OPENSSL_RANDOM => eval { require Crypt::OpenSSL::Random; };
BEGIN
{
eval{
Crypt::OpenSSL::Random->import
};
}
use Digest::SHA;
sub read_urandom($) {
my $len = shift;
my $junk;
my $in;
if (-r "/dev/urandom") {
open($in, "<", "/dev/urandom");
read($in, $junk, $len);
close($in);
}
return $junk;
}
my $rng;
my $data;
if(HAS_OPENSSL_RANDOM) {
for (;;) {
$rng .= sprintf "%x\n", rand(0xffffffff);
$rng .= read_urandom(64);
last if(Crypt::OpenSSL::Random::random_status());
}
Crypt::OpenSSL::Random::random_seed($rng);
$data = Crypt::OpenSSL::Random::random_bytes(256);
} else {
$data = read_urandom(256);
}
my $ctx = Digest::SHA->new;
$ctx->add($data);
my $rnd = $ctx->hexdigest;
print "X-MIMEDefang-Relay-$rnd\n";