add caching to hashes

This commit is contained in:
Mystikfluu 2023-02-04 12:02:59 +01:00
parent 9b78633c03
commit 24bd16a6d7

View File

@ -1,4 +1,14 @@
import crypto from "crypto"; import crypto from "crypto";
let SHA256_cache = {}
function _SHA256(str) {
return crypto
.createHash("sha256")
.update(str)
.digest("base64");
}
/** /**
* hashes with the secure hashing algorithm 256 * hashes with the secure hashing algorithm 256
* @param {string} str string to hash * @param {string} str string to hash
@ -11,13 +21,18 @@ function SHA256(str, salt, num) {
num = 1; num = 1;
if (!str) if (!str)
return; return;
let identifier = _SHA256(str+salt+num.toString())
if(SHA256_cache[identifier] != undefined) {
return SHA256_cache[identifier];
}
let ret = str; let ret = str;
for (let i = 0; i < num; i++) { for (let i = 0; i < num; i++) {
ret = crypto ret = _SHA256(ret + salt)
.createHash("sha256")
.update(ret + salt)
.digest("base64");
} }
SHA256_cache[identifier] = ret;
setTimeout(()=>{
SHA256_cache[identifier] = undefined
},10000) //cache for 10s
return ret; return ret;
} }
export { SHA256 }; export { SHA256 };