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

Ferrous Cranus nikos.gr33k at gmail.com
Wed Jan 23 10:30:49 EST 2013


Τη Τετάρτη, 23 Ιανουαρίου 2013 3:58:45 μ.μ. UTC+2, ο χρήστης Dave Angel έγραψε:
> On 01/23/2013 08:38 AM, Ferrous Cranus wrote:
> 
> > Please DON'T tell me to save both the pin <=> filepath and associate them (that can be done by SQL commands, i know)
> 
> > I will not create any kind of primary/unique keys to the database.
> 
> > I will not store the filepath into the database, just the number which indicates the filepath(html page).
> 
> > Also no external table associating fielpaths and numbers.
> 
> > i want this to be solved only by Python Code, not database oriented.
> 
> >
> 
> >
> 
> > That is:  I need to be able to map both ways, in a one to one relation, 5-digit-integer <=> string
> 
> >
> 
> > int( hex ( string ) ) can encode a string to a number. Can this be decoded back? I gues that can also be decoded-converted back because its not losing any information. Its encoding, not compressing.
> 
> >
> 
> > But it's the % modulo that breaks the forth/back association.
> 
> >
> 
> > So, the question is:
> 
> >
> 
> > HOW to map both ways, in a one to one relation, (5-digit-integer <=> string) without losing any information?
> 
> >
> 
> 
> 
> Simple.  Predefine the 100,000 legal strings, and don't let the user use 
> 
> anything else.  One way to do that would be to require a path string of 
> 
> no more than 5 characters, and require them all to be of a restricted 
> 
> alphabet of 10 characters.  (eg. the alphabet could be 0-9, which is 
> 
> obvious, or it could be ".aehilmpst" (no uppercase, no underscore, no 
> 
> digits, no non-ascii, etc.)
> 
> 
> 
> In the realistic case of file paths or URLs, it CANNOT be done.

OK, its not doable. I'll stop asking for it.
CHANGE of plans.
i will use the database solution which is the most easy wau to do it:

============================================================

	# insert new page record in table counters or update it if already exists
	try:
		cursor.execute( '''INSERT INTO counters(page, hits) VALUES(%s, %s) 
								ON DUPLICATE KEY UPDATE hits = hits + 1''', (htmlpage, 1) )
	except MySQLdb.Error, e:
		print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )
		
	# update existing visitor record if same pin and same host found
	try:
		cursor.execute( '''UPDATE visitors SET hits = hits + 1, useros = %s, browser = %s, date = %s WHERE pin = %s AND host = %s''', (useros, browser, date, page, host))
	except MySQLdb.Error, e:
		print ( "Error %d: %s" % (e.args[0], e.args[1]) )
	
	# insert new visitor record if above update did not affect a row
	if cursor.rowcount == 0:
		cursor.execute( '''INSERT INTO visitors(hits, host, useros, browser, date) VALUES(%s, %s, %s, %s, %s)''', (1, host, useros, browser, date) )

============================================================

I can INSERT a row to the table "counter"
I cannot UPDATE or INSERT into the table "visitors" without knowing the "pin" primary key number the database created.

Can you help on this please?




More information about the Python-list mailing list