Skip to content

Commit 2877e05

Browse files
authored
Merge pull request #2417 from dafriend/encryption-service-documentation
Revise Encryption Service Documentation [ci skip]
2 parents 6a76a77 + df5649d commit 2877e05

1 file changed

Lines changed: 68 additions & 61 deletions

File tree

user_guide_src/source/libraries/encryption.rst

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,41 @@ Encryption Service
33
##################
44

55
.. important:: DO NOT use this or any other *encryption* library for
6-
user password storage! Passwords must be *hashed* instead, and you
7-
should do that via PHP's own `Password Hashing extension
6+
password storage! Passwords must be *hashed* instead, and you
7+
should do that through PHP's `Password Hashing extension
88
<http://php.net/password>`_.
99

1010
The Encryption Service provides two-way symmetric (secret key) data encryption.
11-
The encryption manager will instantiate and/or initialize an
12-
encryption handler to suit your parameters, explained below.
11+
The service will instantiate and/or initialize an
12+
encryption **handler** to suit your parameters as explained below.
1313

14-
The handlers adapt our simple ``EncrypterInterface`` to use an
15-
appropriate PHP cryptographic extension or third party library.
16-
Such extensions may need to be explicitly enabled in your instance of PHP.
14+
Encryption Service handlers must implement CodeIgniter's simple ``EncrypterInterface``.
15+
Using an appropriate PHP cryptographic extension or third-party library may require
16+
additional software is installed on your server and/or might need to be explicitly
17+
enabled in your instance of PHP.
1718

18-
The following extensions are currently supported:
19+
The following PHP extensions are currently supported:
1920

2021
- `OpenSSL <http://php.net/openssl>`_
2122

23+
This is not a full cryptographic solution. If you need more capabilities, for example,
24+
public-key encryption, we suggest you consider direct use of OpenSSL or
25+
one of the other `Cryptography Extensions <https://www.php.net/manual/en/refs.crypto.php>`_.
26+
A more comprehensive package like `Halite <https://github.com/paragonie/halite>`_
27+
(an O-O package built on libsodium) is another possibility.
28+
2229
.. note:: Support for the ``MCrypt`` extension has been dropped, as that has
2330
been deprecated as of PHP 7.2.
2431

25-
This is not a full cryptographic solution. If you need more capabilities,
26-
for instance public key encryption, we suggest you consider using the
27-
above extensions directly, or look into some of the more comprehensive
28-
packages, like:
29-
30-
- `Halite <https://github.com/paragonie/halite>`_, an O-O package built on libsodium, or
31-
- `Sodium_compat <https://github.com/paragonie/sodium_compat>`_, a pure PHP implementation that adds libsodium support to earlier versions of PHP (5.2.4+)
32-
3332
.. contents::
3433
:local:
3534

3635
.. raw:: html
3736

3837
<div class="custom-index container"></div>
3938

39+
.. _usage:
40+
4041
****************************
4142
Using the Encryption Library
4243
****************************
@@ -45,12 +46,12 @@ Like all services in CodeIgniter, it can be loaded via ``Config\Services``::
4546

4647
$encrypter = \Config\Services::encrypter();
4748

48-
Assuming you have set your starting key (see below),
49-
encrypting and decrypting data is simple - pass the appropriate string to the
50-
``encrypt()`` and/or ``decrypt()`` methods::
49+
Assuming you have set your starting key (see :ref:`configuration`),
50+
encrypting and decrypting data is simple - pass the appropriate string to ``encrypt()``
51+
and/or ``decrypt()`` methods::
5152

52-
$plain_text = 'This is a plain-text message!';
53-
$ciphertext = $encrypter->encrypt($plaintext);
53+
$plainText = 'This is a plain-text message!';
54+
$ciphertext = $encrypter->encrypt($plainText);
5455

5556
// Outputs: This is a plain-text message!
5657
echo $encrypter->decrypt($ciphertext);
@@ -61,61 +62,60 @@ You don't need to worry about it.
6162

6263
.. _configuration:
6364

64-
Configuring the library
65+
Configuring the Library
6566
=======================
6667

67-
The example above uses the default configuration settings,
68-
found in ``application/config/Encryption.php``.
68+
The example above uses the configuration settings found in ``app/Config/Encryption.php``.
6969

7070
There are only two settings:
7171

7272
======== ===============================================
7373
Option Possible values (default in parentheses)
7474
======== ===============================================
75-
driver Preferred handler (OpenSSL)
7675
key Encryption key starter
76+
driver Preferred handler (OpenSSL)
7777
======== ===============================================
7878

79-
You can over-ride any of these settings by passing your own ``Config`` object
80-
to the Services::
79+
You can replace the config file's settings by passing a configuration
80+
object of your own to the ``Services`` call. The ``$config`` variable must be
81+
an instance of either the `Config\\Encryption` class or an object
82+
that extends `CodeIgniter\\Config\\BaseConfig`.
83+
::
84+
85+
$config = new Config\Encryption();
86+
$config->key = 'aBigsecret_ofAtleast32Characters';
87+
$config->driver = 'OpenSSL';
8188

8289
$encrypter = \Config\Services::encrypter($config);
8390

