[Openstack] [openstack][swift]make swift.common.utils.streq_const_time more efficient

Mike Green iasybvm at gmail.com
Thu Sep 13 07:05:05 UTC 2012


def streq_const_time(s1, s2):

    if len(s1) != len(s2):
        return False
    result = 0
    for (a, b) in zip(s1, s2):
        result |= ord(a) ^ ord(b)
    return result == 0

+++++++++++++++++++++++++++++++++++++++++

If s1 and s2 are of the same length,  then the function will compare every
characters in them.  I think it may be more efficient as follow:

def streq_const_time(s1, s2):

    if len(s1) != len(s2):
        return False
    result = 0
    for (a, b) in zip(s1, s2):
        if ord(a) ^ ord(b):
          return False
    return True
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstack.org/pipermail/openstack/attachments/20120913/b330d56c/attachment.html>


More information about the Openstack mailing list