mirror of
https://github.com/Nystik-gh/ignis.git
synced 2026-06-17 04:35:53 +00:00
some styling cleanup
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
export function createHash(algorithm) {
|
||||
const alg = algorithm.toUpperCase().replace("-", "");
|
||||
|
||||
const subtleAlg =
|
||||
alg === "SHA256"
|
||||
? "SHA-256"
|
||||
@@ -16,32 +17,47 @@ export function createHash(algorithm) {
|
||||
if (typeof data === "string") {
|
||||
data = new TextEncoder().encode(data);
|
||||
}
|
||||
|
||||
const merged = new Uint8Array(inputData.length + data.length);
|
||||
|
||||
merged.set(inputData);
|
||||
merged.set(data, inputData.length);
|
||||
|
||||
inputData = merged;
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
digest(encoding) {
|
||||
console.warn("[shim:crypto] createHash.digest - using placeholder");
|
||||
|
||||
const hash = simpleHash(inputData);
|
||||
if (encoding === "hex") return hash;
|
||||
if (encoding === "base64") return btoa(hash);
|
||||
|
||||
if (encoding === "hex") {
|
||||
return hash;
|
||||
}
|
||||
|
||||
if (encoding === "base64") {
|
||||
return btoa(hash);
|
||||
}
|
||||
|
||||
return hash;
|
||||
},
|
||||
|
||||
async digestAsync(encoding) {
|
||||
const hashBuffer = await crypto.subtle.digest(subtleAlg, inputData);
|
||||
const hashArray = new Uint8Array(hashBuffer);
|
||||
|
||||
if (encoding === "hex") {
|
||||
return Array.from(hashArray)
|
||||
.map((b) => b.toString(16).padStart(2, "0"))
|
||||
.join("");
|
||||
}
|
||||
|
||||
if (encoding === "base64") {
|
||||
return btoa(String.fromCharCode(...hashArray));
|
||||
}
|
||||
|
||||
return hashArray;
|
||||
},
|
||||
};
|
||||
@@ -49,8 +65,10 @@ export function createHash(algorithm) {
|
||||
|
||||
function simpleHash(data) {
|
||||
let hash = 0;
|
||||
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
hash = ((hash << 5) - hash + data[i]) | 0;
|
||||
}
|
||||
|
||||
return Math.abs(hash).toString(16).padStart(8, "0");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user