-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmkpasswd.py
More file actions
executable file
·50 lines (41 loc) · 1.33 KB
/
mkpasswd.py
File metadata and controls
executable file
·50 lines (41 loc) · 1.33 KB
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
#!/usr/bin/env python
# file : mkpasswd.py
# purpose : creating sha512 hashed passwords (like /etc/shadow)
#
# author : harald van der laan
# date : 2017/03/02
# version : v0.0.1
#
# changelog:
# - v0.0.1 initial version (harald)
'''
mkpasswd.py - python script for creating hashed password (sha512).
these passwords can be used in /etc/shadow and in the user module of
Ansible.
'''
# default imports
from __future__ import print_function
import sys
import string
import random
import getpass
# extra imports (use pip to install)
from passlib.hash import sha512_crypt
# define function
def gen_random_salt(charset):
''' function for creating a random salt '''
return ''.join(random.sample(charset*8, 8))
def gen_sha512_hash(password, salt):
''' function for creating sha512 password hash '''
return sha512_crypt.encrypt(password, salt=salt, rounds=5000)
def main():
''' main function of this script '''
charset = string.ascii_lowercase + string.ascii_uppercase + string.digits
salt = gen_random_salt(charset)
password = getpass.getpass('[*] please enter a password: ')
passwordhash = gen_sha512_hash(password, salt)
print('hash: {}' .format(passwordhash))
# run the main function
if __name__ == "__main__":
main()
sys.exit(0)