C0 code coverage information
Generated on Fri Jul 11 15:55:32 -0700 2008 with rcov 0.7.0
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 require "openid/util"
2 require "digest/sha1"
3 require "digest/sha2"
4 begin
5 require "digest/hmac"
6 rescue LoadError
7 require "hmac/sha1"
8 require "hmac/sha2"
9 end
10
11 module OpenID
12 # This module contains everything needed to perform low-level
13 # cryptograph and data manipulation tasks.
14 module CryptUtil
15
16 # Generate a random number, doing a little extra work to make it
17 # more likely that it's suitable for cryptography. If your system
18 # doesn't have /dev/urandom then this number is not
19 # cryptographically safe. See
20 # <http://www.cosine.org/2007/08/07/security-ruby-kernel-rand/>
21 # for more information. max is the largest possible value of such
22 # a random number, where the result will be less than max.
23 def CryptUtil.rand(max)
24 Kernel.srand()
25 return Kernel.rand(max)
26 end
27
28 def CryptUtil.sha1(text)
29 return Digest::SHA1.digest(text)
30 end
31
32 def CryptUtil.hmac_sha1(key, text)
33 if Digest.const_defined? :HMAC
34 Digest::HMAC.new(key,Digest::SHA1).update(text).digest
35 else
36 return HMAC::SHA1.digest(key, text)
37 end
38 end
39
40 def CryptUtil.sha256(text)
41 return Digest::SHA256.digest(text)
42 end
43
44 def CryptUtil.hmac_sha256(key, text)
45 if Digest.const_defined? :HMAC
46 Digest::HMAC.new(key,Digest::SHA256).update(text).digest
47 else
48 return HMAC::SHA256.digest(key, text)
49 end
50 end
51
52 # Generate a random string of the given length, composed of the
53 # specified characters. If chars is nil, generate a string
54 # composed of characters in the range 0..255.
55 def CryptUtil.random_string(length, chars=nil)
56 s = ""
57
58 unless chars.nil?
59 length.times { s << chars[rand(chars.length)] }
60 else
61 length.times { s << rand(256).chr }
62 end
63 return s
64 end
65
66 # Convert a number to its binary representation; return a string
67 # of bytes.
68 def CryptUtil.num_to_binary(n)
69 bits = n.to_s(2)
70 prepend = (8 - bits.length % 8)
71 bits = ('0' * prepend) + bits
72 return [bits].pack('B*')
73 end
74
75 # Convert a string of bytes into a number.
76 def CryptUtil.binary_to_num(s)
77 # taken from openid-ruby 0.0.1
78 s = "\000" * (4 - (s.length % 4)) + s
79 num = 0
80 s.unpack('N*').each do |x|
81 num <<= 32
82 num |= x
83 end
84 return num
85 end
86
87 # Encode a number as a base64-encoded byte string.
88 def CryptUtil.num_to_base64(l)
89 return OpenID::Util.to_base64(num_to_binary(l))
90 end
91
92 # Decode a base64 byte string to a number.
93 def CryptUtil.base64_to_num(s)
94 return binary_to_num(OpenID::Util.from_base64(s))
95 end
96 end
97 end
Generated using the rcov code coverage analysis tool for Ruby version 0.7.0.