修改scrypt算法, 保证结果和scrypt-js一致

This commit is contained in:
cebgcontract 2022-09-22 21:35:52 +08:00
parent 0a782e88cf
commit 1b4f73b391

View File

@ -7,7 +7,8 @@
#include "native-crypto.h" #include "native-crypto.h"
extern "C" { extern "C"
{
#include "crypto_scrypt.h" #include "crypto_scrypt.h"
#include "base64.h" #include "base64.h"
} }
@ -18,40 +19,54 @@ extern "C" {
#include <random> #include <random>
#include <sstream> #include <sstream>
using namespace std;
int fast_crypto_scrypt(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, int fast_crypto_scrypt(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen,
uint64_t N, uint64_t N,
uint32_t r, uint32_t p, uint8_t *buf, size_t buflen) { uint32_t r, uint32_t p, uint8_t *buf, size_t buflen)
{
return crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen); return crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen);
} }
int crypto_scrypt_base64(std::string *password, std::string *salt, uint64_t N, vector<uint8_t> hexToBytes(const string &hex)
uint32_t r, uint32_t p, size_t size, std::string *value) { {
char *szPassword = const_cast<char *>(password->c_str()); vector<uint8_t> bytes;
size_t passwordBufSize = Base64decode_len(szPassword); for (unsigned int i = 0; i < hex.length(); i += 2)
char *szSalt = const_cast<char *>(salt->c_str()); {
size_t saltBufSize = Base64decode_len(szSalt); string byteString = hex.substr(i, 2);
auto *passwordBuf = (unsigned char *) malloc(sizeof(char) * passwordBufSize); uint8_t byte = (uint8_t)strtol(byteString.c_str(), nullptr, 16);
auto *saltBuf = (unsigned char *) malloc(sizeof(char) * saltBufSize); printf("%hhu ", byte);
bytes.push_back(byte);
}
return bytes;
}
int passwordBufLen = Base64decode((char *) passwordBuf, szPassword); int crypto_scrypt_base64(std::string *password, std::string *salt, uint64_t N,
int saltBufLen = Base64decode((char *) saltBuf, szSalt); uint32_t r, uint32_t p, size_t size, std::string *value)
auto *buffer = (uint8_t *) malloc(sizeof(char) * size); {
int result = fast_crypto_scrypt((uint8_t *) passwordBuf, passwordBufLen, (uint8_t *) saltBuf, uint8_t *passwordBuf = (uint8_t *)password->c_str();
size_t passLen = password->length();
std::vector<uint8_t> bytes = hexToBytes(salt->c_str());
size_t saltBufLen = bytes.size();
uint8_t *saltBuf = bytes.data();
uint8_t *buffer = (uint8_t *)malloc(sizeof(char) * size);
int result = crypto_scrypt((uint8_t *)passwordBuf, passLen, (uint8_t *)saltBuf,
saltBufLen, N, r, p, buffer, size); saltBufLen, N, r, p, buffer, size);
int b64encSize = Base64encode_len(size); int b64encSize = Base64encode_len(size);
char *szB64Encoded = (char *)malloc(sizeof(char) * b64encSize); char *szB64Encoded = (char *)malloc(sizeof(char) * b64encSize);
int b64encLen = Base64encode(szB64Encoded, (const char *) buffer, size); Base64encode(szB64Encoded, (const char *)buffer, size);
value->assign(szB64Encoded); value->assign(szB64Encoded);
free(buffer); free(buffer);
free(szB64Encoded); free(szB64Encoded);
free(passwordBuf);
free(saltBuf);
return result; return result;
} }
void get_uuid(char *buffer)
void get_uuid(char *buffer) { {
static std::random_device dev; static std::random_device dev;
static std::mt19937 rng(dev()); static std::mt19937 rng(dev());
@ -62,19 +77,24 @@ void get_uuid(char *buffer) {
false, false, false, false, false}; false, false, false, false, false};
std::stringstream res; std::stringstream res;
for (bool i : dash) { for (bool i : dash)
if (i) res << "-"; {
if (i)
res << "-";
res << v[dist(rng)]; res << v[dist(rng)];
res << v[dist(rng)]; res << v[dist(rng)];
} }
res >> buffer; res >> buffer;
} }
void uuid_v4(char *buffer) { void uuid_v4(char *buffer)
{
int rc = uuid_v4_gen(buffer); int rc = uuid_v4_gen(buffer);
if (rc == 1) { if (rc == 1)
} else { {
}
else
{
get_uuid(buffer); get_uuid(buffer);
} }
} }