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,27 +16,17 @@ 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 )
@@ -55,6 +46,10 @@ export default class Hash extends Command {
55
46
// @ts -ignore
56
47
let hashed : string = hashObject ( args . string )
57
48
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
+ }
58
53
}
59
54
60
55
// BACKUP function
0 commit comments