modded7/tools/util/hashes.ts
IntegerLimit 5aecff8b4d
Allow for QB Translations of Normal and Expert Quest Books (#432)
[FEATURE]

/* Description: */
Also moves all nodejs stuff into `tools` package.

Also adds a basic `CONTRIBUTING.md` file. 

Superseeds https://github.com/Nomi-CEu/Nomi-CEu/pull/431, which was closed as the branch needed to be renamed, due to the last two items on this list.

TODO:
- [x] Make it only run on master repo.
- [x] Remove qb-lang-revamp branch from the events
- [x] Allow workflow to run on all branches starting with test_buildscript?

/* Commits: */

* Buildscript Changes

* Make Contributing Docs a .md file

* updateqb.yml v1

* Test QB change

* Add lang file to commit file list

* Another Test Change

* Test with only lang path, + `./` at beginning of path

* Test QB change

* updateqb.yml v3

* Test QB Change

* Add some debug settings

* Remove debug, branch input. Switch to auto-commit action

* Make activate: branches prefix `test_buildscript`, commit author GHA bot

* Add branch prefix note into CONTRIBUTING.md

* Make workflow only run if on master repo
2023-09-17 22:44:48 +10:00

45 lines
1.1 KiB
TypeScript

import _sha1 from "sha1";
import _md5 from "md5";
import { HashDef } from "../types/hashDef";
/**
* Returns the hash sum of bytes of given bytes using SHA1.
*
* This is what CurseForge and Forge are using to check files.
*/
export const sha1 = (inputBuffer: Buffer): string => {
return _sha1(inputBuffer);
};
/**
* Returns the hash sum of bytes of given bytes using MD5.
*
* This is what CF is using to check files.
*/
export const md5 = (inputBuffer: Buffer): string => {
return _md5(inputBuffer);
};
const hashFuncs: { [key: string]: (buffer: Buffer) => string } = {
sha1,
md5,
};
/**
* Compare buffer to the given HashDef.
*
* @param {Buffer} buffer
* @param {HashDef} hashDef
*
* @throws {Error} Throws a generic error if hashes don't match.
*/
export const compareBufferToHashDef = (buffer: Buffer, hashDef: HashDef): boolean => {
if (!hashFuncs[hashDef.id]) {
throw new Error(`No hash function found for ${hashDef.id}.`);
}
const sum = hashFuncs[hashDef.id](buffer);
return (Array.isArray(hashDef.hashes) && hashDef.hashes.includes(sum)) || hashDef.hashes == sum;
};