You are here

protected static function SocialApi::encryptToken in Social API 8.2

Same name and namespace in other branches
  1. 3.x src/Entity/SocialApi.php \Drupal\social_api\Entity\SocialApi::encryptToken()

Returns the encrypted token.

Parameters

string $token: The plain-text token provided by the provider.

Return value

string The encrypted token.

3 calls to SocialApi::encryptToken()
SocialApi::create in src/Entity/SocialApi.php
Constructs a new entity object, without permanently saving it.
SocialApi::preCreate in src/Entity/SocialApi.php
Changes the values of an entity before it is created.
SocialApi::setToken in src/Entity/SocialApi.php
Sets the encrypted, serialized token.

File

src/Entity/SocialApi.php, line 85

Class

SocialApi
Defines a base class for Social API content entities.

Namespace

Drupal\social_api\Entity

Code

protected static function encryptToken($token) {
  $key = static::getEncryptionKey();

  // Remove the base64 encoding from our key.
  $encryption_key = base64_decode($key);

  // Generates an initialization vector.
  $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));

  // Encrypts the data using AES 256 encryption in CBC mode using our
  // encryption key and initialization vector.
  $encrypted = openssl_encrypt($token, 'aes-256-cbc', $encryption_key, 0, $iv);

  // The $iv is just as important as the key for decrypting, so save it with
  // our encrypted data using a unique separator (::).
  return base64_encode($encrypted . '::' . $iv);
}