Tip: Generate crypt encrypted passwords under Linux

  

When we use Red Hat Kickstart scripts or useradd or other means of writing, we often need to use the crypt command to encrypt the generated password format. So, is there any other way to generate passwords in this format? In fact, there are many ways.

We can use the mkpasswd command: This command is used to generate passwords in crypt format:


mkpasswd

After entering the command, the program will ask for a password and then generate A string in crypt format.

If you use Apache web server, you can also use htpasswd:


htpasswd -nd user

User name (user) does not matter, we are concerned about passwords. This command will output a string in the form of user:password, and copy the password field directly to ok.

If you have OpenSSL, you can use the openssl command:


openssl passwd -crypt myPassword

Replace the myPassword in the above command with the password string you want to use.

There are other ways to enter commands directly on the command line, but there is a problem with this method, that is, the password can be seen in the ps command, and the password will be recorded in the shell history.

But there is a solution to this problem: use a script, or a language interpreter.

For example, use Perl:


perl -e "print crypt('password','sa');"

Perl needs an encrypted salt, as used here Sa (salt refers to the random string used in encryption, with different salts can generate different encryption results).

Ruby also needs to encrypt salt:


ruby ​​-e 'print "password".crypt("JU"); print("\ ");'

PHP can also be:


php -r "print(crypt('password','JU') . \\"\ \\");"

Note that if you do not use an encryption salt (such as JU in the above command), the output string will not be in crypt encryption format, but in MD5 encryption format. Therefore, the encryption salt is actually a necessary parameter.

Python needs to import the crypt library and use the encrypted salt:


python -c 'import crypt; print crypt.crypt("password","Fx")'

The encrypted salt here is Fx.

The database can also generate crypt passwords. For example, use MySQL:


echo "select encrypt('password');" |  Mysql

In addition, Tcl, trf under Ubuntu, and Lua's lua-crypt plugin can achieve the same purpose.

Welcome to add other ways!

Copyright © Windows knowledge All Rights Reserved