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.
| Name |
Total lines |
Lines of code |
Total coverage |
Code coverage |
|
lib/openid/dh.rb
|
89
|
62
|
|
|
1 require "openid/util"
2 require "openid/cryptutil"
3
4 module OpenID
5
6 # Encapsulates a Diffie-Hellman key exchange. This class is used
7 # internally by both the consumer and server objects.
8 #
9 # Read more about Diffie-Hellman on wikipedia:
10 # http://en.wikipedia.org/wiki/Diffie-Hellman
11
12 class DiffieHellman
13
14 # From the OpenID specification
15 @@default_mod = 155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443
16 @@default_gen = 2
17
18 attr_reader :modulus, :generator, :public
19
20 # A new DiffieHellman object, using the modulus and generator from
21 # the OpenID specification
22 def DiffieHellman.from_defaults
23 DiffieHellman.new(@@default_mod, @@default_gen)
24 end
25
26 def initialize(modulus=nil, generator=nil, priv=nil)
27 @modulus = modulus.nil? ? @@default_mod : modulus
28 @generator = generator.nil? ? @@default_gen : generator
29 set_private(priv.nil? ? OpenID::CryptUtil.rand(@modulus-2) + 1 : priv)
30 end
31
32 def get_shared_secret(composite)
33 DiffieHellman.powermod(composite, @private, @modulus)
34 end
35
36 def xor_secret(algorithm, composite, secret)
37 dh_shared = get_shared_secret(composite)
38 packed_dh_shared = OpenID::CryptUtil.num_to_binary(dh_shared)
39 hashed_dh_shared = algorithm.call(packed_dh_shared)
40 return DiffieHellman.strxor(secret, hashed_dh_shared)
41 end
42
43 def using_default_values?
44 @generator == @@default_gen && @modulus == @@default_mod
45 end
46
47 private
48 def set_private(priv)
49 @private = priv
50 @public = DiffieHellman.powermod(@generator, @private, @modulus)
51 end
52
53 def DiffieHellman.strxor(s, t)
54 if s.length != t.length
55 raise ArgumentError, "strxor: lengths don't match. " +
56 "Inputs were #{s.inspect} and #{t.inspect}"
57 end
58
59 if String.method_defined? :bytes
60 s.bytes.zip(t.bytes).map{|sb,tb| sb^tb}.pack('C*')
61 else
62 indices = 0...(s.length)
63 chrs = indices.collect {|i| (s[i]^t[i]).chr}
64 chrs.join("")
65 end
66 end
67
68 # This code is taken from this post:
69 # <http://blade.nagaokaut.ac.jp/cgi-bin/scat.\rb/ruby/ruby-talk/19098>
70 # by Eric Lee Green.
71 def DiffieHellman.powermod(x, n, q)
72 counter=0
73 n_p=n
74 y_p=1
75 z_p=x
76 while n_p != 0
77 if n_p[0]==1
78 y_p=(y_p*z_p) % q
79 end
80 n_p = n_p >> 1
81 z_p = (z_p * z_p) % q
82 counter += 1
83 end
84 return y_p
85 end
86
87 end
88
89 end
Generated using the rcov code coverage analysis tool for Ruby version 0.7.0.