Linux /etc/shadow 上使用的加密方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12660851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:28:18  来源:igfitidea点击:

Which is the encryption method used on /etc/shadow?

clinuxencryption

提问by b3h3m0th

Which is the encryption method used on /etc/shadow on GNU/Linux systems? I would like to write a small program for personal purpose that uses the same API, but at the moment I don't know where to start.

GNU/Linux 系统上 /etc/shadow 使用的加密方法是什么?我想为个人目的编写一个使用相同 API 的小程序,但目前我不知道从哪里开始。

Thanks in advance

提前致谢

采纳答案by ninjalj

Use the crypt(3)function. On glibc, the method used depends on the salt, if it starts with:

使用该crypt(3)功能。在 glibc 上,使用的方法取决于盐,如果它以以下开头:

  • $1$: it uses MD5.
  • $5$: it uses SHA-256.
  • $6$: it uses SHA-512.
  • $2a$: it uses blowfish, not supported everywhere.
  • Otherwise it uses DES.
  • $1$:它使用 MD5。
  • $5$:它使用 SHA-256。
  • $6$:它使用 SHA-512。
  • $2a$:它使用河豚,并非所有地方都支持。
  • 否则它使用DES。

回答by Petr Baudis

Multiple encryption methods are available in glibc, see man 3 crypt, the Glibc Notes section: http://manpages.courier-mta.org/htmlman3/crypt.3.html

glibc 中提供了多种加密方法,参见 man 3 crypt,Glibc 注释部分:http: //manpages.courier-mta.org/htmlman3/crypt.3.html

When verifying an existing password, just pass the encrypted form as salt; only the initial $id$salt part will be used. When creating new password, initialize id with whatever you need and put some random characters in salt.

验证现有密码时,只需将加密形式作为盐传递即可;只会使用最初的 $id$salt 部分。创建新密码时,使用您需要的任何内容初始化 id 并在 salt 中放入一些随机字符。

回答by b3h3m0th

basic example with crypt()

crypt() 的基本示例

#include <stdio.h>
#include <stdlib.h>

#define MAX_STR 256
#define MAX_SALT 12

int main(int argc, char *argv[]) {
    char password[MAX_STR];
    char salt[MAX_SALT];

    printf("salt: ");
    scanf("%s", salt);

    printf("password: ");
    scanf("%s", password);

    printf("Encrypt '%s' : '%s'\n", password, crypt(password, salt));

    return(EXIT_SUCCESS);
}

Compile program:

编译程序:

$ gcc -lcrypt test.c

回答by Georgi Dimov

I receive

我收到

undefined reference to `crypt'

对“crypt”的未定义引用

so I think that you should compile with

所以我认为你应该编译

$ gcc test.c -lcrypt

$ gcc test.c -lcrypt