473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Integer to code and back

Tom
I need to convert an integer to a unique code string consisting only of
capital letters and numbers.
I also need to be able to convert it back to an integer again.

Example:

10 =DFG56JKG4 =10

How do I accomplish that?
Googling has not helped.

Regards

Jan 3 '07 #1
25 1960
On Wed, 03 Jan 2007 19:05:14 +0100, Tom wrote:
I need to convert an integer to a unique code string consisting only of
capital letters and numbers.
I also need to be able to convert it back to an integer again.

Example:

10 =DFG56JKG4 =10

How do I accomplish that?
Googling has not helped.

Regards
What is the logic used to map from integer to string?

Jan 3 '07 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dan M wrote:
On Wed, 03 Jan 2007 19:05:14 +0100, Tom wrote:
>10 =DFG56JKG4 =10

How do I accomplish that?

What is the logic used to map from integer to string?
I guess that Tom has absolutely no idea on how to do that.

Viable possibilities:
- - A hash table.
- - A simetric encryption function.

- --
- ----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
MSN:i_*************************@hotmail.com
Jabber:iv*********@jabber.org ; iv*********@kdetalk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFFnAFRR56dWuhgxGgRAtXIAKCC1sid+P5QVExaUxpJF0 pGqpyhoACfYoA7
UhwYA0Cl03R6ZUwG+8C1jMw=
=K7s3
-----END PGP SIGNATURE-----
Jan 3 '07 #3
Tom
>>
>What is the logic used to map from integer to string?

I guess that Tom has absolutely no idea on how to do that.
Exactly!

I have triede base64_decode / encode but it fails as it uses non
alphanumerics.
So does base32

Viable possibilities:
- - A hash table.
Hmm doesn't ring a bell, and google gives me oodles of stuff that is not
directly related.
Could you please provide me with an example in pseudo code or something?
- - A simetric encryption function.
Well, same result when googling. The concept exists but apparently everybody
knows it so well they don't explain it but just refer to it :-D
An example please?

Thanks for answering!

Tom
Jan 3 '07 #4
..oO(Tom)
>I need to convert an integer to a unique code string consisting only of
capital letters and numbers.
I also need to be able to convert it back to an integer again.

Example:

10 =DFG56JKG4 =10
Just curious: Why do you need that? The integer itself is unique and
consists only of numbers ... so what's the purpose of the "encoding"?

Micha
Jan 3 '07 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tom wrote:
>- - A hash table.

Hmm doesn't ring a bell, and google gives me oodles of stuff that is not
directly related.
Could you please provide me with an example in pseudo code or something?
Basically, you generate a table that you store somewhere, and relates every
integer you can think of with its hash.

Got a new integer? Calculate the hash and store it in the table. Got a hash?
Check if it exists in the table.

How you exactly hash the integers is your problem, tough.
>- - A simetric encryption function.

Well, same result when googling. The concept exists but apparently
everybody knows it so well they don't explain it but just refer to it :-D
An example please?
RTFM. php.net/mcrypt

- --
- ----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFFnB3ER56dWuhgxGgRArsGAJ0WTtAWZfRU+TfGWf8ZtN WC1Y+KMACfcXBu
QH8OlXlAYsPnd1lNVrkgHlQ=
=8jDe
-----END PGP SIGNATURE-----
Jan 3 '07 #6
..oO(Iván Sánchez Ortega)
>Basically, you generate a table that you store somewhere, and relates every
integer you can think of with its hash.
A hash is not unique.

Micha
Jan 3 '07 #7
Tom
Got a new integer? Calculate the hash and store it in the table. Got a
hash?
Check if it exists in the table.
Ah, good idea. Thanks.
>>- - A simetric encryption function.

Well, same result when googling. The concept exists but apparently
everybody knows it so well they don't explain it but just refer to it :-D
An example please?

RTFM. php.net/mcrypt

1) How was I supposed to link "simetric encryption" to "mcrypt" ??? :-D :-D
2) Are you sure you it shouldn't be spelled "symmetric" ?

Tom
Jan 3 '07 #8
Tom

"Michael Fesser" <ne*****@gmx.dewrote in message
news:rn********************************@4ax.com...
.oO(Tom)
>>I need to convert an integer to a unique code string consisting only of
capital letters and numbers.
I also need to be able to convert it back to an integer again.

Example:

10 =DFG56JKG4 =10

Just curious: Why do you need that? The integer itself is unique and
consists only of numbers ... so what's the purpose of the "encoding"?
The purpose is to use it in links.
Each code will link to a given row in a database and then show an image.
But I don't want script kiddies to fetch all images iteratively :-)

Tom
Jan 3 '07 #9
Michael Fesser wrote:
.oO(Tom)
I need to convert an integer to a unique code string consisting only of
capital letters and numbers.
I also need to be able to convert it back to an integer again.

Example:

