I recently had a need to cache the results of decryption of an encrypted string so that decryption only runs once, and ended up using a cache where the 'key' of the cache is the hash of the string. Here's the hashing functions I have:
hashOfString=(s:string):string=>{lethash=0;leti=0;letchr=0;if(s.length===0)returnhash.toString();for(i=0;i<s.length;i++){chr=s.charCodeAt(i);hash=((hash<<5)-hash)+chr;hash|=0;// Convert to 32bit integer}returnhash.toString();}hashOfObject=(obj:Object):string=>{if(!obj)return"null";returnthis.hashOfString(JSON.stringify(obj));}
Although the above isn't perfectly good in terms of avoiding hash collisions. I guess I should boost it up to a SHA-256 to be more 'solid' code.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I recently had a need to cache the results of decryption of an encrypted string so that decryption only runs once, and ended up using a cache where the 'key' of the cache is the hash of the string. Here's the hashing functions I have:
Although the above isn't perfectly good in terms of avoiding hash collisions. I guess I should boost it up to a SHA-256 to be more 'solid' code.