|
| 1 | +import {Command, flags} from '@oclif/command' |
| 2 | +import * as CryptoJS from 'crypto-js' |
| 3 | + |
| 4 | +import Logger from '../utilities/logger' |
| 5 | + |
| 6 | +import Hash from './hash' |
| 7 | + |
| 8 | +export default class Crypto extends Command { |
| 9 | + static ENCRYPTION = 'encryption' |
| 10 | + static DECRYPTION = 'decryption' |
| 11 | + static description = 'Encryption and Decryption functionality' |
| 12 | + static flags = { |
| 13 | + help: flags.help({char: 'h'}), |
| 14 | + |
| 15 | + encryption: flags.string({char: 'e', description: 'encryption type, Supported [AES, DES, 3DES, Rabbit, RC4, RC4Drop]'}), |
| 16 | + decryption: flags.string({char: 'd', description: 'decryption type, Supported [AES, DES, 3DES, Rabbit, RC4, RC4Drop]'}), |
| 17 | + string: flags.string({char: 's' , description: 'string to be encrypted/decrypted'}), |
| 18 | + file: flags.string({char: 'f' , description: 'file to be encrypted/decrypted'}), |
| 19 | + key: flags.string({char: 'k' , description: 'key for encryption/decryption'}), |
| 20 | + mode: flags.string({char: 'm' , description: 'Block Mode, Supported [CBC, CFB, CTR, OFB, ECB]'}) |
| 21 | + } |
| 22 | + |
| 23 | + static args = [{name: 'string'}] |
| 24 | + //need INPUT_STRING, TYPE_OF_CRYPTO , KEY, MODE |
| 25 | + async run() { |
| 26 | + const {args, flags} = this.parse(Crypto) |
| 27 | + |
| 28 | + args.string = Hash.getInputString(this, flags, args) //always add input to args |
| 29 | + args.type = flags.encryption ? flags.encryption : flags.decryption //type like AES,DES |
| 30 | + |
| 31 | + this.checkParameters(flags, args) |
| 32 | + flags.encryption ? this.Encrypt(flags, args) : this.Decrypt(flags, args) |
| 33 | + } |
| 34 | + |
| 35 | + private Encrypt(flags: any, args: any) { |
| 36 | + let crypto = this.getCryptoType(args.type) |
| 37 | + Logger.info(this, `Encryption: ${flags.encryption.toUpperCase()}`) |
| 38 | + // @ts-ignore // as crypto will never be undefined and reach here |
| 39 | + let encrypted: string = crypto.encrypt(args.string, flags.key, { |
| 40 | + mode: this.getCryptoMode(flags) |
| 41 | + }).toString() |
| 42 | + Logger.success(this, `${encrypted}`) |
| 43 | + } |
| 44 | + |
| 45 | + private Decrypt(flags: any, args: any) { |
| 46 | + let crypto = this.getCryptoType(args.type) |
| 47 | + Logger.info(this, `Decryption: ${flags.decryption.toUpperCase()}`) |
| 48 | + // @ts-ignore // as crypto will never be undefined and reach here |
| 49 | + let decrypted: string = crypto.decrypt(args.string, flags.key, { |
| 50 | + mode: this.getCryptoMode(flags) |
| 51 | + }).toString(CryptoJS.enc.Utf8) |
| 52 | + Logger.success(this, `${decrypted}`) |
| 53 | + } |
| 54 | + |
| 55 | + private getCryptoType(type: string) { |
| 56 | + switch (type.toUpperCase()) { |
| 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') |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // to check required parameters passed or not |
| 75 | + private checkParameters(flags: any, args: any) { |
| 76 | + if (!flags.key) |
| 77 | + Logger.error(this, 'Key is not passed') |
| 78 | + |
| 79 | + if (args.string === undefined || args.string === '') |
| 80 | + Logger.error(this, 'Input string is empty or undefined') |
| 81 | + |
| 82 | + if (flags.encryption && flags.decryption) |
| 83 | + Logger.error(this, 'Both encryption and decryption methods passed') |
| 84 | + |
| 85 | + if (!(flags.encryption || flags.decryption)) |
| 86 | + Logger.error(this, 'Neither encryption or decryption methods passed') |
| 87 | + } |
| 88 | + |
| 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) |
| 93 | + switch (flags.mode.toUpperCase()) { |
| 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 | + } |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments