-
Notifications
You must be signed in to change notification settings - Fork 0
/
hkdf-extractor
executable file
·54 lines (44 loc) · 1.35 KB
/
hkdf-extractor
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
#!/usr/bin/env python
'''
Requirements
------------
* Install hkdf globally.
sudo pip install hkdf
* Copy file `hkdf-extractor` to /usr/local/bin
* Make `hkdf-extractor` executable
sudo chmod +x /usr/local/bin/hkdf-extractor
Help
----
hkdf-extractor --help
Testme
------
echo -n hFtWoAyknKOorIBXUjgOptpVwCfmOFUN | ./hkdf-extractor --hex
'''
import sys
import argparse
import hkdf
import hashlib
parser = argparse.ArgumentParser(description='HKDF extractor')
parser.add_argument("--salt", default=None, type=str,
help="The salt (optional)")
parser.add_argument("--hash", default='sha512',
help="Hash algorithm (sha1/sha256/sha512; default: sha512)")
parser.add_argument("--hex", action='store_true',
help="output hexadecimal (default: raw)")
parser.add_argument("-n", action='store_true',
help="do not output the trailing newline in hexmode")
args = parser.parse_args()
hash_algos = { 'sha1': hashlib.sha1,
'sha256': hashlib.sha256,
'sha512': hashlib.sha512 }
infile = sys.stdin
ikm = bytearray("")
for line in infile:
ikm += bytearray(line)
prk = hkdf.hkdf_extract(args.salt, ikm, hash_algos[args.hash])
if args.hex:
sys.stdout.write(prk.encode('hex'))
if not args.n:
sys.stdout.write("\n")
else:
sys.stdout.write(prk)