UNPKG

jquery-hash

Version:

jQuery plugin to return a hash for a string using SHA 1/256/384/512 and the web crypto api

8 lines (7 loc) 797 B
(function($){function str2u8(string){let arrayBuffer=new ArrayBuffer(string.length*1);let newUint=new Uint8Array(arrayBuffer);newUint.forEach((_,i)=>{newUint[i]=string.charCodeAt(i);});return newUint;} function u82str(STR){let str='' for(var i=0;i<STR.byteLength;i++){str+=String.fromCharCode(STR[i])} return str;} function s2h(str){try{let hex='';for(var i=0;i<str.length;i++){hex+=''+str.charCodeAt(i).toString(16);} return hex;}catch(err){if(err){return console.log(err)}}} $.hash=function(str,digest,enc,cb){window.crypto.subtle.digest({name:'SHA-'+digest},str2u8(str)).then(function(res){if(enc==='base64'){cb(btoa(u82str(new Uint8Array(res))))}else if(enc==='Uint8'){cb(new Uint8Array(res))}else{cb(s2h(u82str(new Uint8Array(res))))}}).catch(function(err){console.error(err);});}}(jQuery));