11import { Command , flags } from '@oclif/command'
22import * as CryptoJS from 'crypto-js'
3+
34import Logger from '../utilities/Logger'
5+
46import Hash from './hash'
57
68export default class Crypto extends Command {
@@ -23,84 +25,82 @@ export default class Crypto extends Command {
2325 async run ( ) {
2426 const { args, flags} = this . parse ( Crypto )
2527
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
2729 args . type = flags . encryption ? flags . encryption : flags . decryption //type like AES,DES
2830
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 )
3133 }
3234
33- private Encrypt ( flags : any , args :any ) {
35+ private Encrypt ( flags : any , args : any ) {
3436 let crypto = this . getCryptoType ( args . type )
35- Logger . info ( this , `Encryption: ${ flags . encryption . toUpperCase ( ) } ` )
37+ Logger . info ( this , `Encryption: ${ flags . encryption . toUpperCase ( ) } ` )
3638 // @ts -ignore // as crypto will never be undefined and reach here
3739 let encrypted : string = crypto . encrypt ( args . string , flags . key , {
38- mode : this . getCryptoMode ( this , flags )
40+ mode : this . getCryptoMode ( flags )
3941 } ) . toString ( )
40- Logger . success ( this , `${ encrypted } ` )
42+ Logger . success ( this , `${ encrypted } ` )
4143 }
4244
4345 private Decrypt ( flags : any , args : any ) {
4446 let crypto = this . getCryptoType ( args . type )
45- Logger . info ( this , `Decryption: ${ flags . decryption . toUpperCase ( ) } ` )
47+ Logger . info ( this , `Decryption: ${ flags . decryption . toUpperCase ( ) } ` )
4648 // @ts -ignore // as crypto will never be undefined and reach here
4749 let decrypted : string = crypto . decrypt ( args . string , flags . key , {
48- mode : this . getCryptoMode ( this , flags )
50+ mode : this . getCryptoMode ( flags )
4951 } ) . toString ( CryptoJS . enc . Utf8 )
50- Logger . success ( this , `${ decrypted } ` )
52+ Logger . success ( this , `${ decrypted } ` )
5153 }
5254
5355 private getCryptoType ( type : string ) {
5456 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' )
7071 }
7172 }
7273
7374 // to check required parameters passed or not
7475 private checkParameters ( flags : any , args : any ) {
7576 if ( ! flags . key )
76- Logger . error ( this , 'Key is not passed' )
77+ Logger . error ( this , 'Key is not passed' )
7778
78- if ( args . string == undefined || args . string == "" )
79+ if ( args . string === undefined || args . string === '' )
7980 Logger . error ( this , 'Input string is empty or undefined' )
8081
8182 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' )
8384
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' )
8687 }
8788
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 )
9293 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' )
104104 }
105105 }
106106
0 commit comments