Skip to content

Commit 1af9596

Browse files
authored
Merge pull request #15 from codingtools/feature/add-ciphers
Feature ciphers cryptography functionality
2 parents c15ec6b + 7fa1ea5 commit 1af9596

File tree

9 files changed

+540
-71
lines changed

9 files changed

+540
-71
lines changed

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,80 @@ Command Line tools for CODERs
99
[![License](https://img.shields.io/npm/l/cdt.svg)](https://github.com//cdt/blob/master/package.json) [![Greenkeeper badge](https://badges.greenkeeper.io/codingtools/cdt.svg)](https://greenkeeper.io/)
1010

1111
<!-- toc -->
12+
* [Usage](#usage)
13+
* [Commands](#commands)
14+
<!-- tocstop -->
1215
# Usage
1316
<!-- usage -->
17+
```sh-session
18+
$ npm install -g cdt
19+
$ cdt COMMAND
20+
running command...
21+
$ cdt (-v|--version|version)
22+
cdt/0.0.1 darwin-x64 node-v12.9.0
23+
$ cdt --help [COMMAND]
24+
USAGE
25+
$ cdt COMMAND
26+
...
27+
```
28+
<!-- usagestop -->
1429
# Commands
1530
<!-- commands -->
31+
* [`cdt crypto [STRING]`](#cdt-crypto-string)
32+
* [`cdt hash [STRING]`](#cdt-hash-string)
33+
* [`cdt help [COMMAND]`](#cdt-help-command)
34+
35+
## `cdt crypto [STRING]`
36+
37+
Encryption and Decryption functionality
38+
39+
```
40+
USAGE
41+
$ cdt crypto [STRING]
42+
43+
OPTIONS
44+
-d, --decryption=decryption decryption type, Supported [AES, DES, 3DES, Rabbit, RC4, RC4Drop]
45+
-e, --encryption=encryption encryption type, Supported [AES, DES, 3DES, Rabbit, RC4, RC4Drop]
46+
-f, --file=file file to be encrypted/decrypted
47+
-h, --help show CLI help
48+
-k, --key=key key for encryption/decryption
49+
-m, --mode=mode Block Mode, Supported [CBC, CFB, CTR, OFB, ECB]
50+
-s, --string=string string to be encrypted/decrypted
51+
```
52+
53+
_See code: [src/commands/crypto.ts](https://github.com/codingtools/cdt/blob/v0.0.1/src/commands/crypto.ts)_
54+
55+
## `cdt hash [STRING]`
56+
57+
create hash for a string/file
58+
59+
```
60+
USAGE
61+
$ cdt hash [STRING]
62+
63+
OPTIONS
64+
-f, --file=file file to be hashed
65+
-h, --help show CLI help
66+
-s, --string=string string to be hashed
67+
-t, --type=type type of hash [SHA1(default),MD5,SHA256,SHA512,RMD160]
68+
```
69+
70+
_See code: [src/commands/hash.ts](https://github.com/codingtools/cdt/blob/v0.0.1/src/commands/hash.ts)_
71+
72+
## `cdt help [COMMAND]`
73+
74+
display help for cdt
75+
76+
```
77+
USAGE
78+
$ cdt help [COMMAND]
79+
80+
ARGUMENTS
81+
COMMAND command to show help for
82+
83+
OPTIONS
84+
--all see all commands in CLI
85+
```
86+
87+
_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v2.2.1/src/commands/help.ts)_
88+
<!-- commandsstop -->

package-lock.json

Lines changed: 112 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
"@oclif/command": "^1.5.19",
1212
"@oclif/config": "^1.13.3",
1313
"@oclif/plugin-help": "^2.2.1",
14+
"@types/crypto-js": "^3.1.43",
15+
"@types/signale": "^1.2.1",
16+
"crypto-js": "^3.1.9-1",
1417
"jshashes": "^1.0.7",
15-
"tslib": "^1.10.0",
16-
"nyc": "^14.1.1"
18+
"nyc": "^14.1.1",
19+
"signale": "^1.4.0",
20+
"tslib": "^1.10.0"
1721
},
1822
"devDependencies": {
1923
"@oclif/dev-cli": "^1.22.2",
@@ -38,8 +42,9 @@
3842
"/npm-shrinkwrap.json",
3943
"/oclif.manifest.json"
4044
],
41-
"homepage": "https://github.com//cdt",
45+
"homepage": "https://github.com/codingtools/cdt",
4246
"keywords": [
47+
"cdt",
4348
"oclif"
4449
],
4550
"license": "MIT",
@@ -51,7 +56,7 @@
5156
"@oclif/plugin-help"
5257
]
5358
},
54-
"repository": "/cdt",
59+
"repository": "codingtools/cdt",
5560
"scripts": {
5661
"postpack": "rm -f oclif.manifest.json",
5762
"posttest": "tslint -p test -t stylish",

src/commands/crypto.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)