angelovcom.net

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

State.php (3624B)


      1 <?php
      2 
      3 /**
      4  * Class ParagonIE_Sodium_Core_SecretStream_State
      5  */
      6 class ParagonIE_Sodium_Core_SecretStream_State
      7 {
      8     /** @var string $key */
      9     protected $key;
     10 
     11     /** @var int $counter */
     12     protected $counter;
     13 
     14     /** @var string $nonce */
     15     protected $nonce;
     16 
     17     /** @var string $_pad */
     18     protected $_pad;
     19 
     20     /**
     21      * ParagonIE_Sodium_Core_SecretStream_State constructor.
     22      * @param string $key
     23      * @param string|null $nonce
     24      */
     25     public function __construct($key, $nonce = null)
     26     {
     27         $this->key = $key;
     28         $this->counter = 1;
     29         if (is_null($nonce)) {
     30             $nonce = str_repeat("\0", 12);
     31         }
     32         $this->nonce = str_pad($nonce, 12, "\0", STR_PAD_RIGHT);;
     33         $this->_pad = str_repeat("\0", 4);
     34     }
     35 
     36     /**
     37      * @return self
     38      */
     39     public function counterReset()
     40     {
     41         $this->counter = 1;
     42         $this->_pad = str_repeat("\0", 4);
     43         return $this;
     44     }
     45 
     46     /**
     47      * @return string
     48      */
     49     public function getKey()
     50     {
     51         return $this->key;
     52     }
     53 
     54     /**
     55      * @return string
     56      */
     57     public function getCounter()
     58     {
     59         return ParagonIE_Sodium_Core_Util::store32_le($this->counter);
     60     }
     61 
     62     /**
     63      * @return string
     64      */
     65     public function getNonce()
     66     {
     67         if (!is_string($this->nonce)) {
     68             $this->nonce = str_repeat("\0", 12);
     69         }
     70         if (ParagonIE_Sodium_Core_Util::strlen($this->nonce) !== 12) {
     71             $this->nonce = str_pad($this->nonce, 12, "\0", STR_PAD_RIGHT);
     72         }
     73         return $this->nonce;
     74     }
     75 
     76     /**
     77      * @return string
     78      */
     79     public function getCombinedNonce()
     80     {
     81         return $this->getCounter() .
     82             ParagonIE_Sodium_Core_Util::substr($this->getNonce(), 0, 8);
     83     }
     84 
     85     /**
     86      * @return self
     87      */
     88     public function incrementCounter()
     89     {
     90         ++$this->counter;
     91         return $this;
     92     }
     93 
     94     /**
     95      * @return bool
     96      */
     97     public function needsRekey()
     98     {
     99         return ($this->counter & 0xffff) === 0;
    100     }
    101 
    102     /**
    103      * @param string $newKeyAndNonce
    104      * @return self
    105      */
    106     public function rekey($newKeyAndNonce)
    107     {
    108         $this->key = ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 0, 32);
    109         $this->nonce = str_pad(
    110             ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 32),
    111             12,
    112             "\0",
    113             STR_PAD_RIGHT
    114         );
    115         return $this;
    116     }
    117 
    118     /**
    119      * @param string $str
    120      * @return self
    121      */
    122     public function xorNonce($str)
    123     {
    124         $this->nonce = ParagonIE_Sodium_Core_Util::xorStrings(
    125             $this->getNonce(),
    126             str_pad(
    127                 ParagonIE_Sodium_Core_Util::substr($str, 0, 8),
    128                 12,
    129                 "\0",
    130                 STR_PAD_RIGHT
    131             )
    132         );
    133         return $this;
    134     }
    135 
    136     /**
    137      * @param string $string
    138      * @return self
    139      */
    140     public static function fromString($string)
    141     {
    142         $state = new ParagonIE_Sodium_Core_SecretStream_State(
    143             ParagonIE_Sodium_Core_Util::substr($string, 0, 32)
    144         );
    145         $state->counter = ParagonIE_Sodium_Core_Util::load_4(
    146             ParagonIE_Sodium_Core_Util::substr($string, 32, 4)
    147         );
    148         $state->nonce = ParagonIE_Sodium_Core_Util::substr($string, 36, 12);
    149         $state->_pad = ParagonIE_Sodium_Core_Util::substr($string, 48, 8);
    150         return $state;
    151     }
    152 
    153     /**
    154      * @return string
    155      */
    156     public function toString()
    157     {
    158         return $this->key .
    159             $this->getCounter() .
    160             $this->nonce .
    161             $this->_pad;
    162     }
    163 }