|
1 | 1 | import {Command, flags} from '@oclif/command'
|
2 | 2 | import * as CryptoJS from 'crypto-js'
|
| 3 | +import {JsonFormatter} from 'tslint/lib/formatters' |
3 | 4 |
|
4 | 5 | import Utilities from '../utilities/Utilities'
|
5 | 6 | import Logger from '../utilities/Logger'
|
| 7 | +import Hash from './hash' |
6 | 8 |
|
7 | 9 | export default class Crypto extends Command {
|
| 10 | + static ENCRYPTION = 'encryption' |
| 11 | + static DECRYPTION = 'decryption' |
| 12 | + |
8 | 13 | static description = 'Encryption and Decryption functionality'
|
9 | 14 | static flags = {
|
10 | 15 | help: flags.help({char: 'h'}),
|
11 | 16 |
|
12 |
| - encryption: flags.string({char: 'e', description: 'encryption type'}), |
13 |
| - decryption: flags.string({char: 'd', description: 'decryption type'}), |
| 17 | + encryption: flags.string({char: 'e', description: 'encryption type, Supported [AES, DES, 3DES, Rabbit, RC4, RC4Drop]'}), |
| 18 | + decryption: flags.string({char: 'd', description: 'decryption type, Supported [AES, DES, 3DES, Rabbit, RC4, RC4Drop]'}), |
14 | 19 | string: flags.string({char: 's' , description: 'string to be encrypted/decrypted'}),
|
15 | 20 | file: flags.string({char: 'f' , description: 'file to be encrypted/decrypted'}),
|
16 | 21 | key: flags.string({char: 'k' , description: 'key for encryption/decryption'}),
|
| 22 | + mode: flags.string({char: 'm' , description: 'Block Mode, Supported [CBC, CFB, CTR, OFB, ECB]'}) |
17 | 23 | }
|
18 | 24 |
|
| 25 | + static args = [{name: 'string'}] |
| 26 | + |
| 27 | + //need INPUT_STRING, TYPE_OF_CRYPTO , KEY, MODE |
19 | 28 | async run() {
|
20 | 29 | const {args, flags} = this.parse(Crypto)
|
21 | 30 |
|
22 |
| - let enc = CryptoJS.DES.encrypt('Message', 'Secret Passphrase').ciphertext |
| 31 | + args.string = Hash.getInputString(this,flags,args) //always add input to args |
| 32 | + args.type = flags.encryption ? flags.encryption : flags.decryption //type like AES,DES |
23 | 33 |
|
24 |
| - this.log(enc) |
| 34 | + this.checkParameters(flags,args) |
| 35 | + flags.encryption ? this.Encrypt(flags, args) : this.Decrypt(flags, args) |
25 | 36 |
|
26 |
| - // if -s or -f is not passed we will take it from args |
27 |
| - let str = '' |
| 37 | + } |
28 | 38 |
|
29 |
| - const {key, decryption, string, file, encryption} = flags |
| 39 | + private Encrypt(flags: any, args:any) { |
30 | 40 |
|
31 |
| - if (string) //if -s given |
32 |
| - str = string |
33 |
| - else if (file) { |
34 |
| - str = Utilities.getStringFromFile(this, file) |
35 |
| - } else |
36 |
| - str = args.string |
| 41 | + let crypto = this.getCryptoType(args.type) |
37 | 42 |
|
38 |
| - if (!key) { |
39 |
| - Logger.error(this,'Key is not passed') |
40 |
| - } |
| 43 | + Logger.info(this,`Encryption: ${flags.encryption.toUpperCase()}`) |
41 | 44 |
|
42 |
| - if (encryption) { |
43 |
| - if (decryption) // if both given |
44 |
| - Logger.error(this,'Both encryption and decryption methods passed') |
45 |
| - this.Encrypt(str, encryption, key) |
46 |
| - } else if (decryption) { |
47 |
| - this.Decrypt(str, decryption, key) |
48 |
| - } else { |
49 |
| - Logger.error(this,'Neither encryption or decryption methods passed') |
50 |
| - } |
51 |
| - } |
| 45 | + // @ts-ignore // as crypto will never be undefined and reach here |
| 46 | + let encrypted: string = crypto.encrypt(args.string, flags.key, { |
| 47 | + mode: this.getCryptoMode(this,flags) |
| 48 | + }).toString() |
52 | 49 |
|
53 |
| - private Encrypt(str: string, type: string, key: string | undefined) { |
54 |
| - let crypto = this.getCryptoType(type) |
55 |
| - |
56 |
| - if (crypto) { |
57 |
| - // @ts-ignore |
58 |
| - // let encrypted: string = crypto.encrypt(str, key, { |
59 |
| - // mode: CryptoJS.mode.CBC}).ciphertext.toString(CryptoJS.enc.Hex) |
60 |
| - let encrypted: string = crypto.encrypt(str, key).ciphertext |
61 |
| - this.log(`[${type.toUpperCase()}]: ${encrypted}`) |
62 |
| - } else { |
63 |
| - Logger.error(this,'invalid hash type') |
64 |
| - } |
| 50 | + //always add input to args |
| 51 | + |
| 52 | + Logger.success(this,`${encrypted}`) |
65 | 53 | }
|
66 | 54 |
|
67 |
| - private Decrypt(str: string, type: string, key: string | undefined) { |
68 |
| - let crypto = this.getCryptoType(type) |
| 55 | + private Decrypt(flags: any, args: any) { |
| 56 | + let crypto = this.getCryptoType(args.type) |
69 | 57 |
|
70 |
| - if (crypto) { |
71 |
| - // @ts-ignore |
72 |
| - let decrypted: string = crypto.decrypt(str, key) |
73 |
| - this.log(`[${type.toUpperCase()}]: ${decrypted}`) |
74 |
| - } else { |
75 |
| - Logger.error(this,'invalid hash type') |
76 |
| - } |
| 58 | + Logger.info(this,`Decryption: ${flags.decryption.toUpperCase()}`) |
| 59 | + |
| 60 | + // @ts-ignore // as crypto will never be undefined and reach here |
| 61 | + let decrypted: string = crypto.decrypt(args.string, flags.key, { |
| 62 | + mode: this.getCryptoMode(this,flags) |
| 63 | + }).toString(CryptoJS.enc.Utf8) |
| 64 | + |
| 65 | + Logger.success(this,`${decrypted}`) |
77 | 66 | }
|
78 | 67 |
|
79 | 68 | private getCryptoType(type: string) {
|
80 | 69 | switch (type.toUpperCase()) {
|
81 |
| - case 'AES': |
82 |
| - return CryptoJS.AES |
83 |
| - case 'DES': |
84 |
| - return CryptoJS.DES |
85 |
| - default: |
86 |
| - // tslint:disable-next-line:no-return-undefined |
87 |
| - return undefined //returning because of check there |
| 70 | + case 'AES': |
| 71 | + return CryptoJS.AES |
| 72 | + case 'DES': |
| 73 | + return CryptoJS.DES |
| 74 | + case '3DES': |
| 75 | + return CryptoJS.TripleDES |
| 76 | + case 'RABBIT': |
| 77 | + return CryptoJS.Rabbit |
| 78 | + case 'RC4': |
| 79 | + return CryptoJS.RC4 |
| 80 | + case 'RC4DROP': |
| 81 | + return CryptoJS.RC4Drop |
| 82 | + default: |
| 83 | + Logger.error(this,'Invalid or Unsupported Encryption/Decryption type') |
| 84 | + return undefined // will never reach here |
88 | 85 | }
|
89 | 86 |
|
90 | 87 | }
|
| 88 | + |
| 89 | + // to check required parameters passed or not |
| 90 | + private checkParameters(flags: any, args: any) { |
| 91 | + if (!flags.key) |
| 92 | + Logger.error(this,'Key is not passed') |
| 93 | + |
| 94 | + if(args.string == undefined || args.string =="" ) |
| 95 | + Logger.error(this, 'Input string is empty or undefined') |
| 96 | + |
| 97 | + if (flags.encryption && flags.decryption) |
| 98 | + Logger.error(this,'Both encryption and decryption methods passed') |
| 99 | + |
| 100 | + if(!(flags.encryption || flags.decryption)) |
| 101 | + Logger.error(this,'Neither encryption or decryption methods passed') |
| 102 | + } |
| 103 | + |
| 104 | + private getCryptoMode(thisRef: any, flags: any) { |
| 105 | + if(!flags.mode) //set default |
| 106 | + flags.mode='CBC' // it will not set to flags.mode there in run() but we do not require it |
| 107 | + Logger.info(this,'Block Mode: '+flags.mode) |
| 108 | + switch (flags.mode.toUpperCase()) { |
| 109 | + case 'CBC': |
| 110 | + return CryptoJS.mode.CBC |
| 111 | + case 'CFB': |
| 112 | + return CryptoJS.mode.CFB |
| 113 | + case 'OFB': |
| 114 | + return CryptoJS.mode.OFB |
| 115 | + case 'ECB': |
| 116 | + return CryptoJS.mode.ECB |
| 117 | + default: |
| 118 | + Logger.error(this,'Invalid or Unsupported Block Mode') |
| 119 | + return undefined // will never reach here |
| 120 | + } |
| 121 | + } |
| 122 | + |
91 | 123 | }
|
0 commit comments