@codingtools/cdt
Version:
CLI for Developers
84 lines • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const command_1 = require("@oclif/command");
const crypto_js_1 = tslib_1.__importDefault(require("crypto-js"));
const logger_1 = tslib_1.__importDefault(require("../utilities/logger"));
const utilities_1 = tslib_1.__importDefault(require("../utilities/utilities"));
// import Hashes from 'jshashes'
// TODO: all are Hexadecimal encoding for now, can also add b64
class Hash extends command_1.Command {
// only 2 parameters required HASH_TYPE and INPUT_STRING
async run() {
const { args, flags } = this.parse(Hash);
flags.type = this.getHashType(flags); //by default let it be sha1
args.string = utilities_1.default.getInputString(this, flags, args); // from either -s,-f or args
//check params after evaluating all
this.checkParameters(flags, args);
this.calculateHash(flags, args);
}
// to check required parameters passed or not
// tslint:disable-next-line:no-unused
checkParameters(flags, args) {
if (args.string === undefined || args.string === '')
logger_1.default.error(this, 'Input string is empty or undefined');
}
calculateHash(flags, args) {
const hashObject = this.getHashObject(flags);
// @ts-ignore
let hashed = hashObject(args.string);
logger_1.default.success(this, `[${flags.type.toUpperCase()}] ${hashed}`);
if (flags.output) { // if output path is provided then write to file also
utilities_1.default.writeStringToFile(this, flags.output, hashed);
}
}
// BACKUP function
// private getHashObject2(flags: any) {
// switch (flags.type.toUpperCase()) {
// case 'SHA1':
// return new Hashes.SHA1().hex
// case 'SHA256':
// return new Hashes.SHA256().hex
// case 'SHA512':
// return new Hashes.SHA512().hex
// case 'MD5':
// return new Hashes.MD5().hex
// case 'RMD160':
// return new Hashes.RMD160().hex
// default:
// Logger.error(this, 'Invalid Or Unsupported hash type')
// }
// }
getHashObject(flags) {
switch (flags.type.toUpperCase()) {
case 'SHA1':
return crypto_js_1.default.SHA1;
case 'SHA256':
return crypto_js_1.default.SHA256;
case 'SHA512':
return crypto_js_1.default.SHA512;
case 'MD5':
return crypto_js_1.default.MD5;
case 'RMD160':
case 'RIPEMD160':
return crypto_js_1.default.RIPEMD160;
default:
logger_1.default.error(this, 'Invalid Or Unsupported hash type');
}
}
getHashType(flags) {
return flags.type || 'sha1';
}
}
exports.default = Hash;
Hash.description = 'Hashing functionality for a string/file';
Hash.flags = {
help: command_1.flags.help({ char: 'h' }),
// -t , --type for hashing
type: command_1.flags.string({ char: 't', description: 'type of hash [SHA1(default), MD5, SHA256, SHA512, RMD160 or RIPEMD160]' }),
string: command_1.flags.string({ char: 's', description: 'string to be hashed' }),
file: command_1.flags.string({ char: 'f', description: 'file to be hashed' }),
output: command_1.flags.string({ char: 'o', description: 'output file path' }),
};
Hash.args = [{ name: 'string' }];
//# sourceMappingURL=hash.js.map