84-
Default behavior
85-
================
8691

87-
By default, the Encryption Library will use the OpenSSL handler, with
88-
the AES-256-CTR cipher,
89-
using your configured *key* and SHA512 HMAC authentication.
92+
Default Behavior
93+
================
9094

91-
The *key* you provide is used to derive
92-
two separate keys from your configured one:
93-
one for encryption and one for authentication. This is
94-
done via a technique called `HMAC-based Key Derivation Function
95-
<http://en.wikipedia.org/wiki/HKDF>`_ (HKDF).
95+
By default, the Encryption Library uses the OpenSSL handler. That handler encrypts using
96+
the AES-256-CTR algorithm, your configured *key*, and SHA512 HMAC authentication.
9697

97-
Setting your encryption key
98+
Setting Your Encryption Key
9899
===========================
99100

100-
Your encryption key **must** be as long as the encryption algorithm in use
101-
allows. For AES-256, that's 256 bits or 32 bytes (characters) long.
101+
Your encryption key **must** be as long as the encryption algorithm in use allows.
102+
For AES-256, that's 256 bits or 32 bytes (characters) long.
102103

103-
The key should be as random as possible and it **must not** be a regular
104-
text string, nor the output of a hashing function, etc. In order to create
105-
a proper key, you can use the Encryption library's ``createKey()`` method
104+
The key should be as random as possible, and it **must not** be a regular text string,
105+
nor the output of a hashing function, etc. To create a proper key,
106+
you can use the Encryption library's ``createKey()`` method.
106107
::
107108

108109
// $key will be assigned a 32-byte (256-bit) random key
109110
$key = Encryption::createKey(32);
110111

111-
The key can be either stored in your *application/Config/Encryption.php*, or
112-
you can design your own storage mechanism and pass the key dynamically
113-
when encrypting/decrypting.
112+
The key can be stored in *app/Config/Encryption.php*, or you can design
113+
a storage mechanism of your own and pass the key dynamically when encrypting/decrypting.
114114

115-
To save your key to your *application/Config/Encryption.php*, open the file
115+
To save your key to your *app/Config/Encryption.php*, open the file
116116
and set::
117117

118-
$key = 'YOUR KEY';
118+
public $key = 'YOUR KEY';
119119

120120
Encoding Keys or Results
121121
------------------------
@@ -126,7 +126,7 @@ is hard to deal with (i.e. a copy-paste may damage it), so you may use
126126
a more friendly manner. For example::
127127

128128
// Get a hex-encoded representation of the key:
129-
$encoded = bin2hex($encrypter->createKey(32));
129+
$encoded = bin2hex(Encryption::createKey(32));
130130

131131
// Put the same value in your config with hex2bin(),
132132
// so that it is still passed as binary to the library:
@@ -144,36 +144,43 @@ Encryption Handler Notes
144144
OpenSSL Notes
145145
-------------
146146

147-
OpenSSL has been a standard part of PHP for some time.
147+
The `OpenSSL <http://php.net/openssl>`_ extension has been a standard part of PHP for a long time.
148+
149+
CodeIgniter's OpenSSL handler uses the AES-256-CTR cipher.
148150

149-
The OpenSSL handler uses the AES-256-CTR cipher.
151+
The *key* your configuration provides is used to derive two other keys, one for
152+
encryption and one for authentication. This is achieved by way of a technique known
153+
as an `HMAC-based Key Derivation Function <http://en.wikipedia.org/wiki/HKDF>`_ (HKDF).
150154

151155
Message Length
152156
==============
153157

154-
An encrypted string is usually
155-
longer than the original, plain-text string (depending on the cipher).
158+
An encrypted string is usually longer than the original, plain-text string (depending on the cipher).
156159

157160
This is influenced by the cipher algorithm itself, the initialization vector (IV)
158-
prepended to the
159-
cipher-text and the HMAC authentication message that is also prepended.
161+
prepended to the cipher-text, and the HMAC authentication message that is also prepended.
160162
Furthermore, the encrypted message is also Base64-encoded so that it is safe
161-
for storage and transmission, regardless of a possible character set in use.
163+
for storage and transmission regardless of the character-set in use.
162164

163165
Keep this information in mind when selecting your data storage mechanism.
164166
Cookies, for example, can only hold 4K of information.
165167

166-
Using the Encryption manager directly
168+
Using the Encryption Service Directly
167169
=====================================
168170

169-
Instead of, or in addition to, using the `Services` described
170-
at the beginning of this page, you can use the encryption manager
171-
directly, to create an ``Encrypter`` or to change the settings
172-
of the current one::
171+
Instead of (or in addition to) using ``Services`` as described in :ref:`usage`,
172+
you can create an "Encrypter" directly, or change the settings of an existing instance.
173+
::
173174

175+
// create an Encrypter instance
174176
$encryption = new \Encryption\Encryption();
177+
178+
// reconfigure an instance with different settings
175179
$encrypter = $encryption->initialize($config);
176180

181+
Remember, that ``$config`` must me an instance of either a `Config\Encryption` class
182+
or an object that extends `CodeIgniter\Config\BaseConfig`.
183+
177184

178185
***************
179186
Class Reference

0 commit comments

Comments
 (0)