1
1
import { Command , flags } from '@oclif/command'
2
2
import * as CryptoJS from 'crypto-js'
3
+
3
4
import Logger from '../utilities/Logger'
5
+
4
6
import Hash from './hash'
5
7
6
8
export default class Crypto extends Command {
@@ -23,84 +25,82 @@ export default class Crypto extends Command {
23
25
async run ( ) {
24
26
const { args, flags} = this . parse ( Crypto )
25
27
26
- args . string = Hash . getInputString ( this , flags , args ) //always add input to args
28
+ args . string = Hash . getInputString ( this , flags , args ) //always add input to args
27
29
args . type = flags . encryption ? flags . encryption : flags . decryption //type like AES,DES
28
30
29
- this . checkParameters ( flags , args )
30
- flags . encryption ? this . Encrypt ( flags , args ) : this . Decrypt ( flags , args )
31
+ this . checkParameters ( flags , args )
32
+ flags . encryption ? this . Encrypt ( flags , args ) : this . Decrypt ( flags , args )
31
33
}
32
34
33
- private Encrypt ( flags : any , args :any ) {
35
+ private Encrypt ( flags : any , args : any ) {
34
36
let crypto = this . getCryptoType ( args . type )
35
- Logger . info ( this , `Encryption: ${ flags . encryption . toUpperCase ( ) } ` )
37
+ Logger . info ( this , `Encryption: ${ flags . encryption . toUpperCase ( ) } ` )
36
38
// @ts -ignore // as crypto will never be undefined and reach here
37
39
let encrypted : string = crypto . encrypt ( args . string , flags . key , {
38
- mode : this . getCryptoMode ( this , flags )
40
+ mode : this . getCryptoMode ( flags )
39
41
} ) . toString ( )
40
- Logger . success ( this , `${ encrypted } ` )
42
+ Logger . success ( this , `${ encrypted } ` )
41
43
}
42
44
43
45
private Decrypt ( flags : any , args : any ) {
44
46
let crypto = this . getCryptoType ( args . type )
45
- Logger . info ( this , `Decryption: ${ flags . decryption . toUpperCase ( ) } ` )
47
+ Logger . info ( this , `Decryption: ${ flags . decryption . toUpperCase ( ) } ` )
46
48
// @ts -ignore // as crypto will never be undefined and reach here
47
49
let decrypted : string = crypto . decrypt ( args . string , flags . key , {
48
- mode : this . getCryptoMode ( this , flags )
50
+ mode : this . getCryptoMode ( flags )
49
51
} ) . toString ( CryptoJS . enc . Utf8 )
50
- Logger . success ( this , `${ decrypted } ` )
52
+ Logger . success ( this , `${ decrypted } ` )
51
53
}
52
54
53
55
private getCryptoType ( type : string ) {
54
56
switch ( type . toUpperCase ( ) ) {
55
- case 'AES' :
56
- return CryptoJS . AES
57
- case 'DES' :
58
- return CryptoJS . DES
59
- case '3DES' :
60
- return CryptoJS . TripleDES
61
- case 'RABBIT' :
62
- return CryptoJS . Rabbit
63
- case 'RC4' :
64
- return CryptoJS . RC4
65
- case 'RC4DROP' :
66
- return CryptoJS . RC4Drop
67
- default :
68
- Logger . error ( this , 'Invalid or Unsupported Encryption/Decryption type' )
69
- return undefined // will never reach here
57
+ case 'AES' :
58
+ return CryptoJS . AES
59
+ case 'DES' :
60
+ return CryptoJS . DES
61
+ case '3DES' :
62
+ return CryptoJS . TripleDES
63
+ case 'RABBIT' :
64
+ return CryptoJS . Rabbit
65
+ case 'RC4' :
66
+ return CryptoJS . RC4
67
+ case 'RC4DROP' :
68
+ return CryptoJS . RC4Drop
69
+ default :
70
+ Logger . error ( this , 'Invalid or Unsupported Encryption/Decryption type' )
70
71
}
71
72
}
72
73
73
74
// to check required parameters passed or not
74
75
private checkParameters ( flags : any , args : any ) {
75
76
if ( ! flags . key )
76
- Logger . error ( this , 'Key is not passed' )
77
+ Logger . error ( this , 'Key is not passed' )
77
78
78
- if ( args . string == undefined || args . string == "" )
79
+ if ( args . string === undefined || args . string === '' )
79
80
Logger . error ( this , 'Input string is empty or undefined' )
80
81
81
82
if ( flags . encryption && flags . decryption )
82
- Logger . error ( this , 'Both encryption and decryption methods passed' )
83
+ Logger . error ( this , 'Both encryption and decryption methods passed' )
83
84
84
- if ( ! ( flags . encryption || flags . decryption ) )
85
- Logger . error ( this , 'Neither encryption or decryption methods passed' )
85
+ if ( ! ( flags . encryption || flags . decryption ) )
86
+ Logger . error ( this , 'Neither encryption or decryption methods passed' )
86
87
}
87
88
88
- private getCryptoMode ( thisRef : any , flags : any ) {
89
- if ( ! flags . mode ) //set default
90
- flags . mode = 'CBC' // it will not set to flags.mode there in run() but we do not require it
91
- Logger . info ( this , 'Block Mode: ' + flags . mode )
89
+ private getCryptoMode ( flags : any ) {
90
+ if ( ! flags . mode ) //set default
91
+ flags . mode = 'CBC' // it will not set to flags.mode there in run() but we do not require it
92
+ Logger . info ( this , 'Block Mode: ' + flags . mode )
92
93
switch ( flags . mode . toUpperCase ( ) ) {
93
- case 'CBC' :
94
- return CryptoJS . mode . CBC
95
- case 'CFB' :
96
- return CryptoJS . mode . CFB
97
- case 'OFB' :
98
- return CryptoJS . mode . OFB
99
- case 'ECB' :
100
- return CryptoJS . mode . ECB
101
- default :
102
- Logger . error ( this , 'Invalid or Unsupported Block Mode' )
103
- return undefined // will never reach here
94
+ case 'CBC' :
95
+ return CryptoJS . mode . CBC
96
+ case 'CFB' :
97
+ return CryptoJS . mode . CFB
98
+ case 'OFB' :
99
+ return CryptoJS . mode . OFB
100
+ case 'ECB' :
101
+ return CryptoJS . mode . ECB
102
+ default :
103
+ Logger . error ( this , 'Invalid or Unsupported Block Mode' )
104
104
}
105
105
}
106
106
0 commit comments