mirror of
https://github.com/status-im/react-native-securerandom.git
synced 2026-08-27 09:01:14 +00:00
Desktop support added
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
set(REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_TYPE_NAMES ${REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_TYPE_NAMES}
|
||||
\"RNSecureRandom\" PARENT_SCOPE)
|
||||
|
||||
set(REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_SRC ${REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_SRC}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rnsecurerandom.cpp PARENT_SCOPE)
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Copyright (C) 2016, Canonical Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* Author: Justin McPherson <justin.mcpherson@canonical.com>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "bridge.h"
|
||||
#include "rnsecurerandom.h"
|
||||
#include <QCryptographicHash>
|
||||
#include <QDateTime>
|
||||
#include <QDebug>
|
||||
#include <QMap>
|
||||
|
||||
namespace {
|
||||
struct RegisterQMLMetaType {
|
||||
RegisterQMLMetaType() {
|
||||
qRegisterMetaType<RNSecureRandom*>();
|
||||
}
|
||||
} registerMetaType;
|
||||
} // namespace
|
||||
|
||||
class RNSecureRandomPrivate {
|
||||
public:
|
||||
|
||||
Bridge* bridge = nullptr;
|
||||
};
|
||||
|
||||
RNSecureRandom::RNSecureRandom(QObject* parent) : QObject(parent), d_ptr(new RNSecureRandomPrivate) {}
|
||||
|
||||
RNSecureRandom::~RNSecureRandom() {}
|
||||
|
||||
void RNSecureRandom::generateSecureRandomAsBase64(int size,
|
||||
const ModuleInterface::ListArgumentBlock &resolve,
|
||||
const ModuleInterface::ListArgumentBlock &reject) {
|
||||
Q_D(RNSecureRandom);
|
||||
|
||||
qDebug()<<"invoked RNSecureRandom::generateSecureRandomAsBase64";
|
||||
|
||||
QByteArray randomString;
|
||||
qsrand(QDateTime::currentDateTime().currentMSecsSinceEpoch());
|
||||
while (randomString.size() < size) {
|
||||
randomString.push_back(qrand());
|
||||
}
|
||||
QString result = randomString.toBase64();
|
||||
|
||||
qDebug()<<"RNSecureRandom::generateSecureRandomAsBase64 result: "<<result;
|
||||
|
||||
resolve(d->bridge, QVariantList{QVariant{result}});
|
||||
|
||||
}
|
||||
|
||||
QString RNSecureRandom::moduleName() {
|
||||
return "RNSecureRandom";
|
||||
}
|
||||
|
||||
QList<ModuleMethod*> RNSecureRandom::methodsToExport() {
|
||||
return QList<ModuleMethod*>{};
|
||||
}
|
||||
|
||||
QVariantMap RNSecureRandom::constantsToExport() {
|
||||
return QVariantMap{};
|
||||
}
|
||||
|
||||
void RNSecureRandom::setBridge(Bridge* bridge) {
|
||||
Q_D(RNSecureRandom);
|
||||
d->bridge = bridge;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
|
||||
/**
|
||||
* Copyright (C) 2016, Canonical Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* Author: Justin McPherson <justin.mcpherson@canonical.com>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef RNSECURERANDOM_H
|
||||
#define RNSECURERANDOM_H
|
||||
|
||||
#include <QUrl>
|
||||
|
||||
#include "moduleinterface.h"
|
||||
|
||||
class RNSecureRandomPrivate;
|
||||
class RNSecureRandom : public QObject, public ModuleInterface {
|
||||
Q_OBJECT
|
||||
|
||||
Q_INTERFACES(ModuleInterface)
|
||||
|
||||
Q_DECLARE_PRIVATE(RNSecureRandom)
|
||||
|
||||
public:
|
||||
|
||||
Q_INVOKABLE RNSecureRandom(QObject* parent = 0);
|
||||
~RNSecureRandom();
|
||||
|
||||
Q_INVOKABLE REACT_PROMISE void generateSecureRandomAsBase64(int size,
|
||||
const ModuleInterface::ListArgumentBlock &resolve,
|
||||
const ModuleInterface::ListArgumentBlock &reject);
|
||||
virtual QString moduleName() override;
|
||||
virtual QList<ModuleMethod*> methodsToExport() override;
|
||||
virtual QVariantMap constantsToExport() override;
|
||||
virtual void setBridge(Bridge* bridge) override;
|
||||
|
||||
private:
|
||||
QScopedPointer<RNSecureRandomPrivate> d_ptr;
|
||||
};
|
||||
|
||||
#endif // RNSECURERANDOM_H
|
||||
@@ -6,8 +6,8 @@ import { toByteArray } from 'base64-js';
|
||||
const { RNSecureRandom } = NativeModules;
|
||||
|
||||
export function generateSecureRandom(length: number): Promise<Uint8Array> {
|
||||
if (Platform.OS !== 'ios' && Platform.OS !== 'android') {
|
||||
throw Error('react-native-securerandom is currently only available for iOS and Android');
|
||||
if (Platform.OS !== 'ios' && Platform.OS !== 'android' && Platform.OS !== 'desktop') {
|
||||
throw Error('react-native-securerandom is currently only available for iOS, Android and Desktop');
|
||||
}
|
||||
|
||||
if (!RNSecureRandom || !RNSecureRandom.generateSecureRandomAsBase64) {
|
||||
|
||||
Reference in New Issue
Block a user