wp/wp-includes/sodium_compat/src/Core/XChaCha20.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    37     }
    37     }
    38 
    38 
    39     /**
    39     /**
    40      * @internal You should not use this directly from another application
    40      * @internal You should not use this directly from another application
    41      *
    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      *
    42      * @param string $message
    69      * @param string $message
    43      * @param string $nonce
    70      * @param string $nonce
    44      * @param string $key
    71      * @param string $key
    45      * @param string $ic
    72      * @param string $ic
    46      * @return string
    73      * @return string
    59                 $ic
    86                 $ic
    60             ),
    87             ),
    61             $message
    88             $message
    62         );
    89         );
    63     }
    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     }
    64 }
   117 }