10 =DFG56JKG4 =10

Just curious: Why do you need that? The integer itself is unique and
consists only of numbers ... so what's the purpose of the "encoding"?

Micha
I'm curious as well...a number is already unique, what benefit are you
looking to get from the encoding?

Heck, if all you want is a way to convert a number to an alphanumeric
string and back reliably without any kind of obfuscation, then you
could just convert the int to hex and voila, you've got an alphanumeric
string that contains 0-9 and A-F which can easily be converted back to
decimal notation.

- Moot

Jan 3 '07 #10
"Tom" <no@spam.pleasewrote in message
news:45***********************@news.sunsite.dk...
2) Are you sure you it shouldn't be spelled "symmetric" ?

I'm pretty sure it's "symmetric", but let's find out, shall we...
http://www.googlefight.com/index.php...word2=simetric

"symmetric" 28,000,000 results vs. "simetric" 104,000 results...

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Jan 4 '07 #11
Michael Fesser wrote:
A hash is not unique.
For small[1] integers $i, md5($i) should be unique.

____
1. Say, lower than 1,000,000,000

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 4 '07 #12
Tom wrote:
10 =DFG56JKG4 =10

How do I accomplish that?
How about this...

define('MYSECRET','Some unguessable string');

function int2code ($i)
{
$md5 = strtoupper(md5(MYSECRET.$i));
$mdStart = substr($md5, 0, 4);
$mdEnd = substr($md5, 28, 4);
return $mdStart.$i.$mdEnd;
}

function code2int ($code)
{
preg_match('/^([A-F0-9]{4})([0-9]+)([A-F0-9]{4})$/', $code, $matches);
$i = $matches[2];

$md5 = strtoupper(md5(MYSECRET.$i));
$mdStart = substr($md5, 0, 4);
$mdEnd = substr($md5, 28, 4);
if ($mdStart.$i.$mdEnd==$code)
return $i;

return FALSE; // Invalid code!
}

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 4 '07 #13
Iván Sánchez Ortega wrote:
Viable possibilities:
- - A hash table.
- - A simetric encryption function.
Possibility three:
- including the unencrypted integer in the query string, but signing it.

I've posted example code for this in my reply to Tom's post.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 4 '07 #14
"Toby Inkster" <us**********@tobyinkster.co.ukwrote in message
news:j2************@ophelia.g5n.co.uk...
Michael Fesser wrote:
>A hash is not unique.

For small[1] integers $i, md5($i) should be unique.

Unique yes, but md5 is one-way, and the op wanted a way to convert integers
to strings and back to integers. How do you convert an md5 digest to
integer?

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Jan 4 '07 #15
Tom wrote:
The purpose is to use it in links.
Each code will link to a given row in a database and then show an image.
But I don't want script kiddies to fetch all images iteratively :-)
Use "wget -r" and say good-bye to your "security".

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Now listening to: Culture Beat - Metamorphosis0 - [3] Metamorphosis (9:33)
(95%)
Jan 4 '07 #16
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kimmo Laine wrote:
Unique yes, but md5 is one-way, and the op wanted a way to convert
integers to strings and back to integers. How do you convert an md5 digest
to integer?
Using a rainbow table :-)

- --
- ----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFFnOqpR56dWuhgxGgRAgW5AJ0TGd+tYjIbbEDfxKbt9i j+uKyghgCfWnU2
MPQvyObryuZ2XS7gzs7EUhc=
=8j+t
-----END PGP SIGNATURE-----
Jan 4 '07 #17
Tom
Hey

Wow!

Nice, exactly what I needed.

Thanks a lot!

Tom
"Toby Inkster" <us**********@tobyinkster.co.ukwrote in message
news:2r************@ophelia.g5n.co.uk...
Tom wrote:
>10 =DFG56JKG4 =10

How do I accomplish that?

How about this...

define('MYSECRET','Some unguessable string');

function int2code ($i)
{
$md5 = strtoupper(md5(MYSECRET.$i));
$mdStart = substr($md5, 0, 4);
$mdEnd = substr($md5, 28, 4);
return $mdStart.$i.$mdEnd;
}

function code2int ($code)
{
preg_match('/^([A-F0-9]{4})([0-9]+)([A-F0-9]{4})$/', $code, $matches);
$i = $matches[2];

$md5 = strtoupper(md5(MYSECRET.$i));
$mdStart = substr($md5, 0, 4);
$mdEnd = substr($md5, 28, 4);
if ($mdStart.$i.$mdEnd==$code)
return $i;

return FALSE; // Invalid code!
}

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 4 '07 #18
Tom

"Tom" <no@spam.pleasewrote in message
news:45***********************@news.sunsite.dk...
Hey

Wow!

Nice, exactly what I needed.
Well, maybe I rushed to conclusions there :-D

I'm not too fond of having the unencrypted integer sitting right there in
all hashes :-D

