Babban matsalar ƙirƙirar kalmar sirri ta hash a cikin JavaScript shine cewa yana da sauƙin zato. Kalmar sirrin hash shine kawai jerin haruffan da aka yi hashed, ko kuma aka canza su zuwa lamba ta musamman, sannan a adana su a kwamfutar mai amfani. Duk wanda ya san kalmar sirrin hash zai iya shiga cikin asusun mai amfani cikin sauki ba tare da ya tuna ainihin kalmar sirri ba.
var password = ""; var salt = ""; function hashPassword(password, salt) { var hash = CryptoJS.SHA256(password + salt); return hash.toString(CryptoJS.enc.Hex); }
var kalmar sirri = "";
Wannan layin yana ƙirƙirar maɓalli da ake kira kalmar sirri kuma yana saita shi daidai da igiya mara komai.
gishiri = "";
Wannan layin yana haifar da canji mai suna gishiri kuma ya saita shi daidai da igiya mara komai.
aiki hashPassword (kalmar sirri, gishiri) {
var hash = CryptoJS.SHA256 (kalmar sirri + gishiri);
mayar da hash.toString(CryptoJS.enc.Hex);
}
Wannan aikin yana ɗaukar sigogi biyu, kalmar sirri da gishiri, kuma yana dawo da sigar kalmar sirri ta hashed ta amfani da SHA256 algorithm da tsarin ɓoye na Hex.
Hash kalmomin shiga
Hash kalmomin shiga wani nau'in kalmar sirri ne da ke amfani da aikin hash na sirri don ƙirƙirar kalmar sirri ta musamman ga kowane mai amfani. Aikin hash yana ɗaukar kirtan shigarwa kuma yana samar da tsayayyen igiyar fitarwa, wanda ake kira ƙimar hash. Ƙimar zanta ta keɓanta ga kowane kirtani na shigarwa kuma baya da alaƙa da asalin shigar da kirtani.
Don ƙirƙirar kalmar sirri ta hash, da farko kuna buƙatar samar da zantan sirri na shaidar shiga mai amfani. Kuna iya yin wannan ta amfani da MD5 ko SHA-1 hashing algorithm, ya danganta da dandalin da kuke amfani da su. Na gaba, kuna buƙatar adana ƙimar hash a cikin amintaccen wuri akan sabar ku. A duk lokacin da masu amfani da ku suka shiga, za su buƙaci shigar da takaddun shaidar shiga cikin aikace-aikacenku sannan su yi amfani da ƙimar thehash don samar da sabon kalmar sirri.
Yi aiki tare da hashes
A cikin JavaScript, ana amfani da hashes don wakiltar tsararraki. Misali, lambar da ke biyowa ta ƙirƙiri tsararrun kirtani kuma tana adana shi a cikin madaidaicin mai suna myArray:
myArray = ["a", "b", "c"];
Hakanan zaka iya amfani da hashes don wakiltar wasu nau'ikan bayanai. Misali, lambar mai zuwa ta ƙirƙiri zanta wanda ke adana ƙimar "1" da "2":
zanta = {1: "1", 2: "2"}