diff options
author | Azul <azul@riseup.net> | 2012-10-05 12:44:47 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2012-10-05 12:44:47 +0200 |
commit | 7762129834afab32dbdd4b16e79b4fd487d1fafe (patch) | |
tree | 63e226e1df93e95a8e9be2494c8d58ab5d04839c /lib/srp/client.rb | |
parent | 9683634eb18843151d318b483a5fb237508f4755 (diff) | |
parent | ce246cb722f7f61b3a1ba7223857627f1bed4f4c (diff) |
Merge branch 'feature-py_srp_compat' into developdevelop
Diffstat (limited to 'lib/srp/client.rb')
-rw-r--r-- | lib/srp/client.rb | 43 |
1 files changed, 17 insertions, 26 deletions
diff --git a/lib/srp/client.rb b/lib/srp/client.rb index 484d12b..94e36af 100644 --- a/lib/srp/client.rb +++ b/lib/srp/client.rb @@ -5,46 +5,37 @@ module SRP include Util - attr_reader :salt, :verifier + attr_reader :salt, :verifier, :username - def initialize(username, password) + def initialize(username, password, salt = nil) @username = username @password = password - @salt = "5d3055e0acd3ddcfc15".hex # bigrand(10).hex - @multiplier = multiplier # let's cache it + @salt = salt || bigrand(4).hex calculate_verifier end - def authenticate(server, username, password) - x = calculate_x(username, password, salt) - a = bigrand(32).hex - aa = modpow(GENERATOR, a, PRIME_N) # A = g^a (mod N) - bb = server.handshake(username, aa) - u = calculate_u(aa, bb, PRIME_N) - client_s = calculate_client_s(x, a, bb, u) - server.validate(calculate_m(aa, bb, client_s)) + def authenticate(server) + @session = SRP::Session.new(self) + @session.handshake(server) + @session.validate(server) + end + + def private_key + @private_key ||= calculate_private_key end protected + def calculate_verifier - x = calculate_x(@username, @password, @salt) - @verifier = modpow(GENERATOR, x, PRIME_N) - @verifier + @verifier ||= modpow(GENERATOR, private_key) end - def calculate_x(username, password, salt) - shex = '%x' % [salt] - spad = "" # if shex.length.odd? then '0' else '' end - sha256_str(spad + shex + sha256_str([username, password].join(':'))).hex + def calculate_private_key + shex = '%x' % [@salt] + inner = sha256_str([@username, @password].join(':')) + sha256_hex(shex, inner).hex end - def calculate_client_s(x, a, bb, u) - base = bb - base += PRIME_N * @multiplier - base -= modpow(GENERATOR, x, PRIME_N) * @multiplier - base = base % PRIME_N - modpow(base, x * u + a, PRIME_N) - end end end |