User Tools

Site Tools


λ Fowler Noll Vo hash string

======= Algorithm ======= <syntax js> function fnv1aHashString(string) { // Returns the FNV-1a hash of a string. // 32-bit FNV offset basis and prime. var fnvPrime = 16777619; var hash = 2166136261; for (var i=0, c=string.length; i<c; i++) { // Each char code is two octets of data. var charCode = string.charCodeAt(i); // Mask to get the first octet, and add it to the hash. var firstOctet = (charCode & 0xFF); hash = hash ^ firstOctet; // Bitwise OR with zero to ensure a 32bit integer. hash = (hash * fnvPrime) | 0; // Shift to get the second byte, and add it to the hash. var secondOctet = (charCode >> 8); hash = hash ^ secondOctet; hash = (hash * fnvPrime) | 0; } return hash; } </syntax> ======= Support ======= <syntax js> function run(string) { hash = fnv1aHashString(string); } </syntax> ======= Tests ======= <syntax js> </syntax> ======= Options ======= <syntax js> { "title":"Fowler Noll Vo hash string", "height":"340px" } </syntax> ======= Visualisation ======= <syntax html> <html> <head> <style> input { width: 100px; } #binary { font-family: monospace; } #charcode { font-family: monospace; } .highlight { color: red; } </style> <script src="http://d3js.org/d3.v3.min.js"></script> </head> <body> <div style="padding:2px" id="buttons"> Input: <input id="input" value="hello ★" onchange="reset()"></br> Hash: <div id="binary"></div> CharCode: <span id="char"></span> <div id="charcode"></div> </div> <div id="visualisation"></div> <script type="text/javascript"> function args() { return document.getElementById("input").value; } function binary(num) { var binary = num.toString(2); if (binary[0] === '-') { binary = binary.substr(1); } while (binary.length < 32) { binary = "0"+binary; } return binary; } function update(n, x, isRunning, duration, prev) { if (duration < 0) { return; // State-less visualisation. } if (x) { var hash = x.lookupInScope("hash"); var charcode = x.lookupInScope("charCode"); var string = x.lookupInScope("string"); var i = x.lookupInScope("i"); if (typeof hash === "number") { document.getElementById("binary").innerHTML = binary(hash) + " ("+hash+")"; } else { document.getElementById("binary").innerHTML = "&nbsp;"; } if (typeof string === "string" && typeof i === "number" && i<string.length) { document.getElementById("char").innerHTML = "'"+string[i]+"'"; if (typeof charcode === "number") { var binaryStr = binary(charcode); var html = binaryStr; if (n.lineno > 18) { html = binaryStr.slice(0, 16)+"<span class=highlight>"+binaryStr.slice(16,24)+"</span>"+binaryStr.slice(24); } else if (n.lineno > 13) { html = binaryStr.slice(0, 24)+"<span class=highlight>"+binaryStr.slice(24)+"</span>"; } document.getElementById("charcode").innerHTML = html; } else { document.getElementById("charcode").innerHTML = "&nbsp;"; } } else { document.getElementById("char").innerHTML = ""; document.getElementById("charcode").innerHTML = "&nbsp;"; } } } </script> </body> </html> </syntax>

algorithm/fowler_noll_vo_hash-string.txt · Last modified: 2015/02/02 08:28 (external edit)