Skip to content

Commit cc15b03

Browse files
committed
[HASHES]: hashing added with test
1 parent 4ee7908 commit cc15b03

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

src/commands/hash.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default class Hash extends Command {
1010
static flags = {
1111
help: flags.help({char: 'h'}),
1212
// -t , --type for hashing
13-
type: flags.string({char: 't' , description: 'type of hash, default [SHA1]'}),
13+
type: flags.string({char: 't' , description: 'type of hash [SHA1(default),MD5,SHA256,SHA512,RMD160]'}),
1414
string: flags.string({char: 's' , description: 'string to be hashed'}),
1515
file: flags.string({char: 'f' , description: 'file to be hashed'}),
1616
}
@@ -20,9 +20,16 @@ export default class Hash extends Command {
2020
async run() {
2121
const {args, flags} = this.parse(Hash)
2222

23-
const type = flags.type || 'sha1' //by default let it be sha1
23+
const type: string = flags.type || 'sha1' //by default let it be sha1
2424

25-
this.log('arg:' + args.string)
25+
// if -s is not passed we will take it from args
26+
27+
let str: string
28+
29+
if (flags.string)
30+
str = flags.string
31+
else
32+
str = args.string
2633

2734
let hash: Hashes
2835
switch (type.toUpperCase()) {
@@ -40,7 +47,7 @@ export default class Hash extends Command {
4047
hash = undefined
4148
}
4249

43-
let hashed: string = hash.hex(args.string)
50+
let hashed: string = hash.hex(str)
4451
this.log(`[HASH]: ${hashed}`)
4552
}
4653
}

test/commands/hash.test.ts

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,64 @@ import {expect, test} from '@oclif/test'
33
describe('hash', () => {
44
test
55
.stdout()
6-
.command(['hash'])
7-
.it('runs hello', ctx => {
8-
expect(ctx.stdout).to.contain('hello world')
6+
.command(['hash', 'ashish'])
7+
.it("cdt hash 'ashish'", ctx => {
8+
expect(ctx.stdout).to.contain('428b6da53085b8fd7b37e9fb259c0c609bd09984')
99
})
1010

11+
// passing sha1 as option
1112
test
1213
.stdout()
13-
.command(['hash', '--name', 'jeff'])
14-
.it('runs hello --name jeff', ctx => {
15-
expect(ctx.stdout).to.contain('hello jeff')
14+
.command(['hash', 'ashish' ,'-t','sha1'])
15+
.it("cdt hash 'ashish' -t 'sha1'", ctx => {
16+
expect(ctx.stdout).to.contain('428b6da53085b8fd7b37e9fb259c0c609bd09984')
17+
})
18+
19+
//overriding string with option -s
20+
test
21+
.stdout()
22+
.command(['hash', '-s', 'ashish'])
23+
.it("cdt hash -s 'ashish'", ctx => {
24+
expect(ctx.stdout).to.contain('428b6da53085b8fd7b37e9fb259c0c609bd09984')
25+
})
26+
27+
// if both passed then need to take flag value of -s
28+
test
29+
.stdout()
30+
.command(['hash', 'patel', '-s', 'ashish'])
31+
.it("cdt hash 'patel' -s 'ashish'", ctx => {
32+
expect(ctx.stdout).to.contain('428b6da53085b8fd7b37e9fb259c0c609bd09984')
33+
})
34+
35+
//sha256
36+
test
37+
.stdout()
38+
.command(['hash', '-t', 'sha256', 'ashish'])
39+
.it("cdt hash -t sha256 'ashish'", ctx => {
40+
expect(ctx.stdout).to.contain('05d08de271d2773a504b3a30f98df26cccda55689a8dc3514f55d3f247553d2b')
41+
})
42+
43+
//md5
44+
test
45+
.stdout()
46+
.command(['hash', '--type', 'md5', 'ashish'])
47+
.it("cdt hash --type md5 'ashish'", ctx => {
48+
expect(ctx.stdout).to.contain('7b69ad8a8999d4ca7c42b8a729fb0ffd')
49+
})
50+
51+
//sha512
52+
test
53+
.stdout()
54+
.command(['hash', '--type', 'sha512', 'ashish'])
55+
.it("cdt hash --type sha512 'ashish'", ctx => {
56+
expect(ctx.stdout).to.contain('2e076fcbd0e5dcb29d30da42a554de919864bbc29eaefe6d32c4f6bcb219d77ce5e48dada485dae92c918b10a03f42008c43ca721f06da6efa5fa9a223401907')
57+
})
58+
59+
//rmd160
60+
test
61+
.stdout()
62+
.command(['hash', '--type', 'rmd160', 'ashish'])
63+
.it("cdt hash --type rmd160 'ashish'", ctx => {
64+
expect(ctx.stdout).to.contain('a85a72b0a240abecdf27f127aa75fd8663d6d5be')
1665
})
1766
})

0 commit comments

Comments
 (0)