class Encrypter implements Encrypter, StringEncrypter

Properties

protected string $key

The encryption key.

protected array $previousKeys

The previous / legacy encryption keys.

protected string $cipher

The algorithm used for encryption.

Methods

__construct(string $key, string $cipher = 'aes-128-cbc')

Create a new encrypter instance.

static bool
supported(string $key, string $cipher)

Determine if the given key and cipher combination is valid.

static string
generateKey(string $cipher)

Create a new encryption key for the given cipher.

string
encrypt(mixed $value, bool $serialize = true)

Encrypt the given value.

string
encryptString(string $value)

Encrypt a string without serialization.

mixed
decrypt(string $payload, bool $unserialize = true)

Decrypt the given value.

string
decryptString(string $payload)

Decrypt the given string without unserialization.

string
hash(string $iv, mixed $value, string $key)

Create a MAC for the given value.

array
getJsonPayload(string $payload)

Get the JSON array from the given payload.

bool
validPayload(mixed $payload)

Verify that the encryption payload is valid.

bool
validMac(array $payload)

Determine if the MAC for the given payload is valid.

bool
validMacForKey(array $payload, string $key)

Determine if the MAC is valid for the given payload and key.

void
ensureTagIsValid(string|null $tag)

Ensure the given tag is a valid tag given the selected cipher.

bool
shouldValidateMac()

Determine if we should validate the MAC while decrypting.

static bool
appearsEncrypted(mixed $value)

Determine if the given value appears to be encrypted by this encrypter.

string
getKey()

Get the encryption key that the encrypter is currently using.

array
getAllKeys()

Get the current encryption key and all previous encryption keys.

array
getPreviousKeys()

Get the previous encryption keys.

previousKeys(array $keys)

Set the previous / legacy encryption keys that should be utilized if decryption fails.

Details

at line 46
__construct(string $key, string $cipher = 'aes-128-cbc')

Create a new encrypter instance.

Parameters

string $key
string $cipher

Exceptions

RuntimeException

at line 61
static bool supported(string $key, string $cipher)

Determine if the given key and cipher combination is valid.

Parameters

string $key
string $cipher

Return Value

bool

at line 73
static string generateKey(string $cipher)

Create a new encryption key for the given cipher.

Parameters

string $cipher

Return Value

string

at line 83
string encrypt(mixed $value, bool $serialize = true)

Encrypt the given value.

Parameters

mixed $value
bool $serialize

Return Value

string

Exceptions

EncryptException

at line 121
string encryptString(string $value)

Encrypt a string without serialization.

Parameters

string $value

Return Value

string

Exceptions

EncryptException

at line 131
mixed decrypt(string $payload, bool $unserialize = true)

Decrypt the given value.

Parameters

string $payload
bool $unserialize

Return Value

mixed

Exceptions

DecryptException

at line 198
string decryptString(string $payload)

Decrypt the given string without unserialization.

Parameters

string $payload

Return Value

string

Exceptions

DecryptException

at line 206
protected string hash(string $iv, mixed $value, string $key)

Create a MAC for the given value.

Parameters

string $iv
mixed $value
string $key

Return Value

string

at line 216
protected array getJsonPayload(string $payload)

Get the JSON array from the given payload.

Parameters

string $payload

Return Value

array

Exceptions

DecryptException

at line 233
protected bool validPayload(mixed $payload)

Verify that the encryption payload is valid.

Parameters

mixed $payload

Return Value

bool

at line 255
protected bool validMac(array $payload)

Determine if the MAC for the given payload is valid.

Parameters

array $payload

Return Value

bool

at line 263
protected bool validMacForKey(array $payload, string $key)

Determine if the MAC is valid for the given payload and key.

Parameters

array $payload
string $key

Return Value

bool

at line 274
protected void ensureTagIsValid(string|null $tag)

Ensure the given tag is a valid tag given the selected cipher.

Parameters

string|null $tag

Return Value

void

at line 288
protected bool shouldValidateMac()

Determine if we should validate the MAC while decrypting.

Return Value

bool

at line 296
static bool appearsEncrypted(mixed $value)

Determine if the given value appears to be encrypted by this encrypter.

Parameters

mixed $value

Return Value

bool

at line 317
string getKey()

Get the encryption key that the encrypter is currently using.

Return Value

string

at line 325
array getAllKeys()

Get the current encryption key and all previous encryption keys.

Return Value

array

at line 333
array getPreviousKeys()

Get the previous encryption keys.

Return Value

array

at line 344
Encrypter previousKeys(array $keys)

Set the previous / legacy encryption keys that should be utilized if decryption fails.

Boot-only. Mutates the previous-key list; the encrypter is registered as a container singleton, so per-request use races across coroutines.

Parameters

array $keys

Return Value

Encrypter