Commit 54d8fea2 authored by Philipp Hörist's avatar Philipp Hörist

Add compare argument to compute_caps_hash()

parent f57bd1ae
......@@ -112,13 +112,26 @@ AdHocCommandNote = namedtuple('AdHocCommandNote', 'text type')
IBBData = namedtuple('IBBData', 'block_size sid seq type data')
IBBData.__new__.__defaults__ = (None, None, None, None, None)
DiscoInfo = namedtuple('DiscoInfo', 'jid node identities features dataforms')
DiscoItems = namedtuple('DiscoItems', 'jid node items')
DiscoItem = namedtuple('DiscoItem', 'jid name node')
DiscoItem.__new__.__defaults__ = (None, None)
class DiscoInfo(namedtuple('DiscoInfo', 'jid node identities features dataforms')):
__slots__ = []
def __new__(cls, jid, node, identities, features, dataforms):
return super(DiscoInfo, cls).__new__(cls, jid, node, identities,
features, dataforms)
def get_caps_hash(self):
return self.node.split('#')[1]
except Exception:
return None
class DiscoIdentity(namedtuple('DiscoIdentity', 'category type name lang')):
__slots__ = []
......@@ -257,12 +257,14 @@ def text_to_color(text, background_color):
return rc, gc, bc
def compute_caps_hash(info):
def compute_caps_hash(info, compare=True):
Compute caps hash according to XEP-0115, V1.5
:param: info DiscoInfo
:param: compare If True an exception is raised if the hash announced in
the node attr is not equal to what is calculated
# Initialize an empty string S.
string_ = ''
......@@ -367,4 +369,8 @@ def compute_caps_hash(info):
string_ += '%s<' % value
hash_ = hashlib.sha1(string_.encode())
return b64encode(hash_.digest())
b64hash = b64encode(hash_.digest())
if compare and b64hash != info.get_caps_hash():
raise DiscoInfoMalformed('Caps hashes differ: %s != %s' % (
b64hash, info.get_caps_hash()))
return b64hash
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment