The previous SF questions I've seen have lead to answers that produce MD5 hashed password.
Does anyone have a suggestion on to produce an SHA-512 hashed password? I'd prefer a one liner instead of a script but, if a script is the only solution, that's fine as well.
Update
Replacing previous py2 versions with this one:
python3 -c "import crypt;print(crypt.crypt(input('clear-text pw: '), crypt.mksalt(crypt.METHOD_SHA512)))"
Edit: Please note this answer is 10+ years old.
Here's a one liner:
Python 3.3+ includes
mksalt
in crypt, which makes it much easier (and more secure) to use:If you don't provide an argument to
crypt.mksalt
(it could acceptcrypt.METHOD_CRYPT
,...MD5
,SHA256
, andSHA512
), it will use the strongest available.The ID of the hash (number after the first
$
) is related to the method used:I'd recommend you look up what salts are and such and as per smallclamgers comment the difference between encryption and hashing.
Update 1: The string produced is suitable for shadow and kickstart scripts.
Update 2: Warning. If you are using a Mac, see the comment about using this in python on a mac where it doesn't seem to work as expected.
On macOS you should not use the versions above, because Python uses the system's version of
crypt()
which does not behave the same and uses insecure DES encryption. You can use this platform independent one liner (requires passlib – install withpip3 install passlib
):On Debian you can use mkpasswd to create passwords with different hashing algorithms suitable for /etc/shadow. It is included in the package whois (according to apt-file)
to get a list of available hashing algoritms type:
HTH
Using
grub-crypt
Here's a short C code to generate the SHA-512 password on various Unix type OSes.
File:
passwd-sha512.c
to compile:
usage:
Surprising that no answer suggests the simple
openssl passwd
command with the-6
option. Maybe it wasn't available yet in 2011?If you don't care providing the password on the command-line (risking it staying in the command history), then you can do:
It will generate the salt, and output a line like this:
With the
stdin
option, it can also read the password from STDIN (or a file), so you don't leave it behind in the history:Perl one-liner solution to generate SHA-512 hashed password:
perl -le 'print crypt "desiredPassword", "\$6\$customSalt\$"'
Worked on RHEL 6
Why not perform the following check and modification to Centos/RHEL machines to ensure that all password hashing for /etc/shadow is done with sha512. Then you can just set your passworkd normally with the passwd command
Here is a one-liner that uses shell commands to create a SHA-512 hashed password with a random salt:
Notes
Read the comment below to learn about security implications of this answer
For those of the Ruby mindset here is a one-liner:
All examples will be using SHA-512,
<password>
as password placeholder and<salt>
as salt placeholder.mkpasswd
Note:
mkpasswd
binary is installed via the packagewhois
on Debian / Ubuntu only. On other Linux distribution such as ArchLinux, Fedora, CentOS, openSUSE, etc.mkpasswd
is provided by theexpect
package but is an totally different utility which is available asexpect_mkpasswd
on Debian / Ubuntu.whois
of all other Linux distro doesn't includemkpasswd
but the source (C lang) can be found on the original repository https://github.com/rfc1036/whois.OpenSSL
Ruby
Note: for those who complains that
Random#rand
is a PRNG, you can use the secureSecureRandom#rand
but it's not very important is rand is used only to generate the salt which is publicly available in the hash at the end.Perl
Python
Requires Python >= 3.3
grub-crypt
Note: The
grub
package doesn't includegrub-crypt
in many distros.