CryptUtil.pm

filter: Statementstmt 77.5% 69 / 89 Branchbran 55.0% 11 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 7 totaltotal 72.1% 96 / 133 CC 14 SCAR 20.3
CC
14
Cov
73%
randomString
22.6
bytesToNum
4.1
numToBytes
4.0
CRAP 7.6
« Stores/PostgreSQLStore.pm ↑ Summary Server.pm »
1 package Net::OpenID::JanRain::CryptUtil;
2  
3 # vi:ts=4:sw=4
4  
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Digest::HMAC_SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Math::BigInt lib => 'GMP'; #without GMP Diffie Hellman takes a LONG time
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Net::OpenID::JanRain::Util qw( toBase64 fromBase64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 our @ISA = qw(Exporter);
18 4 our @EXPORT_OK = qw(
19 hmacSha1
20 sha1
21 numToBytes
22 numToBase64
23 bytesToNum
24 base64ToNum
25 randomString
26 DEFAULT_DH_MOD
27 DEFAULT_DH_GEN
28 );
29  
30 4 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 DEFAULT_DH_MOD => "155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443",
32 DEFAULT_DH_GEN => 2,
33 4 };
34  
35 # We need a Cryptographic strength source of randomness
36 # /dev/urandom will work nicely.
37 # output will be biased as 0..255 % len(chrs)
38 # i.e. unbiased if len(chrs) is a power of two
39 64 ▶sub randomString {
Subroutine randomString: called CC=8 SCAR=31.2
Pod undocumented
40 64 my ($length, $chrs) = @_;
41 64 my $s = "";
42  
43 64 ▶ if (-e "/dev/urandom") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e '/dev/urandom') { } 64 0 64
44 64 my ($ur, $randomness, $got);
45 64 open $ur, "< /dev/urandom";
46 64 $got = sysread $ur, $randomness, $length;
47 64 ▶ die "Couldn't get enough of /dev/urandom" unless $got == $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $got == $length 0 64 64
48 64 close $ur;
49 64 ▶ if($chrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chrs) { } 0 64 64
50 0 my @chrs = split(//, $chrs);
51 0 my @rand = split(//, $randomness);
52 0 ▶ for(my $i=0;$i<$length;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 0 0 0
53 0 $s .= $chrs[ord($rand[$i]) % @chrs];
54 }
55 }
56 else {
57 64 $s = $randomness;
58 }
59 64 return $s;
60 }
61 else {
62 # An attack using the predictability of rand is possible.
63 # We strongly recommend using a system with a cryptographically
64 # secure source of randomness to run our OpenID library.
65 0 warn "No /dev/urandom - YOUR OPENID LIBRARY IS NOT SECURE!";
66 0 die "Comment out this line to continue anyway";
67 0 my ($length, $chrs) = @_;
68 0 my $s = "";
69 0 ▶ if($chrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chrs) { } 0 0 0
70 0 my @chrs = split(//, $chrs);
71 0 for(1..$length) {
72 0 $s .= $chrs[int(rand(@chrs))];
73 }
74 }
75 else {
76 0 for(1..$length) {
77 0 $s .= chr(int(rand(256)));
78 }
79 }
80 0 return($s);
81 } # end randomString
82 }
83 ########################################################################
84  
85 10 ▶sub hmacSha1 {
Subroutine hmacSha1: called CC=1 SCAR=0.0
Pod undocumented
86 10 my ($key, $text) = @_;
87 10 return(Digest::HMAC_SHA1::hmac_sha1($text, $key));
88 } # end hmacSha1
89 ########################################################################
90 50 ▶sub sha1 {
Subroutine sha1: called CC=1 SCAR=0.0
Pod undocumented
91 50 my ($s) = @_;
92 50 return(Digest::SHA1::sha1($s));
93 } # end sha1
94 ########################################################################
95 0 ▶sub numToBase64 {
Subroutine numToBase64: not called CC=1 SCAR=6.9
Pod undocumented
96 0 my ($n) = @_;
97 0 return toBase64(numToBytes($n));
98 }
99 ########################################################################
100 50 ▶sub numToBytes {
Subroutine numToBytes: called CC=4 SCAR=13.9
Pod undocumented
101 50 my ($n) = @_;
102 0 ▶ if ($n < 0) {die "numToBytes takes only positive integers.";}
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 50 50
103 50 my @bytes = ();
104 # get a big-endian base 256 representation of n
105 50 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 1275 50 1325
106 1275 unshift( @bytes, $n % 256 );
107 1275 $n = $n >> 8;
108 }
109 # first byte high bit is the sign bit
110 50 ▶ if ($bytes[0] > 127) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes[0] > 127) 22 28 50
111 22 unshift( @bytes, 0);
112 }
113 50 my $string = pack('C*',@bytes);
114 50 return $string;
115 }
116 ########################################################################
117 1 ▶sub base64ToNum {
Subroutine base64ToNum: called CC=1 SCAR=0.0
Pod undocumented
118 1 my ($b64str) = @_;
119 1 return bytesToNum(fromBase64($b64str));
120 }
121 ########################################################################
122 101 ▶sub bytesToNum {
Subroutine bytesToNum: called CC=4 SCAR=14.2
Pod undocumented
123 101 my ($string) = @_;
124 101 ▶ unless($string) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string) 0 101 101
125 0 warn "empty string passed to bytesToNum";
126 0 return 0;
127 }
128 101 my @bytes = unpack('C*',$string);
129 101 my $n = Math::BigInt->new(0);
130 # high bit set means negative in twos complement; invalid for us
131 101 ▶ return undef if ($bytes[0] > 127);
Branch: 1 branch
BranchTrueFalseTotal
if $bytes[0] > 127 1 100 101
132 100 for (@bytes) {
133 2594 $n = $n << 8;
134 2594 $n = $n + $_;
135 }
136 100 return $n;
137 }
138 ########################################################################
139 4 1;
« Stores/PostgreSQLStore.pm ↑ Summary Server.pm »