-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
197 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "crypto_keygen.h" | ||
|
||
#ifdef ANDROID | ||
#include "JSIUtils/MGLJSIMacros.h" | ||
#include "Utils/MGLUtils.h" | ||
#else | ||
#include "MGLJSIUtils.h" | ||
#include "MGLUtils.h" | ||
#endif | ||
|
||
namespace margelo { | ||
|
||
jsi::Value SecretKeyGen::DoKeyGen(jsi::Runtime &rt, const jsi::Value *args) { | ||
auto skg = new SecretKeyGen(FnMode::kAsync); | ||
CHECK(skg->getParamsFromJS(rt, args)); | ||
CHECK(skg->doKeyGen()); | ||
return toJSI(rt, std::move(skg->getKey())); | ||
} | ||
|
||
jsi::Value SecretKeyGen::DoKeyGenSync(jsi::Runtime &rt, const jsi::Value *args) { | ||
auto skg = new SecretKeyGen(FnMode::kSync); | ||
CHECK(skg->getParamsFromJS(rt, args)); | ||
CHECK(skg->doKeyGen()); | ||
return toJSI(rt, std::move(skg->getKey())); | ||
} | ||
|
||
bool SecretKeyGen::getParamsFromJS(jsi::Runtime &rt, const jsi::Value *args) { | ||
SecretKeyGenConfig params; | ||
unsigned int offset = 0; | ||
|
||
// length | ||
CHECK(CheckIsUint32(args[offset])); | ||
uint32_t bits = (uint32_t)args[offset].asNumber(); | ||
params.length = bits / CHAR_BIT; | ||
|
||
this->params_ = std::move(params); | ||
return true; | ||
} | ||
|
||
bool SecretKeyGen::doKeyGen() { | ||
// TODO: threading / async here, as we don't have jsi::Runtime | ||
ByteSource::Builder bytes(this->params_.length); | ||
if (CSPRNG(bytes.data<unsigned char>(), this->params_.length).is_err()) | ||
return false; | ||
this->key_ = std::move(bytes).release(); | ||
return true; | ||
} | ||
|
||
ByteSource SecretKeyGen::getKey() { | ||
return std::move(this->key_); | ||
} | ||
|
||
} // namespace margelo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef crypto_keygen_h | ||
#define crypto_keygen_h | ||
|
||
#include <jsi/jsi.h> | ||
|
||
#include "MGLKeys.h" | ||
#ifdef ANDROID | ||
#include "Utils/MGLUtils.h" | ||
#else | ||
#include "MGLUtils.h" | ||
#endif | ||
|
||
namespace margelo | ||
{ | ||
|
||
namespace jsi = facebook::jsi; | ||
|
||
struct SecretKeyGenConfig { | ||
size_t length; // In bytes. | ||
ByteSource out; // Placeholder for the generated key bytes. | ||
|
||
SecretKeyGenConfig() = default; | ||
}; | ||
|
||
class SecretKeyGen { | ||
public: | ||
static jsi::Value DoKeyGen(jsi::Runtime &rt, const jsi::Value *args); | ||
static jsi::Value DoKeyGenSync(jsi::Runtime &rt, const jsi::Value *args); | ||
inline SecretKeyGen(FnMode mode) { | ||
this->setMode(mode); | ||
} | ||
private: | ||
inline void setMode(FnMode mode) { mode_ = mode; }; | ||
bool getParamsFromJS(jsi::Runtime &rt, const jsi::Value *args); | ||
bool doKeyGen(); | ||
ByteSource getKey(); | ||
|
||
FnMode mode_; | ||
SecretKeyGenConfig params_; | ||
ByteSource key_; | ||
}; | ||
|
||
} // namespace margelo | ||
|
||
#endif // crypto_keygen_h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters