gostcrypto icon indicating copy to clipboard operation
gostcrypto copied to clipboard

Баг в реализации Streebog

Open cnt0 opened this issue 3 months ago • 0 comments

Добрый день.

    data = bytearray.fromhex("a" * 512)
    c1 = gosthash.new("streebog256")
    c1.update(data)
    print(c1.hexdigest())

    c2 = gosthash.new("streebog256")
    for i in range(0, len(data), 32):
        c2.update(data[i:i+32])
    print(c2.hexdigest())

печатают разные значения. Судя по замечанию "In this case the ‘buffer_size’ value must be a multiple of the ‘block_size’ value" это известная проблема.

Предлагаю либо явно это документировать в функции update, либо исправить:

    def update(self, data: bytearray) -> None:
        """
        Update the hash object with the bytes-like object.

        Args:
            data: The string from which to get the hash. Repeated calls are
              equivalent to a single call with the concatenation of all the
              arguments: 'm.update(a)'; 'm.update(b)' is equivalent to
              'm.update(a+b)'.

        Raises:
            GOSTHashError('GOSTHashError: invalid data value'): In case where
              the data is not byte object.
        """
        if not isinstance(data, (bytes, bytearray)):
            raise GOSTHashError('GOSTHashError: invalid data value')
        data = self._buff + bytearray(data)
        self._buff = bytearray()
        self._num_block = len(data) // _BLOCK_SIZE
        for i in range(0, self._num_block * _BLOCK_SIZE, _BLOCK_SIZE):
            block = data[i:i + _BLOCK_SIZE]
            self._hash_h = self._hash_g(self._hash_h, self._hash_n, block)
            self._hash_n = self._hash_add_512(self._hash_n, _V_512)
            self._hash_sigma = self._hash_add_512(self._hash_sigma, block)
        if len(data) % _BLOCK_SIZE:
            self._buff = data[-(len(data) % _BLOCK_SIZE):]

Толком не проверял, но на моих тестах проходит.

В случае исправления желательно выпустить новый релиз.

cnt0 avatar Sep 22 '25 13:09 cnt0