summaryrefslogtreecommitdiff
path: root/lib/srp/client.rb
blob: 94e36afeb1e35bab4d6544d32b8c3aa01320df33 (plain)
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
require File.expand_path(File.dirname(__FILE__) + '/util')

module SRP
  class Client

    include Util

    attr_reader :salt, :verifier, :username

    def initialize(username, password, salt = nil)
      @username = username
      @password = password
      @salt = salt || bigrand(4).hex
      calculate_verifier
    end

    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
      @verifier ||= modpow(GENERATOR, private_key)
    end

    def calculate_private_key
      shex = '%x' % [@salt]
      inner = sha256_str([@username, @password].join(':'))
      sha256_hex(shex, inner).hex
    end

  end
end