Skip to content

Commit c7fb2a1

Browse files
committed
[ISSUE#21]: output file added for hash
Signed-off-by: ashish <[email protected]> Signed-off-by: ashish <[email protected]>
1 parent 01bb819 commit c7fb2a1

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/commands/hash.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {Command, flags} from '@oclif/command'
22
import * as CryptoJS from 'crypto-js'
3-
// import * as Hashes from 'jshashes'
43

54
import Logger from '../utilities/logger'
65
import Utilities from '../utilities/utilities'
6+
// import * as Hashes from 'jshashes'
7+
78
// TODO: all are Hexadecimal encoding for now, can also add b64
89

910
export 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

src/utilities/utilities.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,12 @@ export default class Utilities {
3838
return args.string
3939
}
4040

41-
41+
public static writeStringToFile(thisRef: any, filePath: string, string: string) {
42+
if (!fs.existsSync(filePath)) {
43+
Logger.error(thisRef, `Could not find file: ${filePath}`) // this will output error and exit command
44+
} else {
45+
fs.writeFileSync(filePath, string)
46+
Logger.success(thisRef, `Hash written to file: ${filePath}`) // this will output error and exit command
47+
}
48+
}
4249
}

0 commit comments

Comments
 (0)