-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hash.ts
93 lines (78 loc) · 2.82 KB
/
hash.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {Command, flags} from '@oclif/command'
import CryptoJS from 'crypto-js'
import Logger from '../utilities/logger'
import Utilities from '../utilities/utilities'
// import Hashes from 'jshashes'
// TODO: all are Hexadecimal encoding for now, can also add b64
export default class Hash extends Command {
static description = 'Hashing functionality for a string/file'
static flags = {
help: flags.help({char: 'h'}),
// -t , --type for hashing
type: flags.string({char: 't' , description: 'type of hash [SHA1(default), MD5, SHA256, SHA512, RMD160 or RIPEMD160]'}),
string: flags.string({char: 's' , description: 'string to be hashed'}),
file: flags.string({char: 'f' , description: 'file to be hashed'}),
output: flags.string({char: 'o' , description: 'output file path'}),
}
static args = [{name: 'string'}]
// only 2 parameters required HASH_TYPE and INPUT_STRING
async run() {
const {args, flags} = this.parse(Hash)
flags.type = this.getHashType(flags) //by default let it be sha1
args.string = Utilities.getInputString(this, flags, args) // from either -s,-f or args
//check params after evaluating all
this.checkParameters(flags, args)
this.calculateHash(flags, args)
}
// to check required parameters passed or not
// tslint:disable-next-line:no-unused
private checkParameters(flags: any, args: any) {
if (args.string === undefined || args.string === '')
Logger.error(this, 'Input string is empty or undefined')
}
private calculateHash(flags: any, args: any) {
const hashObject = this.getHashObject(flags)
// @ts-ignore
let hashed: string = hashObject(args.string)
Logger.success(this, `[${flags.type.toUpperCase()}] ${hashed}`)
if (flags.output) { // if output path is provided then write to file also
Utilities.writeStringToFile(this, flags.output, hashed)
}
}
// BACKUP function
// private getHashObject2(flags: any) {
// switch (flags.type.toUpperCase()) {
// case 'SHA1':
// return new Hashes.SHA1().hex
// case 'SHA256':
// return new Hashes.SHA256().hex
// case 'SHA512':
// return new Hashes.SHA512().hex
// case 'MD5':
// return new Hashes.MD5().hex
// case 'RMD160':
// return new Hashes.RMD160().hex
// default:
// Logger.error(this, 'Invalid Or Unsupported hash type')
// }
// }
private getHashObject(flags: any) {
switch (flags.type.toUpperCase()) {
case 'SHA1':
return CryptoJS.SHA1
case 'SHA256':
return CryptoJS.SHA256
case 'SHA512':
return CryptoJS.SHA512
case 'MD5':
return CryptoJS.MD5
case 'RMD160': case 'RIPEMD160':
return CryptoJS.RIPEMD160
default:
Logger.error(this, 'Invalid Or Unsupported hash type')
}
}
private getHashType(flags: any) {
return flags.type || 'sha1'
}
}