Created
May 7, 2016 07:58
-
-
Save cecil/3d2f9f0c8f16cbd0ebc8374b7fd8e388 to your computer and use it in GitHub Desktop.
Python script to generate a hash for /etc/shadow (SHA512) and LDAP (SSHA1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # | |
| # Python script to generate a hash for /etc/shadow (SHA512) and LDAP (SSHA1) | |
| # In case you needed to support a hypothetical environment that had both auth mechanisms in play. | |
| # | |
| import hashlib | |
| from base64 import urlsafe_b64encode as encode | |
| from base64 import urlsafe_b64decode as decode | |
| from getpass import getpass | |
| import crypt | |
| import os | |
| def hashssha1(password): | |
| salt = os.urandom(4) | |
| h = hashlib.sha1(password) | |
| h.update(salt) | |
| return "{SSHA}" + encode(h.digest() + salt) | |
| def hash512(password): | |
| h = crypt.crypt(password, "$6$16_CHARACTER_SALT_HERE") | |
| return(h) | |
| if __name__ == '__main__': | |
| passin = getpass('Please enter clear-text password: ') | |
| # print("cats " + passin) | |
| print("SSHA1 :: " + hashssha1(passin)) | |
| print("SHA512 :: " + hash512(passin)) | |
@Torxed: This does not really create a hash suitable for /etc/shadow - it's marked as sha512crypt, which is not what pbkdf2_hmac computes. This will not work in practice.
From man crypt(5):
sha512crypt
A hash based on SHA-2 with 512-bit output, originally developed by Ulrich Drepper for GNU libc. Supported on Linux but not common
elsewhere. Acceptable for new hashes. The default CPU time cost parameter is 5000, which is too low for modern hardware.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For what it's worth, if someone reacted to https://serverfault.com/a/412441/150015 and ends up here looking for any modern solution/code.
This was introduced in Python3.4 2014 as a built-in:
But also this: