1
1
import { Command , flags } from '@oclif/command'
2
2
import * as CryptoJS from 'crypto-js'
3
- // import * as Hashes from 'jshashes'
4
3
5
4
import Logger from '../utilities/logger'
6
5
import Utilities from '../utilities/utilities'
6
+ // import * as Hashes from 'jshashes'
7
+
7
8
// TODO: all are Hexadecimal encoding for now, can also add b64
8
9
9
10
export default class Hash extends Command {
@@ -15,31 +16,25 @@ export default class Hash extends Command {
15
16
type : flags . string ( { char : 't' , description : 'type of hash [SHA1(default), MD5, SHA256, SHA512, RMD160 or RIPEMD160]' } ) ,
16
17
string : flags . string ( { char : 's' , description : 'string to be hashed' } ) ,
17
18
file : flags . string ( { char : 'f' , description : 'file to be hashed' } ) ,
19
+ outputFile : flags . string ( { char : 'o' , description : 'output file path' } ) ,
18
20
}
19
21
20
22
static args = [ { name : 'string' } ]
21
23
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
-
33
24
// only 2 parameters required HASH_TYPE and INPUT_STRING
34
25
async run ( ) {
35
26
const { args, flags} = this . parse ( Hash )
36
27
37
28
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
39
30
40
31
//check params after evaluating all
41
32
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
+ }
43
38
}
44
39
45
40
// to check required parameters passed or not
@@ -55,6 +50,7 @@ export default class Hash extends Command {
55
50
// @ts -ignore
56
51
let hashed : string = hashObject ( args . string )
57
52
Logger . success ( this , `[${ flags . type . toUpperCase ( ) } ] ${ hashed } ` )
53
+ return hashed
58
54
}
59
55
60
56
// BACKUP function
0 commit comments