diff --git a/desktop/CMakeLists.txt b/desktop/CMakeLists.txt new file mode 100644 index 0000000..406b60e --- /dev/null +++ b/desktop/CMakeLists.txt @@ -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) diff --git a/desktop/rnsecurerandom.cpp b/desktop/rnsecurerandom.cpp new file mode 100644 index 0000000..7ffec50 --- /dev/null +++ b/desktop/rnsecurerandom.cpp @@ -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 + * + */ + +#include + +#include "bridge.h" +#include "rnsecurerandom.h" +#include +#include +#include +#include + +namespace { +struct RegisterQMLMetaType { + RegisterQMLMetaType() { + qRegisterMetaType(); + } +} 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: "<bridge, QVariantList{QVariant{result}}); + +} + +QString RNSecureRandom::moduleName() { + return "RNSecureRandom"; +} + +QList RNSecureRandom::methodsToExport() { + return QList{}; +} + +QVariantMap RNSecureRandom::constantsToExport() { + return QVariantMap{}; +} + +void RNSecureRandom::setBridge(Bridge* bridge) { + Q_D(RNSecureRandom); + d->bridge = bridge; +} diff --git a/desktop/rnsecurerandom.h b/desktop/rnsecurerandom.h new file mode 100644 index 0000000..e6746e7 --- /dev/null +++ b/desktop/rnsecurerandom.h @@ -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 + * + */ + +#ifndef RNSECURERANDOM_H +#define RNSECURERANDOM_H + +#include + +#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 methodsToExport() override; + virtual QVariantMap constantsToExport() override; + virtual void setBridge(Bridge* bridge) override; + +private: + QScopedPointer d_ptr; +}; + +#endif // RNSECURERANDOM_H diff --git a/index.js b/index.js index ccd6d4e..1c67f31 100644 --- a/index.js +++ b/index.js @@ -6,8 +6,8 @@ import { toByteArray } from 'base64-js'; const { RNSecureRandom } = NativeModules; export function generateSecureRandom(length: number): Promise { - 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) {