Пример #1
0
        def hash_encode(arg):
            """encode a data type to bytes"""
            if type(arg) is bytes:
                s = arg
            elif type(arg) is ec_element:
                s = serialize(arg)
            elif type(arg) is str:
                s = arg.encode('utf-8')
            elif type(arg) is int:
                s = arg.to_bytes((arg.bit_length() + 7) // 8, 'little')
            elif isinstance(args, tuple):
                # based on TupleHash (see NIST SP 800-185)
                def left_encode(x):
                    # This implictly checks for validity conditions:
                    # An exception is raised if n > 255, i.e., if len(x) > 2**2040
                    n = (x.bit_length() + 7 ) // 8
                    return n.to_bytes(1, 'little') + x.to_bytes(n, 'little')

                s = b''
                for arg in args:
                    z = hash_encode(arg)
                    # concat with encode_string(z)
                    s += left_encode(len(z)) + z
            else:
                raise ValueError("unexpected type to hash: {}".format(type(arg)))

            return s
Пример #2
0
 def hash(self, args, _type=ZR):
     if isinstance(args, tuple):
         s = bytes()
         for i in args:
             if type(i) == elliptic_curve:
                 s += serialize(i)
             elif type(i) == str:
                 s += str(i)
             # consider other types    
         #print("s => %s" % s)
         return hashEC(self.group, str(s), _type)
     elif type(args) == elliptic_curve:
         msg = str(serialize(args))
         return hashEC(self.group, msg, _type)
     elif type(args) == str:
         return hashEC(self.group, args, _type)
     return None
Пример #3
0
 def hash(self, args, _type=ZR):
     if isinstance(args, tuple):
         s = bytes()
         for i in args:
             if type(i) == elliptic_curve:
                 s += serialize(i)
             elif type(i) == str:
                 s += str(i)
             # consider other types
         #print("s => %s" % s)
         return hashEC(self.group, str(s), _type)
     elif type(args) == elliptic_curve:
         msg = str(serialize(args))
         return hashEC(self.group, msg, _type)
     elif type(args) == str:
         return hashEC(self.group, args, _type)
     return None
Пример #4
0
 def hash(self, args, target_type=ZR):
     """hashes objects into ZR or G"""
     if isinstance(args, tuple):
         s = bytes()
         for i in args:
             if type(i) == ec_element:
                 s += serialize(i)
             elif type(i) == str:
                 s += bytes(str(i), 'utf8')
             elif type(i) == bytes:
                 s += i
             else:
                 print("unexpected type: ", type(i))
             # consider other types
         #print("s => %s" % s)
         assert len(s) != 0, "hash input is empty."
         return hashEC(self.ec_group, str(s), target_type)
     elif type(args) == ec_element:
         msg = str(serialize(args))
         return hashEC(self.ec_group, msg, target_type)
     elif type(args) in [str, bytes]:
         return hashEC(self.ec_group, args, target_type)
     raise Exception("ECGroup - invalid input for hash")
Пример #5
0
 def hash(self, args, target_type=ZR):
     """hashes objects into ZR or G"""        
     if isinstance(args, tuple):
         s = bytes()
         for i in args:
             if type(i) == ec_element:
                 s += serialize(i)
             elif type(i) == str:
                 s += bytes(str(i), 'utf8')
             elif type(i) == bytes:
                 s += i
             else:
                 print("unexpected type: ", type(i))
             # consider other types    
         #print("s => %s" % s)
         assert len(s) != 0, "hash input is empty."
         return hashEC(self.ec_group, str(s), target_type)
     elif type(args) == ec_element:
         msg = str(serialize(args))
         return hashEC(self.ec_group, msg, target_type)
     elif type(args) in [str, bytes]:
         return hashEC(self.ec_group, args, target_type)
     raise Exception("ECGroup - invalid input for hash")
Пример #6
0
 def serialize(self, object):
     return serialize(object)
Пример #7
0
 def serialize(self, object):
     """serializes a pairing object into bytes"""
     return serialize(object)
Пример #8
0
 def serialize(self, object):
     """serializes a pairing object into bytes"""        
     return serialize(object)
Пример #9
0
 def serialize(self, object):
     return serialize(object)