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

49 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-03-03 15:23:46 +00:00
import crypto from "crypto";
import { inject, injectable } from "tsyringe";
import { TimeUtil } from "./TimeUtil";
@injectable()
export class HashUtil
{
constructor(
@inject("TimeUtil") protected timeUtil: TimeUtil
)
{ }
/**
* Create a 24 character id using the sha256 algorithm + current timestamp
* @returns 24 character hash
*/
public generate(): string
{
const shasum = crypto.createHash("sha256");
const time = Math.random() * this.timeUtil.getTimestamp();
shasum.update(time.toString());
return shasum.digest("hex").substring(0, 24);
}
public generateMd5ForData(data: string): string
{
return this.generateHashForData("md5", data);
}
public generateSha1ForData(data: string): string
{
return this.generateHashForData("sha1", data);
}
/**
* 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");
}
}