0
0
mirror of https://github.com/sp-tarkov/server.git synced 2025-02-13 09:50:43 -05:00
server/project/src/utils/HashUtil.ts

58 lines
1.5 KiB
TypeScript
Raw Normal View History

import crypto from "node:crypto";
import fs from "node:fs";
import crc32 from "buffer-crc32";
import { mongoid } from "mongoid-js";
2023-03-03 15:23:46 +00:00
import { inject, injectable } from "tsyringe";
import { TimeUtil } from "@spt/utils/TimeUtil";
2023-03-03 15:23:46 +00:00
@injectable()
export class HashUtil
{
2023-11-15 20:35:05 -05:00
constructor(@inject("TimeUtil") protected timeUtil: TimeUtil)
{}
2023-03-03 15:23:46 +00:00
/**
* Create a 24 character id using the sha256 algorithm + current timestamp
* @returns 24 character hash
*/
public generate(): string
{
return mongoid();
2023-03-03 15:23:46 +00:00
}
public generateMd5ForData(data: string): string
{
return this.generateHashForData("md5", data);
}
public generateSha1ForData(data: string): string
{
return this.generateHashForData("sha1", data);
}
public generateCRC32ForFile(filePath: fs.PathLike): number
{
return crc32.unsigned(fs.readFileSync(filePath));
}
2023-03-03 15:23:46 +00:00
/**
* Create a hash for the data parameter
* @param algorithm algorithm to use to hash
* @param data data to be hashed
* @returns hash value
*/
public generateHashForData(algorithm: string, data: crypto.BinaryLike): string
{
const hashSum = crypto.createHash(algorithm);
hashSum.update(data);
return hashSum.digest("hex");
}
public generateAccountId(): number
{
const min = 1000000;
const max = 1999999;
return max > min ? Math.floor(Math.random() * (max - min + 1) + min) : min;
}
}