Skip to content

Commit

Permalink
added crypto support for react native environment
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpubnub committed Dec 5, 2023
1 parent 76bc504 commit 279ce8b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
18 changes: 18 additions & 0 deletions lib/react_native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ var buffer_1 = require("buffer");
var pubnub_common_1 = __importDefault(require("../core/pubnub-common"));
var networking_1 = __importDefault(require("../networking"));
var base64_codec_1 = require("../core/components/base64_codec");
var text_encoding_utf_8_1 = require("text-encoding-utf-8");
require("react-native-get-random-values");
var stringify_buffer_keys_1 = require("../core/components/stringify_buffer_keys");
var common_1 = __importDefault(require("../cbor/common"));
var web_node_1 = require("../networking/modules/web-node");
var react_native_1 = require("../networking/modules/react_native");
var react_native_2 = __importDefault(require("../file/modules/react-native"));
var webCryptoModule_1 = require("../crypto/modules/WebCryptoModule/webCryptoModule");
global.Buffer = global.Buffer || buffer_1.Buffer;
Object.assign(global, {
TextEncoder: text_encoding_utf_8_1.TextEncoder,
TextDecoder: text_encoding_utf_8_1.TextDecoder,
});
global.TextEncoder = text_encoding_utf_8_1.TextEncoder;
global.TextDecoder = text_encoding_utf_8_1.TextDecoder;
var default_1 = /** @class */ (function (_super) {
__extends(default_1, _super);
function default_1(setup) {
Expand All @@ -45,6 +54,15 @@ var default_1 = /** @class */ (function (_super) {
});
setup.sdkFamily = 'ReactNative';
setup.ssl = true;
setup.initCryptoModule = function (cryptoConfiguration) {
return new webCryptoModule_1.CryptoModule({
default: new webCryptoModule_1.LegacyCryptor({
cipherKey: cryptoConfiguration.cipherKey,
useRandomIVs: cryptoConfiguration.useRandomIVs,
}),
cryptors: [new webCryptoModule_1.AesCbcCryptor({ cipherKey: cryptoConfiguration.cipherKey })],
});
};
_this = _super.call(this, setup) || this;
return _this;
}
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@
"cbor-js": "^0.1.0",
"cbor-sync": "^1.0.4",
"lil-uuid": "^0.1.1",
"proxy-agent": "^6.3.0",
"react-native-get-random-values": "^1.10.0",
"superagent": "^8.1.2",
"proxy-agent": "^6.3.0"
"text-encoding-utf-8": "^1.0.2"
},
"devDependencies": {
"@cucumber/cucumber": "^7.3.1",
Expand Down Expand Up @@ -102,4 +104,4 @@
"engine": {
"node": ">=0.8"
}
}
}
21 changes: 20 additions & 1 deletion src/react_native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ import { Buffer } from 'buffer';
import PubNubCore from '../core/pubnub-common';
import Networking from '../networking';
import { decode } from '../core/components/base64_codec';
import { TextDecoder, TextEncoder } from 'text-encoding-utf-8';
import 'react-native-get-random-values';
import { stringifyBufferKeys } from '../core/components/stringify_buffer_keys';
import Cbor from '../cbor/common';
import { del, get, post, patch } from '../networking/modules/web-node';
import { getfile, postfile } from '../networking/modules/react_native';

import PubNubFile from '../file/modules/react-native';
import { stringifyBufferKeys } from '../web';
import { CryptoModule, LegacyCryptor, AesCbcCryptor } from '../crypto/modules/WebCryptoModule/webCryptoModule';

global.Buffer = global.Buffer || Buffer;


Check failure on line 19 in src/react_native/index.js

View workflow job for this annotation

GitHub Actions / Integration and Unit tests (14, ci:node)

Delete `⏎`

Check failure on line 19 in src/react_native/index.js

View workflow job for this annotation

GitHub Actions / Integration and Unit tests (16, ci:node)

Delete `⏎`

Check failure on line 19 in src/react_native/index.js

View workflow job for this annotation

GitHub Actions / Integration and Unit tests (16, ci:web)

Delete `⏎`
Object.assign(global, {
TextEncoder: TextEncoder,
TextDecoder: TextDecoder,
});
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

export default class extends PubNubCore {
constructor(setup) {
setup.cbor = new Cbor((arrayBuffer) => stringifyBufferKeys(CborReader.decode(arrayBuffer)), decode);
Expand All @@ -29,7 +40,15 @@ export default class extends PubNubCore {
});
setup.sdkFamily = 'ReactNative';
setup.ssl = true;

setup.initCryptoModule = (cryptoConfiguration) => {
return new CryptoModule({
default: new LegacyCryptor({
cipherKey: cryptoConfiguration.cipherKey,
useRandomIVs: cryptoConfiguration.useRandomIVs,
}),
cryptors: [new AesCbcCryptor({ cipherKey: cryptoConfiguration.cipherKey })],
});
};
super(setup);
}
}

0 comments on commit 279ce8b

Please sign in to comment.