balmet.com

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

XChaCha20.php (1775B)


      1 <?php
      2 
      3 if (class_exists('ParagonIE_Sodium_Core32_XChaCha20', false)) {
      4     return;
      5 }
      6 
      7 /**
      8  * Class ParagonIE_Sodium_Core32_XChaCha20
      9  */
     10 class ParagonIE_Sodium_Core32_XChaCha20 extends ParagonIE_Sodium_Core32_HChaCha20
     11 {
     12     /**
     13      * @internal You should not use this directly from another application
     14      *
     15      * @param int $len
     16      * @param string $nonce
     17      * @param string $key
     18      * @return string
     19      * @throws SodiumException
     20      * @throws TypeError
     21      */
     22     public static function stream($len = 64, $nonce = '', $key = '')
     23     {
     24         if (self::strlen($nonce) !== 24) {
     25             throw new SodiumException('Nonce must be 24 bytes long');
     26         }
     27         return self::encryptBytes(
     28             new ParagonIE_Sodium_Core32_ChaCha20_Ctx(
     29                 self::hChaCha20(
     30                     self::substr($nonce, 0, 16),
     31                     $key
     32                 ),
     33                 self::substr($nonce, 16, 8)
     34             ),
     35             str_repeat("\x00", $len)
     36         );
     37     }
     38 
     39     /**
     40      * @internal You should not use this directly from another application
     41      *
     42      * @param string $message
     43      * @param string $nonce
     44      * @param string $key
     45      * @param string $ic
     46      * @return string
     47      * @throws SodiumException
     48      * @throws TypeError
     49      */
     50     public static function streamXorIc($message, $nonce = '', $key = '', $ic = '')
     51     {
     52         if (self::strlen($nonce) !== 24) {
     53             throw new SodiumException('Nonce must be 24 bytes long');
     54         }
     55         return self::encryptBytes(
     56             new ParagonIE_Sodium_Core32_ChaCha20_Ctx(
     57                 self::hChaCha20(self::substr($nonce, 0, 16), $key),
     58                 self::substr($nonce, 16, 8),
     59                 $ic
     60             ),
     61             $message
     62         );
     63     }
     64 }