@@ -22,50 +22,55 @@ export default class Hash extends Command {
22
22
async run ( ) {
23
23
const { args, flags} = this . parse ( Hash )
24
24
25
- const type : string = flags . type || 'sha1' //by default let it be sha1
25
+ // only 2 parameters required HASH_TYPE and INPUT_STRING
26
+ flags . type = Hash . getHashType ( flags ) //by default let it be sha1
27
+ flags . string = Hash . getInputString ( flags , args ) // from either -s,-f or args
26
28
27
- // if -s or -f is not passed we will take it from args
28
- let str = ''
29
-
30
- if ( flags . string ) //if -s given
31
- str = flags . string
32
- else if ( flags . file ) {
33
- Logger . info ( this , `reading file: ${ flags . file } ` )
34
- str = Utilities . getStringFromFile ( this , flags . file )
35
- } else
36
- str = args . string
37
-
38
- this . calculateHash ( type , str )
29
+ this . calculateHash ( flags )
39
30
}
40
31
41
- private calculateHash ( type : string , str : string ) {
42
- let hash : Hashes
43
- switch ( type . toUpperCase ( ) ) {
44
- case 'SHA1' :
45
- hash = new Hashes . SHA1 ( )
46
- break
47
- case 'SHA256' :
48
- hash = new Hashes . SHA256 ( )
49
- break
50
- case 'SHA512' :
51
- hash = new Hashes . SHA512 ( )
52
- break
53
- case 'MD5' :
54
- hash = new Hashes . MD5 ( )
55
- break
56
- case 'RMD160' :
57
- hash = new Hashes . RMD160 ( )
58
- break
59
- default :
60
- hash = undefined
61
- }
32
+ private calculateHash ( flags : any ) {
33
+ const hashObject = Hash . getHashObject ( flags )
62
34
63
- if ( hash ) {
64
- let hashed : string = hash . hex ( str )
65
- Logger . success ( this , `[${ type . toUpperCase ( ) } ] ${ hashed } ` )
35
+ if ( hashObject ) {
36
+ let hashed : string = hashObject . hex ( flags . string )
37
+ Logger . success ( this , `[${ flags . type . toUpperCase ( ) } ] ${ hashed } ` )
66
38
} else {
67
39
Logger . error ( this , 'Invalid Or Unsupported hash type' )
68
40
}
69
41
}
70
42
43
+ private static getHashObject ( flags : any ) {
44
+ switch ( flags . type . toUpperCase ( ) ) {
45
+ case 'SHA1' :
46
+ return new Hashes . SHA1 ( )
47
+ case 'SHA256' :
48
+ return new Hashes . SHA256 ( )
49
+ case 'SHA512' :
50
+ return new Hashes . SHA512 ( )
51
+ case 'MD5' :
52
+ return new Hashes . MD5 ( )
53
+ case 'RMD160' :
54
+ return new Hashes . RMD160 ( )
55
+ default :
56
+ return undefined
57
+ }
58
+ }
59
+
60
+ private static getHashType ( flags : any ) {
61
+ return flags . type || 'sha1'
62
+ }
63
+
64
+ private static getInputString ( flags : any , args :any ) {
65
+ // if -s or -f is not passed we will take it from args
66
+ let str = ''
67
+ if ( flags . string ) //if -s given
68
+ str = flags . string
69
+ else if ( flags . file ) {
70
+ Logger . info ( this , `reading file: ${ flags . file } ` )
71
+ str = Utilities . getStringFromFile ( this , flags . file )
72
+ } else
73
+ str = args . string
74
+ return str ;
75
+ }
71
76
}
0 commit comments