@@ -39,66 +39,60 @@ The device's secure storage encrypts and decrypts data on the fly and that means
3939critical things like API tokens, keeping your users and your systems safe.
4040
4141This data is only accessible by your app and is persisted beyond the lifetime of your app, so it will still be available
42- the next time your app is open .
42+ the next time your app is opened .
4343
44- ### Why not use the Laravel ` Crypt ` facade?
4544
46- By default, the ` Crypt ` facade - and by extension the ` encrypt ` and ` decrypt ` helper functions - all rely on the
47- ` APP_KEY ` value set in your ` .env ` file.
45+ <aside >
4846
49- We _ will_ use Laravel's underlying ` Encryption ` class, but you should avoid using these helpers directly.
47+ Secure Storage is only meant for small amounts of text data, usually no more than a few KBs. If you need to store
48+ larger amounts of data or files, you should store this in a database or as a file.
5049
51- In the context of distributed apps, the ` APP_KEY ` is shipped _ with_ your app and therefore isn't secure. Anyone who
52- knows where to look for it will be able to find it. Then any data encrypted with it is no better off than if it was
53- stored in plain text.
50+ </aside >
5451
55- Also, it will be the same key for every user, and this presents a considerable risk.
52+ ### When to use the Laravel ` Crypt ` facade
5653
57- What you really want is a ** unique key for each user** , and for that you really need to generate your encryption key
58- once your app is installed on your user's device.
54+ When a user first opens your app, NativePHP generates a ** unique ` APP_KEY ` just for their device** and stores it in the
55+ device's secure storage. This means each instance of your application has its own encryption key that is securely
56+ stored on the device.
5957
60- You could do this and update the ` .env ` file, but it would still be stored in a way that an attacker may be able to
61- exploit.
58+ NativePHP securely reads the ` APP_KEY ` from secure storage and makes it available to Laravel. So you can safely use the
59+ ` Crypt ` facade to encrypt and decrypt data!
6260
63- A better approach is to generate a secure key the first time your app opens, place that key in Secure Storage, and
64- then use that key to encrypt your other data before storage:
61+ <aside >
6562
66- ``` php
67- use Illuminate\Encryption\Encrypter;
68- use Illuminate\Support\Facades\Storage;
69- use Native\Mobile\Facades\SecureStorage;
63+ Make sure you do not leak the ` APP_KEY ` or decrypted data inadvertently through error tracking or debug logging tools.
7064
71- function generateRandomKey()
72- {
73- return base64_encode(
74- Encrypter::generateKey(config('app.cipher'))
75- );
76- }
65+ </aside >
7766
78- $encryptionKey = SecureStorage::get('encryption_key');
67+ This is great for encrypting larger amounts of data that wouldn't easily fit in secure storage. You can encrypt values
68+ and store them in the file system or in the SQLite database, knowing that they are safe at rest:
7969
80- if (! $encryptionKey) {
81- SecureStorage::set('encryption_key', $encryptionKey = generateRandomKey());
82- }
83-
84- $mobileEncrypter = new Encrypter($encryptionKey);
70+ ``` php
71+ use Illuminate\Support\Facades\Crypt;
8572
86- $encryptedContents = $mobileEncrypter->encrypt (
73+ $encryptedContents = Crypt::encryptString (
8774 $request->file('super_private_file')
8875);
8976
90- Storage::put('my_secure_file.pdf ', $encryptedContents);
77+ Storage::put('my_secure_file', $encryptedContents);
9178```
9279
9380And then decrypt it later:
9481
9582``` php
96- $decryptedContents = $mobileEncrypter->decrypt (
97- Storage::get('my_secure_file.pdf ')
83+ $decryptedContents = Crypt::decryptString (
84+ Storage::get('my_secure_file')
9885);
9986```
10087
101- ### Secure Storage vs Database/Files
88+ <aside >
89+
90+ Data encrypted with the ` Crypt ` facade should stay on the user's device with your app. Placing it encrypted anywhere
91+ else risks the chance that it will be unrecoverable. If the user loses their device or deletes your app,
92+ they will lose the encryption key and the data will be encrypted forever.
93+
94+ If you wish to share data, decrypt it first, transmit securely (e.g. over HTTPS) and re-encrypt it with a different key
95+ that is safely managed elsewhere.
96+
97+ </aside >
10298
103- Secure Storage is only meant for small amounts of text data, usually no more than a few KBs. If you need to store
104- larger amounts of data or files, you should store this in a database or as a file.
0 commit comments