cocos_js/cocos/scrypt/native-crypto.cpp
2022-07-14 22:32:07 +08:00

53 lines
1.8 KiB
C++

/*
* Copyright (c) 2014, Airbitz, Inc.
* All rights reserved.
*
* See the LICENSE file for more information.
*/
#include "native-crypto.h"
extern "C" {
#include "crypto_scrypt.h"
#include "base64.h"
}
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define COMPRESSED_PUBKEY_LENGTH 33
#define DECOMPRESSED_PUBKEY_LENGTH 65
#define PRIVKEY_LENGTH 64
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)
{
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);
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 b64encSize = Base64encode_len(size);
char *szB64Encoded = (char *)malloc(sizeof(char) * b64encSize);
int b64encLen = Base64encode(szB64Encoded, (const char *) buffer, size);
value->assign(szB64Encoded);
free(buffer);
free(szB64Encoded);
free(passwordBuf);
free(saltBuf);
return result;
}