summaryrefslogtreecommitdiff
path: root/lib/srp/client.rb
blob: ebe158f330b21b976f43bad44ce1e7baf96270b5 (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
42
43
44
module SRP
  class Client

    include SRP::Util

    attr_reader :salt, :verifier, :username

    def initialize(username, options)
      @username = username
      if options[:password]
        @password = options[:password]
        @salt = options[:salt] || bigrand(4).hex
        calculate_verifier
      else
        @verifier = options[:verifier]
        @salt = options[:salt]
      end
    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