123456789_123456789_123456789_123456789_123456789_

Module: Net::IMAP::SASL::ScramAlgorithm

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/net/imap/sasl/scram_algorithm.rb

Overview

For method descriptions, see RFC5802 §2.2 and RFC5802 §3.

Instance Method Summary

Instance Method Details

#auth_message

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 35

def auth_message
  [
    client_first_message_bare,
    server_first_message,
    client_final_message_without_proof,
  ]
    .join(",")
end

#client_key

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 48

def client_key;       HMAC(salted_password, "Client Key") end

#client_proof

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 53

def client_proof;     XOR(client_key, client_signature)   end

#client_signature

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 51

def client_signature; HMAC(stored_key, auth_message)      end

H(str)

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 24

def H(str) digest.digest str end

Hi(str, salt, iterations)

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 13

def Hi(str, salt, iterations)
  length = digest.digest_length
  OpenSSL::KDF.pbkdf2_hmac(
    str,
    salt:       salt,
    iterations: iterations,
    length: length,
    hash: digest,
  )
end

HMAC(key, data)

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 26

def HMAC(key, data) OpenSSL::HMAC.digest(digest, key, data) end

Normalize(str)

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 11

def Normalize(str) SASL.saslprep(str) end

#salted_password

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 44

def salted_password
  Hi(Normalize(password), salt, iterations)
end

#server_key

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 49

def server_key;       HMAC(salted_password, "Server Key") end

#server_signature

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 52

def server_signature; HMAC(server_key, auth_message)      end

#stored_key

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 50

def stored_key;       H(client_key)                       end

XOR(str1, str2)

[ GitHub ]

  
# File 'lib/net/imap/sasl/scram_algorithm.rb', line 28

def XOR(str1, str2)
  str1.unpack("C*")
    .zip(str2.unpack("C*"))
    .map {|a, b| a ^ b }
    .pack("C*")
end