Skip to content

Commit df80f65

Browse files
committed
[CRYPTO]: TSLINT fixed
Signed-off-by: ashish <[email protected]>
1 parent 357e468 commit df80f65

File tree

5 files changed

+83
-78
lines changed

5 files changed

+83
-78
lines changed

oclif.manifest.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/commands/crypto.ts

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {Command, flags} from '@oclif/command'
22
import * as CryptoJS from 'crypto-js'
3+
34
import Logger from '../utilities/Logger'
5+
46
import Hash from './hash'
57

68
export 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

src/commands/hash.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {Command, flags} from '@oclif/command'
22
// @ts-ignore
33
import * as Hashes from 'jshashes'
44

5-
import Utilities from '../utilities/Utilities'
65
import Logger from '../utilities/Logger'
6+
import Utilities from '../utilities/Utilities'
77
// TODO: all are Hexadecimal encoding for now, can also add b64
88

99
export default class Hash extends Command {
@@ -19,61 +19,61 @@ export default class Hash extends Command {
1919

2020
static args = [{name: 'string'}]
2121

22+
static getInputString(thisRef: any , flags: any, args: any) { //need to make it static so Crypto can use this
23+
// if -s or -f is not passed we will take it from args
24+
if (flags.string) //if -s given
25+
return flags.string
26+
else if (flags.file) {
27+
Logger.info(thisRef, `reading file: ${flags.file}`)
28+
return Utilities.getStringFromFile(thisRef, flags.file)
29+
} else
30+
return args.string
31+
}
32+
2233
// only 2 parameters required HASH_TYPE and INPUT_STRING
2334
async run() {
2435
const {args, flags} = this.parse(Hash)
2536

2637
flags.type = this.getHashType(flags) //by default let it be sha1
27-
args.string = Hash.getInputString(this,flags,args) // from either -s,-f or args
38+
args.string = Hash.getInputString(this, flags, args) // from either -s,-f or args
2839

2940
//check params after evaluating all
3041
this.checkParameters(flags, args)
3142
this.calculateHash(flags, args)
3243
}
3344

3445
// to check required parameters passed or not
46+
// tslint:disable-next-line:no-unused
3547
private checkParameters(flags: any, args: any) {
36-
if(args.string == undefined || args.string =="" )
48+
if (args.string === undefined || args.string === '')
3749
Logger.error(this, 'Input string is empty or undefined')
3850

3951
}
4052

41-
private calculateHash(flags: any, args:any) {
53+
private calculateHash(flags: any, args: any) {
4254
const hashObject = this.getHashObject(flags)
4355
let hashed: string = hashObject.hex(args.string)
4456
Logger.success(this, `[${flags.type.toUpperCase()}] ${hashed}`)
4557
}
4658

47-
private getHashObject(flags: any){
59+
private getHashObject(flags: any) {
4860
switch (flags.type.toUpperCase()) {
49-
case 'SHA1':
50-
return new Hashes.SHA1()
51-
case 'SHA256':
52-
return new Hashes.SHA256()
53-
case 'SHA512':
54-
return new Hashes.SHA512()
55-
case 'MD5':
56-
return new Hashes.MD5()
57-
case 'RMD160':
58-
return new Hashes.RMD160()
59-
default:
60-
Logger.error(this, 'Invalid Or Unsupported hash type')
61-
return undefined // code never reach here
61+
case 'SHA1':
62+
return new Hashes.SHA1()
63+
case 'SHA256':
64+
return new Hashes.SHA256()
65+
case 'SHA512':
66+
return new Hashes.SHA512()
67+
case 'MD5':
68+
return new Hashes.MD5()
69+
case 'RMD160':
70+
return new Hashes.RMD160()
71+
default:
72+
Logger.error(this, 'Invalid Or Unsupported hash type')
6273
}
6374
}
6475

65-
private getHashType(flags: any) {
76+
private getHashType(flags: any) {
6677
return flags.type || 'sha1'
6778
}
68-
69-
static getInputString(thisRef: any ,flags: any, args:any) { //need to make it static so Crypto can use this
70-
// if -s or -f is not passed we will take it from args
71-
if (flags.string) //if -s given
72-
return flags.string
73-
else if (flags.file) {
74-
Logger.info(thisRef, `reading file: ${flags.file}`)
75-
return Utilities.getStringFromFile(thisRef, flags.file)
76-
} else
77-
return args.string
78-
}
7979
}

src/utilities/Logger.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
// tslint:disable-next-line:file-name-casing
12
import * as signale from 'signale'
23

4+
// tslint:disable-next-line:no-unnecessary-class
35
export default class Logger {
4-
56
// uses signale for logging withoug thisRef
7+
// tslint:disable-next-line:no-unused
68
public static success(thisRef: any, message: string) {
79
signale.success(`${message}`)
810
}
11+
// tslint:disable-next-line:no-unused
912
public static info(thisRef: any, message: string) {
1013
signale.info(`${message}`)
1114
}

src/utilities/Utilities.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
// tslint:disable-next-line:file-name-casing
12
import * as fs from 'fs'
3+
24
import Logger from './Logger'
35

6+
// tslint:disable-next-line:no-unnecessary-class
47
export default class Utilities {
58
public static getStringFromFile(thisRef: any, filePath: string) {
69
let fileStr = ''
710
if (!fs.existsSync(filePath)) {
8-
Logger.error(thisRef,`Could not find file: ${filePath}`) // this will output error and exit command
11+
Logger.error(thisRef, `Could not find file: ${filePath}`) // this will output error and exit command
912
} else {
1013
fileStr = fs.readFileSync(filePath, 'utf8')
1114

0 commit comments

Comments
 (0)