reuse errors.JS and hash.JS
from https://codeberg.org/buzzcode2007/FCC-Project-URLShortener/src/branch/main/scripts/utilities
This commit is contained in:
parent
0902ec9cb2
commit
f966e0564e
2 changed files with 63 additions and 0 deletions
38
scripts/utilities/hash.JS
Normal file
38
scripts/utilities/hash.JS
Normal file
|
@ -0,0 +1,38 @@
|
|||
class Hash {
|
||||
#data;
|
||||
|
||||
constructor(DATA, METHOD) {
|
||||
this.#data = DATA;
|
||||
this.algorithm = (METHOD) ? METHOD : "SHA-512";
|
||||
this.arrayBuffer = crypto.subtle.digest(this.algorithm, (new TextEncoder()).encode(DATA))
|
||||
};
|
||||
|
||||
/*
|
||||
Represent the hash in another format.
|
||||
|
||||
@param {string} TYPE the output format
|
||||
@return {object} the converted hash
|
||||
*/
|
||||
async convert(TYPE) {
|
||||
if (TYPE) {
|
||||
switch (TYPE) {
|
||||
case `Uint8Array`:
|
||||
this[TYPE] = new Uint8Array(await this.arrayBuffer);
|
||||
break;
|
||||
case `Array`:
|
||||
this[TYPE] = Array.from(new Uint8Array(await this.arrayBuffer));
|
||||
break;
|
||||
case `Number`:
|
||||
this[TYPE] = parseInt((Array.from(new Uint8Array(await this.arrayBuffer))).join());
|
||||
break;
|
||||
case `String`:
|
||||
this[TYPE] = Array.from(new Uint8Array(await this.arrayBuffer)).join(``);
|
||||
};
|
||||
|
||||
return(this[TYPE])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = Hash;
|
Loading…
Add table
Add a link
Reference in a new issue