Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > hash from long url to short url

Reply
Thread Tools

hash from long url to short url

 
 
Paul Hsieh
Guest
Posts: n/a
 
      02-23-2008
On Feb 22, 5:26 am, joe <jcha...@gmail.com> wrote:
> Hello anyone knows how to write a funtion to genereate a tiny url with
> letters and numbers only. Something almost always unique. THanks.


As mentioned by santosh, in practice you can essentially try to
duplicate what the site "tinyUrl.com" does, however, either way it
requires that you solve the more general problem of mapping limitless
strings to fixed sized hashes that are "almost always unique". So let
us solve this latter problem before we address the whole problem.

To map a set of variable length strings to something "pseudo-unique"
fundamentally requires an understanding of "The Birthday
Paradox" (look it up on Wikipedia.) However to make a long story
short you should study "secure hashing". Open source implementations
of SHA-256 and WHIRLPOOL exist, which are probably the "safest" bets
right now. The upshot of this is that you can transform variable
length strings to something like 160 or 256 bits in a way that even an
expert adversary cannot find any collisions in.

Now for something like an URL, however, you are going to need to
transform the output to text. So use Base-64 or hex encoding or
something like that to turn the binary to alphanumerics. Lets call
this a "long encoding" of the URL.

Now as for making it tiny, the only way I can see how to do this is to
basically retain a database of all such hashings done and always
return the shortest possible truncated version of the "long
encoding" (which is calculated as described above) that is unique.
Then you can just make the URL something like: www.smalladdress.com/<URL>
and you would probably use some Apache configuration trick to turn
this into a call to php or cgi which takes the <URL> and looks it up
in your DB and produces the original URL as a redirect. I assume that
this, roughly speaking, is what tinyurl.com does.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      02-23-2008
"Default User" <> writes:

> joe wrote:
>
>> May be i could get the char value of each letter multiply it by the
>> position in the string and come up with a number. Just an idea.

>
> Please don't top-post. Your replies belong following or interspersed
> with properly trimmed quotes. See the majority of other posts in the
> newsgroup, or:
> <http://www.caliburn.nl/topposting.html>


CBFalconer and Default User actively contributing to CLC in their own
inimitable manner again. Nice.
 
Reply With Quote
 
 
 
 
Antoninus Twink
Guest
Posts: n/a
 
      02-24-2008
On 23 Feb 2008 at 20:37, Richard wrote:
> "Default User" <> writes:
>
>> joe wrote:
>>
>>> May be i could get the char value of each letter multiply it by the
>>> position in the string and come up with a number. Just an idea.

>>
>> Please don't top-post. Your replies belong following or interspersed
>> with properly trimmed quotes. See the majority of other posts in the
>> newsgroup, or:
>> <http://www.caliburn.nl/topposting.html>

>
> CBFalconer and Default User actively contributing to CLC in their own
> inimitable manner again. Nice.


Yes - with "contributors" like these, who needs trolls?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Having compilation error: no match for call to ‘(const __gnu_cxx::hash<long long int>) (const long long int&)’ veryhotsausage C++ 1 07-04-2008 05:41 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
longs, long longs, short short long ints . . . huh?! David Geering C Programming 15 01-11-2007 09:39 PM
unsigned short short? slougheed@gmail.com C++ 4 10-16-2006 11:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57