11import { Command , flags } from '@oclif/command'
22import * as CryptoJS from 'crypto-js'
3- // import * as Hashes from 'jshashes'
43
54import Logger from '../utilities/logger'
65import Utilities from '../utilities/utilities'
6+ // import * as Hashes from 'jshashes'
7+
78// TODO: all are Hexadecimal encoding for now, can also add b64
89
910export default class Hash extends Command {
@@ -15,27 +16,17 @@ export default class Hash extends Command {
1516 type : flags . string ( { char : 't' , description : 'type of hash [SHA1(default), MD5, SHA256, SHA512, RMD160 or RIPEMD160]' } ) ,
1617 string : flags . string ( { char : 's' , description : 'string to be hashed' } ) ,
1718 file : flags . string ( { char : 'f' , description : 'file to be hashed' } ) ,
19+ outputFile : flags . string ( { char : 'o' , description : 'output file path' } ) ,
1820 }
1921
2022 static args = [ { name : 'string' } ]
2123
22- static getInputString ( thisRef : any , flags : any , args : any ) { //need to make it static so Crypto can use this
23- // if -s or -f is not passed we will take it from args
24- if ( flags . string ) //if -s given
25- return flags . string
26- else if ( flags . file ) {
27- Logger . info ( thisRef , `reading file: ${ flags . file } ` )
28- return Utilities . getStringFromFile ( thisRef , flags . file )
29- } else
30- return args . string
31- }
32-
3324 // only 2 parameters required HASH_TYPE and INPUT_STRING
3425 async run ( ) {
3526 const { args, flags} = this . parse ( Hash )
3627
3728 flags . type = this . getHashType ( flags ) //by default let it be sha1
38- args . string = Hash . getInputString ( this , flags , args ) // from either -s,-f or args
29+ args . string = Utilities . getInputString ( this , flags , args ) // from either -s,-f or args
3930
4031 //check params after evaluating all
4132 this . checkParameters ( flags , args )
@@ -55,6 +46,10 @@ export default class Hash extends Command {
5546 // @ts -ignore
5647 let hashed : string = hashObject ( args . string )
5748 Logger . success ( this , `[${ flags . type . toUpperCase ( ) } ] ${ hashed } ` )
49+
50+ if ( flags . outputFile ) { // if output path is provided then write to file also
51+ Utilities . writeStringToFile ( this , flags . outputFile , hashed )
52+ }
5853 }
5954
6055 // BACKUP function
0 commit comments