title: Two-way encoding function that returns short hashes
body: <p>We are building a URL shortening functionality for our site. </p>
  
  <p>What we've come up at the moment: </p>
  
  <ul>
  <li>We take a URL (<a href="http://www.google.com" rel="nofollow noreferrer">http://www.google.com</a>) and sha1 it and end up with a 40 character hash (738ddf35b3a85a7a6ba7b232bd3d5f1e4d284ad1).</li>
  <li>We take the sha1 hash and encode it into base62 (basically A-Z, a-z, 0-9) and end up with a 28 character hash (jNMYchEoche67ro1k5gsCcHfDzmR) that we can decode back to the original sha1. </li>
  </ul>
  
  <p>The reason we use sha1 is to make sure users cannot guess the next URL from the current / past  URL(s).</p>
  
  <p>The reason we use base62 is to make the URL valid and readable to the user.</p>
  
  <p>Now a 28 character "short-url" that will be appended to our domain (<a href="http://www.google.com/r/jNMYchEoche67ro1k5gsCcHfDzmRis" rel="nofollow noreferrer">http://www.google.com/r/jNMYchEoche67ro1k5gsCcHfDzmRis</a>) is a bit too long, especially when you consider Twitters character limit. </p>
  
  <p>What we're currently considering is cutting the sha1 down by around 20 characters, which would produce a 14 character short-url, but going down any further and we're worried we will run into collisions too quickly. </p>
  
  <p>We also thought about <a href="https://stackoverflow.com/questions/1355100/compressing-big-number-or-string-to-small-value/1355701#1355701">Compressing big number (or string) to small value</a> but that would require us to split the 28 or 14 character hash into pieces of 2 and sort the pieces and we have no idea how to return to the original hash from there. </p>
  
  <p>Does anyone have any idea on what we could do? We would prefer a solution where we do not depend on the database to construct the URL, but if a DB is required please keep in mind that we're limited to Redis / MongoDB (which means no auto-increment integer field).</p>