But, it should be fairly straightforward to convert it to something else and
back.

Thanks :-)
Jan 4 '07 #19
Tom wrote:
"Michael Fesser" <ne*****@gmx.dewrote in message
news:rn********************************@4ax.com...
>>.oO(Tom)

>>>I need to convert an integer to a unique code string consisting only of
capital letters and numbers.
I also need to be able to convert it back to an integer again.

Example:

10 =DFG56JKG4 =10

Just curious: Why do you need that? The integer itself is unique and
consists only of numbers ... so what's the purpose of the "encoding"?


The purpose is to use it in links.
Each code will link to a given row in a database and then show an image.
But I don't want script kiddies to fetch all images iteratively :-)

Tom

So why only numbers and capital letters? Why not lower case, also?

Or, a better question - why not just store a hash in the table itself
and forget about converting back?

Personally I think I'd just mcrypt_enc() the auto increment field with a
key and store it in the row. Then use this value in the requests.

P.S. I would use mcrypt() because it does create unique values, unlike
hashing algorithms like md5().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 4 '07 #20
Tom wrote:
I'm not too fond of having the unencrypted integer sitting right there in
all hashes :-D
What's wrong with having the unencrypted integer there? As long as you
make sure you *check* the hashes (which code2int does!), then you're safe.

<?php
// listing.php
require 'coding_stuff.php';
print "<ul>\n";
for ($i=1; $i<=10; $i++)
{
$c = int2code($i);
print "<li><a href=\"view.php?number={$c}\">view {$i}</a>\n";
}
print "</ul>\n";
?>

<?php
// view.php
require 'coding_stuff.php';
$c = $_GET['number'];
$i = code2int($c);
if ($i!==FALSE)
{
// show the image here.
}
?>

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 4 '07 #21
Kimmo Laine wrote:
Unique yes, but md5 is one-way, and the op wanted a way to convert integers
to strings and back to integers. How do you convert an md5 digest to
integer?
Brute force; or saved lookup table.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 4 '07 #22
Tom

"Toby Inkster" <us**********@tobyinkster.co.ukwrote in message
news:b9************@ophelia.g5n.co.uk...
Tom wrote:
>I'm not too fond of having the unencrypted integer sitting right there in
all hashes :-D

What's wrong with having the unencrypted integer there?
Well, remember I wanted the integer ENCRYPTED :-D

I said the integer came from an autonumber column, så fetch three hashes in
a row and you can easily spot the pattern.

But thanks for the attempt :-)

Tom
Jan 4 '07 #23
Tom wrote:
I said the integer came from an autonumber column, så fetch three
hashes in a row and you can easily spot the pattern.
Who cares that you can spot a pattern though? I don't see who that helps
the script kiddies, given that they can't generate the pattern themselves.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 4 '07 #24
Tom

"Toby Inkster" <us**********@tobyinkster.co.ukwrote in message
news:1l************@ophelia.g5n.co.uk...
Tom wrote:
>I said the integer came from an autonumber column, så fetch three
hashes in a row and you can easily spot the pattern.

Who cares that you can spot a pattern though? I don't see who that helps
the script kiddies, given that they can't generate the pattern themselves.
Sorry, you're right.
Of course this won't help! DOH!

Note to self, don't write on usenet on low bllod sugar :-D

Task solved ;-)

Have a nice weekend,
Tom
Jan 4 '07 #25
On Wed, 03 Jan 2007 10:27:55 -0800, Dan M wrote:
What is the logic used to map from integer to string?
It's called "hex representation". There is a little known
function called "sprintf" which can accomplish that.

--
http://www.mladen-gogala.com
Jan 5 '07 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Faheem Mitha | last post by:
Hi, I'm not sure what would be more appropriate, so I'm ccing it to both alt.comp.lang.learn.c-c++ and comp.lang.python, with followup to alt.comp.lang.learn.c-c++. While working with a...
3
by: Nikola | last post by:
Hi all, I have a problem converting datetime to integer (and than back to datetime). Depending whether the time is AM or PM, same date is converted to two different integer representations,...
20
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg.,...
8
by: Candace | last post by:
I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the...
1
by: charles_gero | last post by:
Hi all, I had a question about the topics in the subject and posted to comp.std.c, but feel it may also be appropriate here. Please excuse this crosspost if it is in bad form. I have a...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
7
by: Spoon | last post by:
Hello everyone, In my code, I use uint16_t (exactly 16-bit-wide unsigned integer type) and I need to print their value in base 10. ...
12
by: lithiumcat | last post by:
Hi, I bothered you a while back about storing integer values in void*. Now in a completely unrelated context, I'm trying to store pointer values in an integer type. So the basic question is,...
9
by: tsuyois | last post by:
Hi, I just signed in to this excellent network. I hope I could get some answers to many questions I have in writing C compilers. My first question is: Is "integer demotion" required in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.