Skip to content

Commit

Permalink
ADD article
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Jan 23, 2025
1 parent 0f039e3 commit bcf0853
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,4 @@ Get started now by [reading the docs](https://rxdb.info/quickstart.html) or expl
that we need to rank better.
-->
#### More content
[Angular Database](https://rxdb.info/articles/angular-database.html), [Frontend Database](https://rxdb.info/articles/frontend-database.html), [localStorage](https://rxdb.info/articles/localstorage.html), [React Database](https://rxdb.info/articles/react-database.html), [Browser Database](https://rxdb.info/articles/browser-database.html), [React Native Database](https://rxdb.info/react-native-database.html), [PWA Database](https://rxdb.info/articles/progressive-web-app-database.html), [In-memory NoSQL database](https://rxdb.info/articles/in-memory-nosql-database.html), [JSON database](https://rxdb.info/articles/json-database.html), [Angular IndexedDB](https://rxdb.info/articles/angular-indexeddb.html), [React IndexedDB](https://rxdb.info/articles/react-indexeddb.html), [Optimistic UI](https://rxdb.info/articles/optimistic-ui.html), [local database](https://rxdb.info/articles/local-database.html)
[Angular Database](https://rxdb.info/articles/angular-database.html), [Frontend Database](https://rxdb.info/articles/frontend-database.html), [localStorage](https://rxdb.info/articles/localstorage.html), [React Database](https://rxdb.info/articles/react-database.html), [Browser Database](https://rxdb.info/articles/browser-database.html), [React Native Database](https://rxdb.info/react-native-database.html), [PWA Database](https://rxdb.info/articles/progressive-web-app-database.html), [In-memory NoSQL database](https://rxdb.info/articles/in-memory-nosql-database.html), [JSON database](https://rxdb.info/articles/json-database.html), [Angular IndexedDB](https://rxdb.info/articles/angular-indexeddb.html), [React IndexedDB](https://rxdb.info/articles/react-indexeddb.html), [Optimistic UI](https://rxdb.info/articles/optimistic-ui.html), [local database](https://rxdb.info/articles/local-database.html), [React Native Encryption](https://rxdb.info/articles/react-native-encryption.html)
2 changes: 1 addition & 1 deletion docs-src/docs/articles/ideas.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

X- "optimistic ui"
X- "local database"
X- "react-native encryption"

- "firebase alternative"

Expand All @@ -20,7 +21,6 @@ X- "local database"

- "react localstorage"

- "react-native encryption"

- "local database"

Expand Down
2 changes: 1 addition & 1 deletion docs-src/docs/articles/optimistic-ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Optimistic UIs offer a host of advantages, from improving the user experience to

<center>
<a href="https://rxdb.info/">
<img src="../files/logo/rxdb_javascript_database.svg" alt="RxDB Flutter Database" width="220" />
<img src="../files/logo/rxdb_javascript_database.svg" alt="RxDB Database" width="220" />
</a>
</center>

Expand Down
199 changes: 199 additions & 0 deletions docs-src/docs/articles/react-native-encryption.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
---
title: React Native Encrpytion and Encrypted Database/Storage

Check failure on line 2 in docs-src/docs/articles/react-native-encryption.md

View workflow job for this annotation

GitHub Actions / test-code-style

Encrpytion ==> Encryption
slug: react-native-encryption.html
description: Secure your React Native app with RxDB encryption. Learn why it matters, how to implement encrypted databases, and best practices to protect user data.
---


# React Native Encryption and Encrypted Database/Storage

Data security is a critical concern in modern mobile applications. As React Native continues to grow in popularity for building cross-platform apps, ensuring that your data is protected—both at rest and in transit—is paramount. RxDB, a real-time database for JavaScript applications, offers powerful encryption features that can help you secure your React Native app's data.

This article explains why encryption is important, how to set it up with RxDB in [React Native](../react-native-database.md), and best practices to keep your app secure.

## Why Encryption Matters

Encryption ensures that, even if an unauthorized party obtains physical access to your device or intercepts data, they cannot read the information without the encryption key. Sensitive user data such as credentials, personal information, or financial details should always be encrypted. Proper encryption practices reduce the risk of data breaches and help your application remain compliant with regulations like [GDPR](https://gdpr.eu/) or [HIPAA](https://www.hhs.gov/hipaa/index.html).


## React Native Encryption Overview


React Native supports multiple ways to secure data at rest:

1. **Encrypted Databases**
Use databases with built-in encryption capabilities, such as SQLite with encryption layers or RxDB with its [encryption plugin](../encryption.md).

2. **Secure Storage Libraries**
For key-value data (like tokens or secrets), you can use libraries like [react-native-keychain](https://github.com/oblador/react-native-keychain) or [react-native-encrypted-storage](https://github.com/emeraldsanto/react-native-encrypted-storage).

3. **Custom Encryption**
If you need more fine-grained control, you can integrate libraries like [`crypto-js`](https://github.com/brix/crypto-js) or the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) to encrypt data before storing it in a database or file.



<center>
<a href="https://rxdb.info/">
<img src="/files/logo/rxdb_javascript_database.svg" alt="RxDB" width="250" />
</a>
</center>

## Setting Up Encryption in RxDB for React Native

### 1. Install RxDB and Required Plugins

Install RxDB and the encryption plugin(s) you need. For the CryptoJS plugin:

```bash
npm install rxdb
npm install crypto-js
```

### 2. Set Up Your RxDB Database with Encryption

RxDB offers two [encryption plugins](../encryption.md):
- **CryptoJS Plugin**: A free and straightforward solution for most basic use cases.
- **Web Crypto Plugin**: A [premium plugin](/premium) that utilizes the native Web Crypto API for better performance and security.

Below is an example showing how to set up RxDB using the CryptoJS plugin. This example uses the [in-memory storage](../rx-storage-memory.md) for testing purposes. In a real production scenario, you would use a persistent storage adapter, mostly the [SQLite-based storage](../rx-storage-sqlite.md).

```js
import { createRxDatabase } from 'rxdb';
import { wrappedKeyEncryptionCryptoJsStorage } from 'rxdb/plugins/encryption-crypto-js';

/*
* For testing, we use the in-memory storage of RxDB.
* In production you would use the persistend SQLite based storage instead.
*/
import { getRxStorageMemory } from 'rxdb/plugins/storage-memory';

async function initEncryptedDatabase() {
// Wrap the normal storage with the encryption plugin
const encryptedMemoryStorage = wrappedKeyEncryptionCryptoJsStorage({
storage: getRxStorageMemory()
});

// Create an encrypted database
const db = await createRxDatabase({
name: 'myEncryptedDatabase',
storage: encryptedMemoryStorage,
password: 'sudoLetMeIn' // Make sure not to hardcode in production
});

// Define a schema and create a collection
await db.addCollections({
secureData: {
schema: {
title: 'secure data schema',
version: 0,
type: 'object',
primaryKey: 'id',
properties: {
id: {
type: 'string',
maxLength: 100
},
normalField: {
type: 'string'
},
secretField: {
type: 'string'
}
},
required: ['id', 'normalField', 'secretField']
}
}
});

return db;
}
```


### 3. Inserting and Querying Encrypted Data

Once you've set up the database with encryption, data in fields specified by your schema will be encrypted automatically before it is stored, and decrypted when queried.

```js
(async () => {
const db = await initEncryptedDatabase();

// Insert encrypted data
const doc = await db.secureData.insert({
id: 'mySecretId',
normalField: 'foobar',
secretField: 'This is top secret data'
});

// Query encrypted data by its primary key or non-encrypted fields
const fetchedDoc = await db.secureData.findOne({
selector: {
normalField: 'foobar'
}
}).exec(true);
console.log(fetchedDoc.secretField); // 'This is top secret data'

// Update data
await fetchedDoc.patch({
secretField: 'Updated secret data'
});
})();
```

**Note**: You can only query directly by non-encrypted fields or primary keys. Encrypted fields cannot be used in queries because they are stored as ciphertext in the database. A common approach is to have a small subset of fields that need to be queried unencrypted while storing any sensitive data in encrypted fields.

## Best Practices for React Native Encryption


- **Secure Password Handling**
- Avoid hardcoding passwords or encryption keys.
- Use secure storage solutions like React Native Keychain or react-native-encrypted-storage to fetch the database password at runtime:

```js
// Example: using react-native-keychain to securely retrieve a stored password
import * as Keychain from 'react-native-keychain';

async function getDatabasePassword() {
const credentials = await Keychain.getGenericPassword();
if (credentials) {
return credentials.password;
}
throw new Error('No password stored in Keychain');
}
```

- **Encrypt Attachments**:
If you need to store files (images, text files, etc.), consider encrypting attachments. RxDB supports attachments that can be encrypted automatically, ensuring your files are protected:

```ts
import { createBlob } from 'rxdb/plugins/core';
const doc = await await db.secureData.findOne({
selector: {
normalField: 'foobar'
}
}).exec(true);
const attachment = await doc.putAttachment({
id: 'encryptedFile.txt',
data: createBlob('Sensitive content', 'text/plain'),
type: 'text/plain',
});
```

- **Optimize Performance**
- If performance is critical, consider using the premium Web Crypto plugin, which leverages native APIs for faster encryption and decryption.
- If big chunks of data are encrypted, store them in attachments instead of document fields. Attachments will only be decrypted on explicit fetches, not during queries.

- **Use DevMode in Development**: RxDB's [DevMode Plugin](../dev-mode.md) can help validate your schema and encryption setup during development. Disable it in production for performance reasons.

- **Secure Communication**:
- Use HTTPS to secure network communication between the app and any backend services.
- If you're synchronizing data to a server, ensure the data is also encrypted in transit. RxDB's [replication plugins](../replication.md) can work with secure endpoints to keep data consistent.

- **SSL Pinning**: Consider SSL Pinning if you want to prevent man-in-the-middle attacks. SSL Pinning ensures the device trusts only the pinned certificate, preventing attackers from swapping out valid certificates with their own.

## Follow Up

- Learn how to use RxDB with the [RxDB Quickstart](../quickstart.md) for a guided introduction.
- Check out the [RxDB GitHub repository](https://github.com/pubkey/rxdb) and leave a star ⭐ if you find it useful.

By following these best practices and leveraging RxDB's powerful encryption plugins, you can build secure, performant, and robust React Native applications that keep your users' data safe.
2 changes: 1 addition & 1 deletion docs-src/docs/cleanup.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Cleanup
slug: cleanup.html
description: Optimize storage and speed up queries with RxDBs Cleanup Plugin, automatically removing old deleted docs while preserving replication states.
description: Optimize storage and speed up queries with RxDB's Cleanup Plugin, automatically removing old deleted docs while preserving replication states.
---


Expand Down
2 changes: 1 addition & 1 deletion docs-src/docs/fulltext-search.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Fulltext Search 👑
slug: fulltext-search.html
description: Master local fulltext search with RxDBs FlexSearch plugin. Enjoy real-time indexing, efficient queries, and offline-first support made easy.
description: Master local fulltext search with RxDB's FlexSearch plugin. Enjoy real-time indexing, efficient queries, and offline-first support made easy.
---


Expand Down
Loading

0 comments on commit bcf0853

Please sign in to comment.