def set_password(self, s, password): socket = self.socket[s] salt, v = SRP.new_passwd(self.co.user, password) cypherv = crypt(long_to_string(v), socket['key'])[0] self._send_msg(s, {'op': 'set password', 's': salt, 'v': cypherv}) socket['state'] = 5 self.rs.listen_forever(self.nh) self.rs.doneflag.clear()
def crypt(text, key, counter=0L): keylen, length = len(key), len(text) pos, cyphertext = 0, [] while pos < length: scounter = long_to_string(counter, keylen) hash = sha.new("ctr mode crypt" + key + scounter).digest() for i in xrange(min(length-pos, len(hash))): cyphertext.append(chr(ord(hash[i]) ^ ord(text[pos]))) pos += 1 counter += 1 return (''.join(cyphertext), counter)
def hash(s): """Hash a value with some hashing algorithm.""" if type(s) != type(''): s = long_to_string(s) return sha.new(s).digest()
def host_authenticator(K, A, m): A = long_to_string(A, 128) return hmac.new(K, A + m, sha)
def _client_authenticator(K, n, g, user, s, A, B, u): A = long_to_string(A, 128) B = long_to_string(B, 128) u = long_to_string(u, 128) return hmac.new(K, hash(n) + hash(g) + hash(user) + s + A + B + u, sha)