我会建议使用 Openssl API
1.) 如果要使用密码生成密钥,请使用PKCS5_PBKDF2_HMAC_SHA1()类似
# include <openssl/evp.h>
int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
unsigned char *salt, int saltlen, int iter,
int keylen, unsigned char *out);
Run Code Online (Sandbox Code Playgroud)
或者
int EVP_BytesToKey(const EVP_CIPHER *type,const EVP_MD *md,
const unsigned char *salt,
const unsigned char *data, int datal, int count,
unsigned char *key,unsigned char *iv);
Run Code Online (Sandbox Code Playgroud)
2.) 散列密码 使用 sha256 而不是 sha1(原因更安全,并且已经在 sha1 安全性上进行了尝试)
# include <openssl/sha.h>
SHA256_CTX context;
unsigned char md[SHA256_DIGEST_LENGTH];
SHA256_Init(&context);
SHA256_Update(&context, (unsigned char*)input, length);
SHA256_Final(md, &context);
Run Code Online (Sandbox Code Playgroud)
所以在这个 md 之后将包含密码哈希
3.) SALT:salt 只不过是一些随机字节,但重点是采用加密安全的随机字节。为此,您可以使用:
# include <openssl/rand.h>
unsigned char salt[32]; //32 is just an example
int RAND_bytes(salt,32);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5455 次 |
| 最近记录: |