8000 Add encryption/decryption docs (#41) · SRWieZ/fork-nativephp.com@5ac9514 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ac9514

Browse files
authored
Add encryption/decryption docs (NativePHP#41)
1 parent bc8f13b commit 5ac9514

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

resources/views/docs/1/digging-deeper/security.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ level of entropy, as this makes them hard to guess and hard to abuse.
3030

3131
If your application allows users to connect _their own_ API keys for a service, you should treat these keys with great
3232
care. If you choose to store them anywhere (either in a [File](/docs/digging-deeper/files) or
33-
[Database](/docs/digging-deeper/databases)), make sure you store them encrypted and decrypt them only when in use.
33+
[Database](/docs/digging-deeper/databases)), make sure you store them
34+
[encrypted](/docs/the-basics/system#encryption-decryption) and decrypt them only when needed.
3435

3536
See [Environment Files](/docs/getting-started/env-files#removing-sensitive-data-from-your-environment-files) for details
3637
on how to redact your `.env` files at build-time.

resources/views/docs/1/the-basics/system.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,51 @@ the platform on which your app is running.
1818
While some features are platform-specific, NativePHP gracefully handles this for you so that you don't have to think
1919
about whether something is Linux-, Mac-, or Windows-only.
2020

21+
## Encryption / Decryption
22+
23+
Almost every non-trivial application will require some concept of secure data storage and retrieval. For example, if
24+
you want to generate and store an API key to access a third-party service on behalf of your user.
25+
26+
You shouldn't ship these sorts of secrets _with_ your app, but rather generate them or ask your user for them at
27+
runtime.
28+
29+
But when your app is running on a user's device, you have
30+
[far less control and fewer guarantees](/docs/digging-deeper/security) over the safety of any secrets stored.
31+
32+
On a traditional server-rendered application, this is a relatively simple problem to solve using server-side encryption
33+
with keys which are hidden from end users.
34+
35+
For this to work on the user's device, you need to be able to generate and store an encryption key securely.
36+
37+
NativePHP takes care of the key generation and storage for you, all that's left for you to do is encrypt, store and
38+
decrypt the secrets that you need to store on behalf of your user.
39+
40+
NativePHP allows you to encrypt and decrypt data in your application easily:
41+
42+
```php
43+
use Native\Laravel\Facades\System;
44+
45+
if (System::canEncrypt()) {
46+
$encrypted = System::encrypt('secret_key_a79hiunfw86...');
47+
48+
// $encrypted => 'djEwJo+Huv+aeBgUoav5nIJWRQ=='
49+
}
50+
```
51+
52+
You can then safely store the encrypted string in a database or the filesystem.
53+
54+
When you need to get the original value, you can decrypt it:
55+
56+
```php
57+
use Native\Laravel\Facades\System;
58+
59+
if (System::canEncrypt()) {
60+
$decrypted = System::decrypt('djEwJo+Huv+aeBgUoav5nIJWRQ==');
61+
62+
// $decrypted = 'secret_key_a79hiunfw86...'
63+
}
64+
```
65+
2166
## TouchID
2267

2368
For Mac systems that support TouchID, you can use TouchID to protect and unlock various parts of your application.

0 commit comments

Comments
 (0)
0