PHP Classes

File: src/Backend/Key/SymmetricKey.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   Cipher Sweet   src/Backend/Key/SymmetricKey.php   Download  
File: src/Backend/Key/SymmetricKey.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Cipher Sweet
Encrypt data in away that can be searched
Author: By
Last change:
Date: 5 years ago
Size: 1,552 bytes
 

Contents

Class file image Download
<?php
namespace ParagonIE\CipherSweet\Backend\Key;

use
ParagonIE\CipherSweet\Contract\BackendInterface;
use
ParagonIE\ConstantTime\Binary;

/**
 * Class SymmetricKey
 * @package ParagonIE\CipherSweet\Backend\Key
 */
class SymmetricKey
{
   
/**
     * @var BackendInterface $Backend
     */
   
private $backend;

   
/**
     * @var string $keyMaterial
     */
   
private $keyMaterial;

   
/**
     * SymmetricKey constructor.
     *
     * @param BackendInterface $backend
     * @param string $rawKeyMaterial
     */
   
public function __construct(BackendInterface $backend, $rawKeyMaterial)
    {
       
/** @psalm-suppress RedundantConditionGivenDocblockType */
       
if (!\is_string($rawKeyMaterial)) {
            throw new \
TypeError('String expected');
        }
       
$this->backend = $backend;
       
$this->keyMaterial = $rawKeyMaterial;
    }

   
/**
     * Attempt to wipe memory.
     */
   
public function __destruct()
    {
        if (\
extension_loaded('sodium')) {
            \
sodium_memzero($this->keyMaterial);
        } elseif (\
extension_loaded('libsodium')) {
            \
Sodium\memzero($this->keyMaterial);
        } else {
           
// Worst-case scenario: Best-ditch effort to wipe memory
           
$m = \str_repeat("\xff", (int) Binary::safeStrlen($this->keyMaterial));
           
$this->keyMaterial ^= ($this->keyMaterial ^ $m);
            unset(
$this->keyMaterial);
        }
    }

   
/**
     * @return string
     */
   
public function getRawKey()
    {
        return
$this->keyMaterial;
    }
}