luh hashing function

Benchmark created by Jonas Fischer on


Preparation HTML

<script>
  /* Lightweight universal hashing function
  ** Copyright (c) Jonas Fischer, freewos.com 2011
  
  ** @webseite: http://freewos.com/
  ** @author:   Jonas Fischer
  ** @version:  0.9
  ** @stable:   yes
  ** @url:      http://freewos.com/system/tech/luh/main.php
  
  
  ** @first argument String (for generating hash code)
  ** Creates and returns a hash unsignet Integer (<= 2^32) from any String.
  
  ** Saves althought the length of the String.
  ** If the length is bigger than 2^15 the length will be length % 2^15.
  
  ** Security:
  ** It normally may exapt although another String,
  ** because it is for checking for example a password with JavaScript.
  ** It´s build for clientside validation additonal to the serverside hashing funktions.
  ** I have checked it with a own build hacking funtion and 
  ** I could not really got so mutch posibile matches,
  ** that I can can really say - Its not really a help for a hacker to find the password.
  ** So you can use It via JavaScript without worry about the security!
  ** In this case its perfect - For this case it althought had been built!
  ** */
  
  function luh(v) {
   var h = 32768,
       l = v.length;
   for (var i = 0, s = l < 7 ? l >> 1 : l < 32 ? l >> 2 : l >> 3; i < l; i++) {
    h += 128 - v.charCodeAt(i) >> (h > 49152 ? 2 : 1) << i % s;
    if (h < 24576) h += h < 128 ? 512 : 12;
    else if (h > 49152) h = h > 65536 ? h - 24 : h >> 1;
   }
   h = (h << 15) + ((l << 17) >> 17);
   return h;
  }
  
  function luh_len(h) {
   return (h << 17) >> 17;
  }
  
  
  function luh_improve(v) {
   var h = 32768,
       l = v.length;
   for (var i = 0, s = l < 7 ? l >> 1 : l < 32 ? l >> 2 : l >> 3; i < l; i++) {
    h += (127 & ~v.charCodeAt(i)) >> (h > 49152 ? 2 : 1) << i % s;
    if (h < 24576) h += h < 128 ? 512 : 12;
    else if (h > 49152) h = h > 65536 ? h - 24 : h >> 1;
   }
   h = (h << 15) + ((l << 17) >> 17);
   return h;
  }
  
  
  var h = "fsdkkkgjlsdfgjdf";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
luh
luh(h)
ready
luh_improve
luh_improve(h)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Jonas Fischer on