是否有标准的方法为crypt系统调用生成一个salt?

时间:2017-08-03 08:30:39

标签: c linux security salt crypt

我需要为我的应用程序实现帐户管理,我宁愿不使用chpasswd子进程,否则请将明文密码从我的应用程序的内存空间中删除。

我想将putspent与我使用crypt生成的密码哈希一起使用,但我无法找到任何标准函数来随机化crypt的盐。在线搜索只发现奇怪的散列函数实现,而不是复制到我的代码中。是否有标准功能可以为我生成盐?

否则,重新使用存储在我的影子文件中的当前盐是否明智?我无法想到为什么它会成为安全隐患(它不会削弱我的影子文件以防止彩虹表攻击),它只是感觉不对,因为在系统安全性中,经验法则是随机化所有内容...... (用户添加了系统实用程序)

2 个答案:

答案 0 :(得分:0)

OpenSSL为sha512提供了函数:

https://www.openssl.org/docs/man1.0.2/crypto/SHA512.html

int SHA256_Init(SHA256_CTX *c);
int SHA256_Update(SHA256_CTX *c, const void *data, size_t len);
int SHA256_Final(unsigned char *md, SHA256_CTX *c);
unsigned char *SHA256(const unsigned char *d, size_t n,
      unsigned char *md);

答案 1 :(得分:-1)

为crypt()生成salt,需要2个随机字节。 为此,您可以使用openssl:

#include <openssl/rand.h>
int RAND_bytes(unsigned char *buf, int num);
int RAND_pseudo_bytes(unsigned char *buf, int num);

参考:https://www.openssl.org/docs/man1.0.2/crypto/RAND_bytes.html

示例:

unsigned char salt[2];
RAND_pseudo_bytes(salt, 2);
char *ptr = crypt(password, salt);