IGA/hash.ts
2025-05-17 17:58:36 +02:00

60 lines
No EOL
1.7 KiB
TypeScript

import IGA from './IGA';
import fs from 'fs';
const args = process.argv.slice(2)
const version = "1.1"
function printHelp() {
console.log("IGA commandline tool v" + version)
console.log("Usage: tsx hash.ts <hash|verify> <input file> [<hash to verify>] [options]");
console.log("Options:");
console.log("[<salt = 0>] [<length of the hash = 32>]");
}
if (args[0] == "hash") {
if (args.length < 2) {
console.log("Not enough arguments");
printHelp()
process.exit(0)
}
if (!fs.existsSync(args[1])) {
console.log("File not found: " + args[1])
process.exit(0)
}
let fileData = fs.readFileSync(args[1], 'utf8');
let salt = args[2] ? parseInt(args[2]) : 0
let len = args[4] ? parseInt(args[4]) : 32
try {
let hash = IGA.hash(fileData, salt, len)
console.log(hash)
} catch (e) {
console.error("Error while creating hash: " + e.stack)
}
} else if (args[0] == "verify") {
if (args.length < 3) {
console.log("Not enough arguments");
printHelp()
process.exit(0)
}
if (!fs.existsSync(args[1])) {
console.log("File not found: " + args[1])
process.exit(0)
}
let fileData = fs.readFileSync(args[1], 'utf8');
let hashToVerify = args[2]
let salt = args[3] ? parseInt(args[3]) : 0
let len = args[5] ? parseInt(args[5]) : 32
try {
let verify = IGA.verifyHash(fileData, hashToVerify, salt, len)
if (verify.valid) {
console.log("Hash is valid!")
} else {
console.log("Hash is invalid!")
console.log("Reason: " + verify.reason)
}
} catch (e) {
console.error("Error while verifying hash: " + e.stack)
}
} else {
printHelp()
}