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