Skip to content

Commit a9e9a67

Browse files
authored
Merge pull request #799 from ethersphere/feat/improve-store-with-encryption
improve encryption page
2 parents 4bdcda5 + 139b855 commit a9e9a67

1 file changed

Lines changed: 40 additions & 13 deletions

File tree

docs/develop/tools-and-features/store-with-encryption.md

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,68 @@ id: store-with-encryption
44
description: Guide for encrypting data before upload to protect privacy and confidentiality.
55
---
66

7-
In Swarm, all data is _public_ by default. To protect sensitive content, it must be encrypted so that only authorised users are able to decrypt and then view the plaintext content.
7+
In Swarm, all data is _public_ by default. To protect sensitive content, it must be encrypted so that only authorised users are able to decrypt and view the plaintext content.
88

9-
The Bee client provides a facility to encrypt files and directories while uploading which are only able to be read by users with access to the corresponding decryption key.
9+
The Bee client can encrypt files and directories during upload, producing a reference that bundles a Swarm address with a decryption key. Only those in possession of the full reference can decrypt the content.
1010

11-
# Encrypt and Upload a File
11+
## Encrypt and Upload a File
1212

13-
To encrypt a file simply include the `Swarm-Encrypt: true` header with your HTTP request.
13+
Include the `Swarm-Encrypt: true` header with your upload request:
1414

1515
```bash
16-
curl -F file=@bee.jpg -H "Swarm-Postage-Batch-Id: 78a26be9b42317fe6f0cbea3e47cbd0cf34f533db4e9c91cf92be40eb2968264" -H "Swarm-Encrypt: true" http://localhost:1633/bzz
16+
curl -F file=@bee.jpg \
17+
-H "Swarm-Postage-Batch-Id: 78a26be9b42317fe6f0cbea3e47cbd0cf34f533db4e9c91cf92be40eb2968264" \
18+
-H "Swarm-Encrypt: true" \
19+
http://localhost:1633/bzz
1720
```
1821

19-
When successful, the Bee client will return a 64 byte reference, instead of the usual 32 bytes.
20-
2122
More information on how to buy a postage stamp batch and get its batch id can be found [here](./buy-a-stamp-batch.md).
2223

24+
When successful, Bee returns a 128-character (64-byte) reference instead of the usual 64-character (32-byte) unencrypted reference:
25+
2326
```json
2427
{
2528
"reference": "f7b1a45b70ee91d3dbfd98a2a692387f24db7279a9c96c447409e9205cf265baef29bf6aa294264762e33f6a18318562c86383dd8bfea2cec14fae08a8039bf3"
2629
}
2730
```
2831

29-
Here we see that, when using the Bee node's encryption function, the reference hash that is returned is 128 hex characters long. The first 64 characters of this is the familiar Swarm address - the reference that allows us to retrieve the data from the swarm. This is the same reference we would get uploading unencrypted files using Bee, so it is safe to share.
32+
The reference is composed of two 64-character (32-byte) parts:
33+
34+
```
35+
f7b1a45b70ee91d3dbfd98a2a692387f24db7279a9c96c447409e9205cf265ba ← Swarm address (safe to share)
36+
ef29bf6aa294264762e33f6a18318562c86383dd8bfea2cec14fae08a8039bf3 ← decryption key (keep private)
37+
```
3038

31-
The second part of the reference is a 64 character decryption key which is required to decrypt the referenced content and view the original data in the clear. This is sensitive key material and must be kept private.
39+
The Swarm address (first 64 characters) is a standard content address — the same identifier you would get from an unencrypted upload and safe to share publicly. The decryption key (last 64 characters) is sensitive: anyone who holds the full 128-character reference can decrypt and read the original content. Access control is entirely possession-based — there is no server-side revocation. The key is never transmitted to the Swarm network; Bee only exposes it through the local API response.
3240

33-
It is important that this data not be sent in requests to a public gateway as this would mean that gateway would be able to decrypt your data. However, if you are running a node on your local machine, you may safely use the API bound to `localhost`. The key material is never exposed to the network so your data remains safe.
41+
:::warning
42+
If you lose the decryption key portion of the reference, the encrypted data becomes permanently unrecoverable. Store the full 128-character reference in a secure location such as a password manager.
43+
:::
3444

3545
:::info
36-
Encryption is disabled by default on all Swarm Gateways to keep your data safe. [Install Bee on your computer](./../../bee/installation/getting-started.md) to use the encryption feature.
46+
Encryption is disabled by default on all Swarm gateways to protect your data. [Install Bee on your computer](./../../bee/installation/getting-started.md) to use the encryption feature.
3747
:::
3848

39-
# Download and Decrypt a File
49+
## Encrypt and Upload a Directory
50+
51+
To upload an entire directory with encryption, package it as a tar archive and set the `Swarm-Collection: true` header:
52+
53+
```bash
54+
tar -cf site.tar ./my-website/
55+
56+
curl --data-binary @site.tar \
57+
-H "Content-Type: application/x-tar" \
58+
-H "Swarm-Collection: true" \
59+
-H "Swarm-Postage-Batch-Id: 78a26be9b42317fe6f0cbea3e47cbd0cf34f533db4e9c91cf92be40eb2968264" \
60+
-H "Swarm-Encrypt: true" \
61+
http://localhost:1633/bzz
62+
```
63+
64+
The response follows the same 128-character reference format. All files in the collection are encrypted with the same key, and the full reference is required to access any file within it.
65+
66+
## Download and Decrypt a File
4067

41-
To retrieve your file, simply supply the full 64 byte string to the files endpoint, and the Bee client will download and decrypt all the relevant chunks and restore them to their original format.
68+
Supply the full 128-character reference to the `/bzz` endpoint. Bee downloads all the relevant chunks, decrypts them, and returns the original content:
4269

4370
```bash
4471
curl -OJ http://localhost:1633/bzz/f7b1a45b70ee91d3dbfd98a2a692387f24db7279a9c96c447409e9205cf265baef29bf6aa294264762e33f6a18318562c86383dd8bfea2cec14fae08a8039bf3

0 commit comments

Comments
 (0)