Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, each character of <code>md5</code> is a hex bit. That means it can have one of 16 possible values. So if you're only using the first 4 "hex-bits", that means you can have <code>16 * 16 * 16 * 16</code> or <code>16^4</code> or 65536 or <code>2^16</code> possibilities. </p> <p>So, that means that the total available "space" for results is only 16 bits wide. Now, according to the <a href="http://en.wikipedia.org/wiki/Birthday_attack" rel="nofollow">Birthday Attack/Problem</a>, there are the following chances for collision:</p> <ul> <li><code>50%</code> chance -> <code>300</code> entries</li> <li><code>1%</code> chance -> <code>36</code> entries</li> <li><code>0.0000001%</code> chance -> <code>2</code> entries.</li> </ul> <p>So there is quite a high chance for collisions.</p> <p>Now, you say you need a 4 character hash. Depending on the exact requirements, you can do:</p> <ul> <li>4 hex-bits for <code>16^4</code> (65,536) possible values </li> <li>4 alpha bits for <code>26^4</code> (456,976) possible values </li> <li>4 alpha numeric bits for <code>36^4</code> (1,679,616) possible values </li> <li>4 ascii printable bits for about <code>93^4</code> (74,805,201) possible values (assuming ASCII 33 -> 126)</li> <li>4 full bytes for <code>256^4</code> (4,294,967,296) possible values.</li> </ul> <p>Now, which you choose will depend on the actual use case. Does the hash need to be transmitted to a browser? How are you storing it, etc.</p> <p>I'll give an example of each (In PHP, but should be easy to translate / see what's going on):</p> <p><strong>4 Hex-Bits</strong>:</p> <pre><code>$hash = substr(md5($data), 0, 4); </code></pre> <p><strong>4 Alpha bits</strong>:</p> <pre><code>$hash = substr(base_convert(md5($data), 16, 26)0, 4); $hash = str_replace(range(0, 9), range('S', 'Z'), $hash); </code></pre> <p><strong>4 Alpha Numeric bits</strong>:</p> <pre><code>$hash = substr(base_convert(md5($data), 16, 36), 0, 4); </code></pre> <p><strong>4 Printable Assci Bits</strong>:</p> <pre><code>$hash = hash('md5', $data, true); // We want the raw bytes $out = ''; for ($i = 0; $i &lt; 4; $i++) { $out .= chr((ord($hash[$i]) % 93) + 33); } </code></pre> <p><strong>4 full bytes</strong>:</p> <pre><code>$hash = substr(hash('md5', $data, true), 0, 4); // We want the raw bytes </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload