Apply suggestions from code review
Co-authored-by: Gordon <[email protected]>
This commit is contained in:
committed by
Salvatore Giordano
co-authored by
Gordon
parent
8babf0acac
commit
cc4bc38f95
@@ -34,23 +34,23 @@ Check out the diagram below for an example:
|
|||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
|
|
||||||
### Add dependency
|
### Dependencies
|
||||||
|
|
||||||
Add the [webcrypto](https://pub.dev/packages/webcrypto) package in your pubspec.yaml.
|
Add the [webcrypto](https://pub.dev/packages/webcrypto) package in your `pubspec.yaml` file.
|
||||||
|
|
||||||
```dart
|
```yaml
|
||||||
dependencies:
|
dependencies:
|
||||||
webcrypto: ^0.5.2 # latest version
|
webcrypto: ^0.5.2 # latest version
|
||||||
```
|
```
|
||||||
|
|
||||||
### Generate key pair
|
### Generate Key Pair
|
||||||
|
|
||||||
You will write a function that generates a key pair using the ECDH algorithm and the P-256 elliptic curve (P-256 is well-supported and
|
Write a function that generates a key pair using the **ECDH** algorithm and the **P-256** elliptic curve (**P-256** is well-supported and
|
||||||
offers the right balance of security and performance).
|
offers the right balance of security and performance).
|
||||||
|
|
||||||
The pair will consist of two keys
|
The pair will consist of two keys:
|
||||||
1. PublicKey: The key that is linked to a user to encrypt messages.
|
- **PublicKey**: The key that is linked to a user to encrypt messages.
|
||||||
2. PrivateKey: The key that is stored locally to decrypt messages.
|
- **PrivateKey**: The key that is stored locally to decrypt messages.
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
Future<JsonWebKeyPair> generateKeys() async {
|
Future<JsonWebKeyPair> generateKeys() async {
|
||||||
@@ -107,8 +107,8 @@ Future<List<int>> deriveKey(String senderJwk, String receiverJwk) async {
|
|||||||
|
|
||||||
### Encrypting Messages
|
### Encrypting Messages
|
||||||
|
|
||||||
Once you have generated the Crypto Key, you're ready to encrypt the message.
|
Once you have generated the **Crypto Key**, you're ready to encrypt the message.
|
||||||
You can use the AES-GCM algorithm for its known security/performance balance and browser availability.
|
You can use the **AES-GCM** algorithm for its known security and performance balance and good browser availability.
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
// The "iv" stands for initialization vector (IV). To ensure the encryption’s strength,
|
// The "iv" stands for initialization vector (IV). To ensure the encryption’s strength,
|
||||||
@@ -162,10 +162,10 @@ Future<String> decryptMessage(String encryptedMessage, List<int> deriveKey) asyn
|
|||||||
|
|
||||||
Now that your setup is complete you can use it to implement end-to-end encryption in your app.
|
Now that your setup is complete you can use it to implement end-to-end encryption in your app.
|
||||||
|
|
||||||
### Store User's PublicKey
|
### Store User's Public Key
|
||||||
|
|
||||||
The first thing you need to do is store the generated `publicKey` as an `extraData` property, in order
|
The first thing you need to do is store the generated `publicKey` as an `extraData` property, in order
|
||||||
for other users to use it for encrypting the messages.
|
for other users to encrypt messages.
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
// Generating keyPair using the function defined in above steps
|
// Generating keyPair using the function defined in above steps
|
||||||
@@ -190,7 +190,7 @@ await client.connectUser(
|
|||||||
|
|
||||||
Now you will use the `encryptMessage()` function created in the previous steps to encrypt the message.
|
Now you will use the `encryptMessage()` function created in the previous steps to encrypt the message.
|
||||||
|
|
||||||
To do that, you need to make some minor changes to the `MessageInput` widget.
|
To do that, you need to make some minor changes to the **MessageInput** widget.
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
final receiverJwk = receiver.extraData['publicKey'];
|
final receiverJwk = receiver.extraData['publicKey'];
|
||||||
@@ -201,7 +201,9 @@ final derivedKey = await deriveKey(keyPair.privateKey, receiverJwk);
|
|||||||
|
|
||||||
```dart
|
```dart
|
||||||
MessageInput(
|
MessageInput(
|
||||||
|
|
||||||
...
|
...
|
||||||
|
|
||||||
preMessageSending: (message) async {
|
preMessageSending: (message) async {
|
||||||
// Encrypting the message text using derivedKey
|
// Encrypting the message text using derivedKey
|
||||||
final encryptedMessage = await encryptMessage(message.text, derivedKey);
|
final encryptedMessage = await encryptMessage(message.text, derivedKey);
|
||||||
@@ -221,7 +223,7 @@ Here, you have used it to encrypt the message before sending it to Stream’s ba
|
|||||||
|
|
||||||
Now, it’s time to decrypt the message and present it in a human-readable format to the receiver.
|
Now, it’s time to decrypt the message and present it in a human-readable format to the receiver.
|
||||||
|
|
||||||
You can customize the `MessageListView` widget to have a custom `messagebuilder`, that can decrypt the message.
|
You can customize the **MessageListView** widget to have a custom `messagebuilder`, that can decrypt the message.
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
MessageListView(
|
MessageListView(
|
||||||
|
|||||||
Reference in New Issue
Block a user