PHP Classes

File: src/Discretion/Data/HiddenString.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   Discretion   src/Discretion/Data/HiddenString.php   Download  
File: src/Discretion/Data/HiddenString.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Discretion
Show contact forms and deliver encrypted mail
Author: By
Last change:
Date: 2 years ago
Size: 1,725 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);
namespace
ParagonIE\Discretion\Data;

/**
 * Class HiddenString
 * @package ParagonIE\Discretion\Data
 */
final class HiddenString
{
   
/**
     * @var string
     */
   
protected $internalStringValue = '';

   
/**
     * HiddenString constructor.
     *
     * @param string $value
     */
   
public function __construct(string $value)
    {
       
$this->internalStringValue = '' . $value;
    }

   
/**
     * @param HiddenString $other
     * @return bool
     */
   
public function equals(HiddenString $other)
    {
        return \
hash_equals(
           
$this->getString(),
           
$other->getString()
        );
    }

   
/**
     * Hide its internal state from var_dump()
     *
     * @return array
     */
   
public function __debugInfo()
    {
        return [];
    }

   
/**
     * Wipe it from memory after it's been used.
     */
   
public function __destruct()
    {
        try {
            \
sodium_memzero($this->internalStringValue);
        } catch (\
Throwable $ex) {
           
$this->internalStringValue ^= $this->internalStringValue;
            unset(
$this->internalStringValue);
        }
    }

   
/**
     * Explicit invocation -- get the raw string value
     *
     * @return string
     */
   
public function getString(): string
   
{
        return
'' . $this->internalStringValue;
    }

   
/**
     * Returns a copy of the string's internal value, which should be zeroed.
     * Optionally, it can return an empty string.
     *
     * @return string
     */
   
public function __toString(): string
   
{
        return
'';
    }

   
/**
     * @return array
     */
   
public function __sleep(): array
    {
        return [];
    }
}