Converting a number back to it's original string (that was hashed to generate that number)

Lele Gaifax lele at metapensiero.it
Wed Jan 23 08:06:47 EST 2013


Ferrous Cranus <nikos.gr33k at gmail.com> writes:

> pin = int( htmlpage.encode("hex"), 16 ) % 100000
>
> Now i need the opposite procedure.

As already said several times by different persons in this thread, there
is no way you can get the original string that originated a particular
“pin”: the function you are using is “lossy”, that is, information gets
lost in order to reduce a BIG string into a SMALL five-digits integer
number.

>  Will hex.decode(number) convert back to the original string?

NO. As people demonstrated you, you are going to meet collisions very
fast, if you insist going this way (even you thought a “smarter” way to
get a checksum out of your string by using a different weight for the
single characters, there is still high chances of collisions, not
counting the final “modulo” operation). Once you get such a collision,
there is not enough information in that single tiny number to get back a
single string that generated it.

Imagine that, instead of using an integer checksum of your full path,
you “shrink” it by replacing each name in the path with its starting
letter, that is:

 /home/ferrous/public_html/index.html => /h/f/p/i

That is evidently way shorter of the original, but you LOST information,
and you cannot write code in any language that eventually reproduce the
original.

The only way out is either use the fullpath as the primary key of your
table, or using a mapping table with a bi-directional univoke mapping
between any single fullpath to the corresponding "short" integer value.

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele at metapensiero.it  |                 -- Fortunato Depero, 1929.




More information about the Python-list mailing list