Python String Comparison function()
Question: I am checking a piece of Python code I found online (<http://www.exploit-
db.com/exploits/18305/>), and I'm stuck over a piece of code.

To be honest I don't know Python, but I have experience in other programming
languages.

The method `_computeCollisionChars` generates a number of characters and then
adds them to a dictionary if they are _different_. Below is the method
signature along with the relevant part for the question. **Note** : The actual
code can be found on the linked source.

    
    
    def _computeCollisionChars(self, function, count, charrange):
        baseStr = ""
        baseHash = function(baseStr) # What is this?
    
        for item in source:
            tempStr = ""
    
            if tempStr == baseStr:
                continue
            if function(tempStr) == baseHash: # What type of comparison is this?
                # logic goes here...
    
        return
    

My questions are:

  1. What does the `function` parameter mean in the method signature?
  2. Most importantly what does `function(string)` do? Why are there two checks, and what sort of output does `function(tempStr)` generate then?

Thanks a lot guys!


Answer: Apparently you can pass any callable object as `function` to
`_computeCollisionChars`. Then `baseHash` is initialised as the result of
calling `function` with an empty string as parameter. Inside the loop, the
condition reads: if the result of `function` called with an empty string as
parameter equals the `baseHash` do this and that. Which is kind of senseless,
because `tempStr` is always `''` and `baseHash` never changes (or you didn't
post that part).

In the current snippet the second `if` is never reached, because invariably
`tempStr == baseStr == ''`.

As the commentors pointed out, in the real code `tempStr` and `baseStr` do
indeed change and `function` is expected to be a hashing-function (but any
other function which takes a string as argument should work).

