PHP Classes
elePHPant
Icontem

File: src/KeyProvider/FileProvider.php

Recommend this page to a friend!
  Classes of Scott Arciszewski  >  Cipher Sweet  >  src/KeyProvider/FileProvider.php  >  Download  
File: src/KeyProvider/FileProvider.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: 8 months ago
Size: 1,585 bytes
 

Contents

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

use
ParagonIE\CipherSweet\Backend\Key\SymmetricKey;
use
ParagonIE\CipherSweet\Contract\BackendInterface;
use
ParagonIE\CipherSweet\Contract\KeyProviderInterface;
use
ParagonIE\CipherSweet\Exception\KeyProviderException;

/**
 * Class FileProvider
 * @package ParagonIE\CipherSweet\KeyProvider
 */
class FileProvider implements KeyProviderInterface
{
   
/**
     * @var BackendInterface $backend
     */
   
protected $backend;

   
/**
     * @var string|null $symmetricKeyPath
     */
   
protected $symmetricKeyPath = null;

   
/**
     * FileProvider constructor.
     *
     * @param BackendInterface $backend
     * @param string|null $symmetricKeyPath
     */
   
public function __construct(
       
BackendInterface $backend,
       
$symmetricKeyPath = null
   
) {
       
$this->backend = $backend;
       
$this->symmetricKeyPath = $symmetricKeyPath;
    }

   
/**
     * @return BackendInterface
     */
   
public function getBackend()
    {
        return
$this->backend;
    }

   
/**
     * @return SymmetricKey
     * @throws KeyProviderException
     */
   
public function getSymmetricKey()
    {
        if (\
is_null($this->symmetricKeyPath)) {
            throw new
KeyProviderException('Symmetric key path was not provided');
        }
       
$contents = \file_get_contents($this->symmetricKeyPath);
        if (!\
is_string($contents)) {
            throw new
KeyProviderException('Could not read symmetric key from file.');
        }

        return new
SymmetricKey($this->backend, $contents);

    }
}