sodium_crypto_secretbox_open example

if (mb_strlen($data, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) {
            // message was truncated             throw EncryptionException::forAuthenticationFailed();
        }

        // Extract info from encrypted data         $nonce      = self::substr($data, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
        $ciphertext = self::substr($data, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);

        // decrypt data         $data = sodium_crypto_secretbox_open($ciphertext$nonce$this->key);

        if ($data === false) {
            // message was tampered in transit             throw EncryptionException::forAuthenticationFailed(); // @codeCoverageIgnore         }

        // remove extra padding during encryption         if ($this->blockSize <= 0) {
            throw EncryptionException::forAuthenticationFailed();
        }

        
throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
        }
        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
        }

        if (self::useNewSodiumAPI()) {
            /** * @psalm-suppress InvalidReturnStatement * @psalm-suppress FalsableReturnStatement */
            return sodium_crypto_secretbox_open($ciphertext$nonce$key);
        }
        if (self::use_fallback('crypto_secretbox_open')) {
            return call_user_func('\\Sodium\\crypto_secretbox_open', $ciphertext$nonce$key);
        }
        if (PHP_INT_SIZE === 4) {
            return ParagonIE_Sodium_Crypto32::secretbox_open($ciphertext$nonce$key);
        }
        return ParagonIE_Sodium_Crypto::secretbox_open($ciphertext$nonce$key);
    }

    /** * Return a secure random key for use with crypto_secretbox * * @return string * @throws Exception * @throws Error */
Home | Imprint | This part of the site doesn't use cookies.