@@ -18,6 +18,51 @@ the platform on which your app is running.
18
18
While some features are platform-specific, NativePHP gracefully handles this for you so that you don't have to think
19
19
about whether something is Linux-, Mac-, or Windows-only.
20
20
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
+
21
66
## TouchID
22
67
23
68
For Mac systems that support TouchID, you can use TouchID to protect and unlock various parts of your application.
0 commit comments