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,31 +16,25 @@ 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 )
42- this . calculateHash ( flags , args )
33+ let hashedString = this . calculateHash ( flags , args )
34+
35+ if ( flags . outputFile ) { // if output path is provided then write to file also
36+ Utilities . writeStringToFile ( this , flags . outputFile , hashedString )
37+ }
4338 }
4439
4540 // to check required parameters passed or not
@@ -55,6 +50,7 @@ export default class Hash extends Command {
5550 // @ts -ignore
5651 let hashed : string = hashObject ( args . string )
5752 Logger . success ( this , `[${ flags . type . toUpperCase ( ) } ] ${ hashed } ` )
53+ return hashed
5854 }
5955
6056 // BACKUP function
0 commit comments