angelovcom.net

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

XChaCha20.php (3370B)


      1 <?php
      2 
      3 if (class_exists('ParagonIE_Sodium_Core_XChaCha20', false)) {
      4     return;
      5 }
      6 
      7 /**
      8  * Class ParagonIE_Sodium_Core_XChaCha20
      9  */
     10 class ParagonIE_Sodium_Core_XChaCha20 extends ParagonIE_Sodium_Core_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_Core_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 int $len
     43      * @param string $nonce
     44      * @param string $key
     45      * @return string
     46      * @throws SodiumException
     47      * @throws TypeError
     48      */
     49     public static function ietfStream($len = 64, $nonce = '', $key = '')
     50     {
     51         if (self::strlen($nonce) !== 24) {
     52             throw new SodiumException('Nonce must be 24 bytes long');
     53         }
     54         return self::encryptBytes(
     55             new ParagonIE_Sodium_Core_ChaCha20_IetfCtx(
     56                 self::hChaCha20(
     57                     self::substr($nonce, 0, 16),
     58                     $key
     59                 ),
     60                 "\x00\x00\x00\x00" . self::substr($nonce, 16, 8)
     61             ),
     62             str_repeat("\x00", $len)
     63         );
     64     }
     65 
     66     /**
     67      * @internal You should not use this directly from another application
     68      *
     69      * @param string $message
     70      * @param string $nonce
     71      * @param string $key
     72      * @param string $ic
     73      * @return string
     74      * @throws SodiumException
     75      * @throws TypeError
     76      */
     77     public static function streamXorIc($message, $nonce = '', $key = '', $ic = '')
     78     {
     79         if (self::strlen($nonce) !== 24) {
     80             throw new SodiumException('Nonce must be 24 bytes long');
     81         }
     82         return self::encryptBytes(
     83             new ParagonIE_Sodium_Core_ChaCha20_Ctx(
     84                 self::hChaCha20(self::substr($nonce, 0, 16), $key),
     85                 self::substr($nonce, 16, 8),
     86                 $ic
     87             ),
     88             $message
     89         );
     90     }
     91 
     92     /**
     93      * @internal You should not use this directly from another application
     94      *
     95      * @param string $message
     96      * @param string $nonce
     97      * @param string $key
     98      * @param string $ic
     99      * @return string
    100      * @throws SodiumException
    101      * @throws TypeError
    102      */
    103     public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '')
    104     {
    105         if (self::strlen($nonce) !== 24) {
    106             throw new SodiumException('Nonce must be 24 bytes long');
    107         }
    108         return self::encryptBytes(
    109             new ParagonIE_Sodium_Core_ChaCha20_IetfCtx(
    110                 self::hChaCha20(self::substr($nonce, 0, 16), $key),
    111                 "\x00\x00\x00\x00" . self::substr($nonce, 16, 8),
    112                 $ic
    113             ),
    114             $message
    115         );
    116     }
    117 }