From 19985dbb8c0aa66dc4bf7905abc1148de909097d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Tue, 11 Jan 2022 12:35:47 +0100 Subject: prvi-commit --- .../sodium_compat/src/Core/SecretStream/State.php | 163 +++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 vendor/paragonie/sodium_compat/src/Core/SecretStream/State.php (limited to 'vendor/paragonie/sodium_compat/src/Core/SecretStream/State.php') diff --git a/vendor/paragonie/sodium_compat/src/Core/SecretStream/State.php b/vendor/paragonie/sodium_compat/src/Core/SecretStream/State.php new file mode 100644 index 0000000..2412f65 --- /dev/null +++ b/vendor/paragonie/sodium_compat/src/Core/SecretStream/State.php @@ -0,0 +1,163 @@ +key = $key; + $this->counter = 1; + if (is_null($nonce)) { + $nonce = str_repeat("\0", 12); + } + $this->nonce = str_pad($nonce, 12, "\0", STR_PAD_RIGHT);; + $this->_pad = str_repeat("\0", 4); + } + + /** + * @return self + */ + public function counterReset() + { + $this->counter = 1; + $this->_pad = str_repeat("\0", 4); + return $this; + } + + /** + * @return string + */ + public function getKey() + { + return $this->key; + } + + /** + * @return string + */ + public function getCounter() + { + return ParagonIE_Sodium_Core_Util::store32_le($this->counter); + } + + /** + * @return string + */ + public function getNonce() + { + if (!is_string($this->nonce)) { + $this->nonce = str_repeat("\0", 12); + } + if (ParagonIE_Sodium_Core_Util::strlen($this->nonce) !== 12) { + $this->nonce = str_pad($this->nonce, 12, "\0", STR_PAD_RIGHT); + } + return $this->nonce; + } + + /** + * @return string + */ + public function getCombinedNonce() + { + return $this->getCounter() . + ParagonIE_Sodium_Core_Util::substr($this->getNonce(), 0, 8); + } + + /** + * @return self + */ + public function incrementCounter() + { + ++$this->counter; + return $this; + } + + /** + * @return bool + */ + public function needsRekey() + { + return ($this->counter & 0xffff) === 0; + } + + /** + * @param string $newKeyAndNonce + * @return self + */ + public function rekey($newKeyAndNonce) + { + $this->key = ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 0, 32); + $this->nonce = str_pad( + ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 32), + 12, + "\0", + STR_PAD_RIGHT + ); + return $this; + } + + /** + * @param string $str + * @return self + */ + public function xorNonce($str) + { + $this->nonce = ParagonIE_Sodium_Core_Util::xorStrings( + $this->getNonce(), + str_pad( + ParagonIE_Sodium_Core_Util::substr($str, 0, 8), + 12, + "\0", + STR_PAD_RIGHT + ) + ); + return $this; + } + + /** + * @param string $string + * @return self + */ + public static function fromString($string) + { + $state = new ParagonIE_Sodium_Core_SecretStream_State( + ParagonIE_Sodium_Core_Util::substr($string, 0, 32) + ); + $state->counter = ParagonIE_Sodium_Core_Util::load_4( + ParagonIE_Sodium_Core_Util::substr($string, 32, 4) + ); + $state->nonce = ParagonIE_Sodium_Core_Util::substr($string, 36, 12); + $state->_pad = ParagonIE_Sodium_Core_Util::substr($string, 48, 8); + return $state; + } + + /** + * @return string + */ + public function toString() + { + return $this->key . + $this->getCounter() . + $this->nonce . + $this->_pad; + } +} -- cgit v1.2.3