如何为阴影创build一个SHA-512哈希密码?

我见过的前面的SF问题导致产生MD5哈希密码的答案。

有没有人有build议产生一个SHA-512哈希密码? 我更喜欢一个class轮,而不是一个脚本,但是,如果一个脚本是唯一的解决scheme,那也不错。

更新

根据戴维的回答,我创造了一个比戴维长一点的单线,但其他一些人可能会觉得有用:

python -c "import crypt,random,string; print crypt.crypt(raw_input('clear-text password: '), '\$6\$' + ''.join([random.choice(string.ascii_letters + string.digits) for _ in range(16)]))" 

还有更优雅的一个,但需要缺省安装的passlib模块:

 python -c "from passlib.hash import sha512_crypt; print sha512_crypt.encrypt(raw_input('clear-text password: '))" 

    这里是一个单行的:

     python -c 'import crypt; print crypt.crypt("test", "$6$random_salt")' 

    Python 3.3+ 在crypt中包含了mksalt ,这使得它更容易(也更安全)使用:

     python3 -c 'import crypt; print(crypt.crypt("test", crypt.mksalt(crypt.METHOD_SHA512)))' 

    如果你没有提供参数crypt.mksalt (它可以接受crypt.METHOD_CRYPT...MD5SHA256SHA512 ),它将使用最强大的可用。

    散列的ID(第一个$之后的数字)与使用的方法有关:

    • 1 – > MD5
    • 2a – > Blowfish(不在mainline glibc中;在某些Linux发行版中添加)
    • 5 – > SHA-256(因为glibc 2.7)
    • 6 – > SHA-512(因为glibc 2.7)

    我build议你看看盐是什么,以及每个小信号评论encryption和散列之间的区别。

    更新1:生成的string适用于阴影和kickstart脚本。 更新2:警告。 如果您使用的是Mac,请参阅有关在Mac上使用python的注释,但看起来不像预期的那样工作。

    在Debian上,您可以使用mkpasswd创build适合/ etc / shadow的不同散列algorithm的密码。 它被包含在包中(根据apt-file)

     mkpasswd -m sha-512 mkpasswd -m md5 

    获取可用的哈希algorithmtypes列表:

     mkpasswd -m help 

    HTH

    最佳答案:grub-crypt

     Usage: grub-crypt [OPTION]... Encrypt a password. -h, --helpPrint this message and exit -v, --version Print the version information and exit --md5 Use MD5 to encrypt the password --sha-256 Use SHA-256 to encrypt the password **--sha-512 Use SHA-512 to encrypt the password (default)** 

    下面是一个简短的C代码,用于在各种Unixtypes的操作系统上生成SHA-512密码。

    文件: passwd-sha512.c

     #define _XOPEN_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) { if ( argc < 3 || (int) strlen(argv[2]) > 16 ) { printf("usage: %s password salt\n", argv[0]); printf("--salt must not larger than 16 characters\n"); return; } char salt[21]; sprintf(salt, "$6$%s$", argv[2]); printf("%s\n", crypt((char*) argv[1], (char*) salt)); return; } 

    编译:

     /usr/bin/gcc -lcrypt -o passwd-sha512 passwd-sha512.c 

    用法:

     passwd-sha512 <password> <salt (16 chars max)> 

    Perl单线程解决scheme生成SHA-512散列密码:

    perl -le 'print crypt "desiredPassword", "\$6\$customSalt\$"'

    在RHEL 6上工作

    为什么不对CentOS / RHEL机器执行以下检查和修改,以确保/ etc / shadow的所有密码哈希都是使用sha512完成的。 然后,您可以使用passwd命令正常设置您的passworkd

     #Set stronger password hasing /usr/sbin/authconfig --test | grep sha512 > /dev/null if [ $? -ne 0 ]; then echo "Configuring sha512 password hashing" sudo /usr/sbin/authconfig --enableshadow --passalgo=sha512 --updateall fi 

    下面是一个使用shell命令创buildSHA-512散列密码和随机盐的单线程:

    [root @ host] mkpasswd -m sha-512 MyPAsSwOrD $(openssl rand -base64 16 | tr -d'+ ='| head -c 16)

    笔记

    1. 您可能需要安装提供“mkpasswd”的“whois”软件包(Debian,SuSE等)。
    2. 有关“/ etc / shadow”中行的格式的详细信息,请参阅crypt(3)。

    它不是一个class轮,但它可能有助于某人:

     import crypt, getpass, pwd, string, sys, random randomsalt = "" password = getpass.getpass() choices = string.ascii_uppercase + string.digits + string.ascii_lowercase for _ in range(0,8): randomsalt += random.choice(choices) print crypt.crypt(password, '$6$%s$' % randomsalt) 

    对于那些Ruby思维的人来说,这是一个单行的:

     'password'.crypt('$6$' + rand(36 ** 8).to_s(36)) 

    HASHalgorithm用于生成MESSAGE摘要,它们永远不适合使用某种HKDF( http://tools.ietf.org/rfc/rfc5869.txt )的密码 – 参见PBKDF2或BCrypt

     #!/usr/bin/env python import getpass from passlib.hash import sha512_crypt if __name__ == "__main__": passwd = getpass.getpass('Password to hash: ') hash = sha512_crypt.encrypt(passwd) print hash 

    你可以克隆它从我的github回购如果你想要的: https : //github.com/antoncohen/mksha

     $ htpasswd -c /tmp/my_hash user1 New password: Re-type new password: Adding password for user user1 $ cat /tmp/my_hash user1:$apr1$oj1ypcQz$4.6lFVtKz2nr8acsQ8hD30 

    很明显,你只需要抓住第二个字段,一旦你把它添加到shadow或sudo(仍然是最有可能的影子),就可以删除这个文件。

    这个脚本在Ubuntu 12.04 LTS上为我工作: https : //gist.github.com/JensRantil/ac691a4854a4f6cb4bd9

     #!/bin/bash read -p "Enter username: " username read -s -p "Enter password: " mypassword echo echo -n $username:$mypassword | chpasswd -S -c SHA512 

    它具有以下一些其他select缺乏的特点:

    • 它安全地生成它的盐。 没有人应该依靠手动来做这​​件事。 永远。
    • 它不会在shell历史中存储任何东西
    • 为了清楚起见,它会打印出生成的用户密码,这在生成许多用户的密码时可以很好。

    看看crypt的手册页(3),我想你会发现crypt工具已经更新为使用glibc和sha256($ 5)和sha512($ 6),多轮,更大的盐等等。

    显然,SHA512与/ etc / shadow的工作方式有关。

    这就是说,这个网页是非常有用的 – 特别是MKPASSWD,因为这解决了我的问题。

    给定一个潜在的“丢失”密码,我可以使用MKPASSWD和salt来生成SHA512哈希,并确认/拒绝候选密码列表。

    我会用开膛手约翰 – 但至less在我的硬件(树莓派)和我的预算(没有) – 约翰不能这样做(它似乎不支持在raspbian免费版本先进的地穴/ glibc的东西。

    请注意,由于我有足够的权限来读/写/ etc / shadow,所以我可以重写hash,然后继续生活…这是一个学术练习。


    注意Glibc注释该函数的glibc2版本支持附加的encryptionalgorithm。

      If salt is a character string starting with the characters "$id$" followed by a string terminated by "$": $id$salt$encrypted then instead of using the DES machine, id identifies the encryp‐ tion method used and this then determines how the rest of the password string is interpreted. The following values of id are supported: ID | Method ───────────────────────────────────────────────────────── 1 | MD5 2a | Blowfish (not in mainline glibc; added in some | Linux distributions) 5 | SHA-256 (since glibc 2.7) 6 | SHA-512 (since glibc 2.7) So $5$salt$encrypted is an SHA-256 encoded password and $6$salt$encrypted is an SHA-512 encoded one. 

    我不确定SHA-512如何与/etc/shadow关联。 这些密码是crypt

    但是,如果你想用SHA-512密码散列,你可以通过echo -n the_password | sha512sum echo -n the_password | sha512sum 。 你不能使用/ etc / shadow的输出。