Skip to content

Commit 4ee7908

Browse files
committed
[HASHES]: added
1 parent 4f5fb91 commit 4ee7908

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@oclif/command": "^1.5.19",
1212
"@oclif/config": "^1.13.3",
1313
"@oclif/plugin-help": "^2.2.1",
14+
"jshashes": "^1.0.7",
1415
"tslib": "^1.10.0"
1516
},
1617
"devDependencies": {

src/commands/hash.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {Command, flags} from '@oclif/command'
2+
// @ts-ignore
3+
import * as Hashes from 'jshashes'
4+
5+
// TODO: all are Hexadecimal encoding for now, can also add b64
6+
7+
export default class Hash extends Command {
8+
static description = 'create hash for a string/file'
9+
10+
static flags = {
11+
help: flags.help({char: 'h'}),
12+
// -t , --type for hashing
13+
type: flags.string({char: 't' , description: 'type of hash, default [SHA1]'}),
14+
string: flags.string({char: 's' , description: 'string to be hashed'}),
15+
file: flags.string({char: 'f' , description: 'file to be hashed'}),
16+
}
17+
18+
static args = [{name: 'string'}]
19+
20+
async run() {
21+
const {args, flags} = this.parse(Hash)
22+
23+
const type = flags.type || 'sha1' //by default let it be sha1
24+
25+
this.log('arg:' + args.string)
26+
27+
let hash: Hashes
28+
switch (type.toUpperCase()) {
29+
case 'SHA1':
30+
hash = new Hashes.SHA1(); break
31+
case 'SHA256':
32+
hash = new Hashes.SHA256(); break
33+
case 'SHA512':
34+
hash = new Hashes.SHA512(); break
35+
case 'MD5':
36+
hash = new Hashes.MD5(); break
37+
case 'RMD160':
38+
hash = new Hashes.RMD160(); break
39+
default:
40+
hash = undefined
41+
}
42+
43+
let hashed: string = hash.hex(args.string)
44+
this.log(`[HASH]: ${hashed}`)
45+
}
46+
}

test/commands/hash.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {expect, test} from '@oclif/test'
2+
3+
describe('hash', () => {
4+
test
5+
.stdout()
6+
.command(['hash'])
7+
.it('runs hello', ctx => {
8+
expect(ctx.stdout).to.contain('hello world')
9+
})
10+
11+
test
12+
.stdout()
13+
.command(['hash', '--name', 'jeff'])
14+
.it('runs hello --name jeff', ctx => {
15+
expect(ctx.stdout).to.contain('hello jeff')
16+
})
17+
})

0 commit comments

Comments
 (0)