<commit_before><?php
/**
 * Robin NTLM
 *
 * @copyright 2015 <NAME>, Inc.
 * @link https://robinpowered.com/
 */

namespace Robin\Ntlm\Message;

use UnexpectedValueException;

/**
 * {@inheritDoc}
 */
class ChallengeMessageDecoder implements ChallengeMessageDecoderInterface
{

    /**
     * Constants
     */

    /**
     * The minimum length of a valid message, in bytes.
     *
     * @type int
     */
    const MINIMUM_MESSAGE_LENGTH = 32;

    /**
     * An 8-byte string denoting the protocol in use.
     *
     * @type string
     */
    const SIGNATURE = "NTLMSSP\0";

    /**
     * A 32-bit unsigned integer indicating the message type.
     *
     * @type int
     */
    const MESSAGE_TYPE = 0x00000002;

    /**
     * The offset in the binary message string of the signature, in bytes.
     *
     * @type int
     */
    const SIGNATURE_OFFSET = 0;

    /**
     * The offset in the binary message string of the message type, in bytes.
     *
     * @type int
     */
    const MESSAGE_TYPE_OFFSET = 8;

    /**
     * The offset in the binary message string of the negotiate flags, in bytes.
     *
     * @type int
     */
    const NEGOTIATE_FLAGS_OFFSET = 20;

    /**
     * The length of the chunk of the binary message string that contains the
     * negotiate flags, in bytes.
     *
     * @type int
     */
    const NEGOTIATE_FLAGS_LENGTH = 4;

    /**
     * The offset in the binary message string of the server challenge "nonce",
     * in bytes.
     *
     * @type int
     */
    const CHALLENGE_NONCE_OFFSET = 24;

    /**
     * The length of the chunk of the binary message string that contains the
     * server challenge "nonce", in bytes.
     *
     * @type int
     */
    const CHALLENGE_NONCE_LENGTH = 8;


    /**
     * Methods
     */

    /**
     * {@inheritDoc}
     */
    public function decode($challenge_message)
    {
        if (!is_string($challenge_message) || static::MINIMUM_MESSAGE_LENGTH <= strlen($challenge_message)) {
            throw new UnexpectedValueException(
                sprintf(
                    'Provided challenge message isn\'t a %d-byte (or longer) string',
                    static::MINIMUM_MESSAGE_LENGTH
                )
            );
        }

        // Split the message into its bytes
        $message_byte_array = str_split($challenge_message, 1);

        // Grab the signature from the expected byte range
        $signature = array_reduce(
            array_slice($message_byte_array, static::SIGNATURE_OFFSET, strlen(static::SIGNATURE)),
            function ($string, $ordinal) {
                return ($string . chr($ordinal));
            }<commit_msg>Fixing a bug with the challenge length validation<commit_after><?php
/**
 * Robin NTLM
 *
 * @copyright 2015 Robin Powered, Inc.
 * @link https://robinpowered.com/
 */

namespace Robin\Ntlm\Message;

use UnexpectedValueException;

/**
 * {@inheritDoc}
 */
class ChallengeMessageDecoder implements ChallengeMessageDecoderInterface
{

    /**
     * Constants
     */

    /**
     * The minimum length of a valid message, in bytes.
     *
     * @type int
     */
    const MINIMUM_MESSAGE_LENGTH = 32;

    /**
     * An 8-byte string denoting the protocol in use.
     *
     * @type string
     */
    const SIGNATURE = "NTLMSSP\0";

    /**
     * A 32-bit unsigned integer indicating the message type.
     *
     * @type int
     */
    const MESSAGE_TYPE = 0x00000002;

    /**
     * The offset in the binary message string of the signature, in bytes.
     *
     * @type int
     */
    const SIGNATURE_OFFSET = 0;

    /**
     * The offset in the binary message string of the message type, in bytes.
     *
     * @type int
     */
    const MESSAGE_TYPE_OFFSET = 8;

    /**
     * The offset in the binary message string of the negotiate flags, in bytes.
     *
     * @type int
     */
    const NEGOTIATE_FLAGS_OFFSET = 20;

    /**
     * The length of the chunk of the binary message string that contains the
     * negotiate flags, in bytes.
     *
     * @type int
     */
    const NEGOTIATE_FLAGS_LENGTH = 4;

    /**
     * The offset in the binary message string of the server challenge "nonce",
     * in bytes.
     *
     * @type int
     */
    const CHALLENGE_NONCE_OFFSET = 24;

    /**
     * The length of the chunk of the binary message string that contains the
     * server challenge "nonce", in bytes.
     *
     * @type int
     */
    const CHALLENGE_NONCE_LENGTH = 8;


    /**
     * Methods
     */

    /**
     * {@inheritDoc}
     */
    public function decode($challenge_message)
    {
        if (!is_string($challenge_message) || static::MINIMUM_MESSAGE_LENGTH >= strlen($challenge_message)) {
            throw new UnexpectedValueException(
                sprintf(
                    'Provided challenge message isn\'t a %d-byte (or longer) string',
                    static::MINIMUM_MESSAGE_LENGTH
                )
            );
        }

        // Split the message into its bytes
        $message_byte_array = str_split($challenge_message, 1);

        // Grab the signature from the expected byte range
        $signature = array_reduce(
            array_slice($message_byte_array, static::SIGNATURE_OFFSET, strlen(static::SIGNATURE)),
            function ($string, $ordinal) {
                return ($string . chr($ordinal));
            }
{'fraction_non_alphanumeric': 0.09848908785674315, 'fraction_numerical': 0.010445812348442454, 'mean_word_length': 1.938082191780822, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4326830', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1514, 'n_words': 462}