# Encryption

## Settings

You can add two fields to specify a specific key and salt for encryption:

```
NAN_DEFAULT_KEY=
NAN_DEFAULT_SALT=
```

If not set it will use the `APP_KEY` as key and change it a bit for a salt.

{% hint style="warning" %}
Changing the key and/or salt will make previously encrypted values unreadable for the decrypt function!!
{% endhint %}

Start of by importing the controller where you are using it

```php
use NaN\NanGeneralController\Controllers\EncryptionController;
```

## Encryption

A function for encrypting data for storage.

Example:

```php
$phone_number = '+31612345678';
$EncryptionController = new EncryptionController();
$encrypted = $EncryptionController->encrypt($phone_number);
```

Tip: Set the debug paramater to true to see what the validation fails on

```php
$ValidationController->validateEmail($email, true);

// invalid_mx_or_a_record
// invalid_domain
// invalid_pregmatch_validate
// invalid_filter_validate
```

## Descrypt

A way to decrypt the encrypted values. This only works on values encrypted with the same key and salt as you are trying to decrypt.

Example:

```php
$number_value = '9H92n....eExMQT0FUDZq=';
$ValidationController = new ValidationController();
$phone_number = $ValidationController->decrypt($number_value, true);    
```

## Dummy

Get a dummy example of the decrypted value for displaying purposes. It only shows the first 3 chars and adds a '●'-char for every other character in the string.

```php
$api_key = 'Hn929....MUDZqQT0eExF=';
$ValidationController = new ValidationController();
$api_key = $ValidationController->decrypt($api_key, true);    
$api_key_to_show = $ValidationController->getDummy($api_key, true);
```

## Contains dummy character

Function to check if a string contains dummy characters. This is so you can prevent the saving of a submitted dummy-ised value

```php
$submitted_value = 'jes●●●●●●●●●●●●●●●●●●●';
$EncryptionController = new EncryptionController();
if (!$EncryptionController->containsDummyCharacter($submitted_value)){
    // Do stuff
}
```

## Decrypt object values

Function to decrypt a encrypted object. For example when getting a entry from a database, you can decrypt the object at once

```php
$user = User::find(1);
$EncryptionController = new EncryptionController();
        $user = $EncryptionController->decryptObjectValues($user);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://composer.notanumber.digital/laravel-general-package/encryption.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
