.. include:: header.rst

`home`__

__ reference.html

.. contents:: Table of contents
  :depth: 2
  :backlinks: none

.. raw:: html

	<a name="hasher"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:class+hasher&labels=documentation&body=Documentation+under+heading+%22class+hasher%22+could+be+improved">report issue</a>]</span>



hasher
------

Declared in "`libtorrent/hasher.hpp`__"


__ include/libtorrent/hasher.hpp

this is a SHA-1 hash class.

You use it by first instantiating it, then call ``update()`` to feed it
with data. i.e. you don't have to keep the entire buffer of which you want to
create the hash in memory. You can feed the `hasher`__ parts of it at a time. When
You have fed the `hasher`__ with all the data, you call ``final()`` and it
will return the sha1-hash of the data.

The constructor that takes a ``char const*`` and an integer will construct the
sha1 context and feed it the data passed in.

If you want to reuse the `hasher`__ object once you have created a hash, you have to
call ``reset()`` to reinitialize it.

The built-in software version of sha1-algorithm was implemented
by Steve Reid and released as public domain.
For more info, see ``src/sha1.cpp``.


__ reference-Utility.html#hasher
__ reference-Utility.html#hasher
__ reference-Utility.html#hasher


.. parsed-literal::
	

	class hasher
	{
	   **hasher** ();
	   explicit **hasher** (span<char const> data);
	   **hasher** (hasher const&);
	   hasher& **operator=** (hasher const&) &;
	   **hasher** (char const\* data, int len);
	   hasher& **update** (char const\* data, int len);
	   hasher& **update** (span<char const> data);
	   sha1_hash **final** ();
	   void **reset** ();
	};

.. raw:: html

	<a name="hasher()"></a>
	<a name="operator=()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:hasher%3A%3A%5Bhasher%28%29+operator%3D%28%29%5D&labels=documentation&body=Documentation+under+heading+%22hasher%3A%3A%5Bhasher%28%29+operator%3D%28%29%5D%22+could+be+improved">report issue</a>]</span>



hasher() operator=()
....................

.. parsed-literal::
	
	explicit **hasher** (span<char const> data);
	**hasher** (hasher const&);
	hasher& **operator=** (hasher const&) &;
	**hasher** (char const\* data, int len);
	

this is the same as default constructing followed by a call to
``update(data, len)``.



.. raw:: html

	<a name="update()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:hasher%3A%3A%5Bupdate%28%29%5D&labels=documentation&body=Documentation+under+heading+%22hasher%3A%3A%5Bupdate%28%29%5D%22+could+be+improved">report issue</a>]</span>



update()
........

.. parsed-literal::
	
	hasher& **update** (char const\* data, int len);
	hasher& **update** (span<char const> data);
	

append the following bytes to what is being hashed



.. raw:: html

	<a name="final()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:hasher%3A%3A%5Bfinal%28%29%5D&labels=documentation&body=Documentation+under+heading+%22hasher%3A%3A%5Bfinal%28%29%5D%22+could+be+improved">report issue</a>]</span>



final()
.......

.. parsed-literal::
	
	sha1_hash **final** ();
	

returns the SHA-1 digest of the buffers previously passed to
`update()`__ and the `hasher`__ constructor.


__ reference-Utility.html#update()
__ reference-Utility.html#hasher

.. raw:: html

	<a name="reset()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:hasher%3A%3A%5Breset%28%29%5D&labels=documentation&body=Documentation+under+heading+%22hasher%3A%3A%5Breset%28%29%5D%22+could+be+improved">report issue</a>]</span>



reset()
.......

.. parsed-literal::
	
	void **reset** ();
	

restore the `hasher`__ state to be as if the `hasher`__ has just been
default constructed.


__ reference-Utility.html#hasher
__ reference-Utility.html#hasher

.. raw:: html

	<a name="hasher256"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:class+hasher256&labels=documentation&body=Documentation+under+heading+%22class+hasher256%22+could+be+improved">report issue</a>]</span>



hasher256
---------

Declared in "`libtorrent/hasher.hpp`__"


__ include/libtorrent/hasher.hpp





.. parsed-literal::
	

	class hasher256
	{
	   **hasher256** ();
	   explicit **hasher256** (span<char const> data);
	   **hasher256** (hasher256 const&);
	   **hasher256** (char const\* data, int len);
	   hasher256& **operator=** (hasher256 const&) &;
	   hasher256& **update** (span<char const> data);
	   hasher256& **update** (char const\* data, int len);
	   sha256_hash **final** ();
	   void **reset** ();
	   **~hasher256** ();
	};

.. raw:: html

	<a name="operator=()"></a>
	<a name="hasher256()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:hasher256%3A%3A%5Boperator%3D%28%29+hasher256%28%29%5D&labels=documentation&body=Documentation+under+heading+%22hasher256%3A%3A%5Boperator%3D%28%29+hasher256%28%29%5D%22+could+be+improved">report issue</a>]</span>



operator=() hasher256()
.......................

.. parsed-literal::
	
	explicit **hasher256** (span<char const> data);
	**hasher256** (hasher256 const&);
	**hasher256** (char const\* data, int len);
	hasher256& **operator=** (hasher256 const&) &;
	

this is the same as default constructing followed by a call to
``update(data, len)``.



.. raw:: html

	<a name="update()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:hasher256%3A%3A%5Bupdate%28%29%5D&labels=documentation&body=Documentation+under+heading+%22hasher256%3A%3A%5Bupdate%28%29%5D%22+could+be+improved">report issue</a>]</span>



update()
........

.. parsed-literal::
	
	hasher256& **update** (span<char const> data);
	hasher256& **update** (char const\* data, int len);
	

append the following bytes to what is being hashed



.. raw:: html

	<a name="final()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:hasher256%3A%3A%5Bfinal%28%29%5D&labels=documentation&body=Documentation+under+heading+%22hasher256%3A%3A%5Bfinal%28%29%5D%22+could+be+improved">report issue</a>]</span>



final()
.......

.. parsed-literal::
	
	sha256_hash **final** ();
	

returns the SHA-1 digest of the buffers previously passed to
`update()`__ and the `hasher`__ constructor.


__ reference-Utility.html#update()
__ reference-Utility.html#hasher

.. raw:: html

	<a name="reset()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:hasher256%3A%3A%5Breset%28%29%5D&labels=documentation&body=Documentation+under+heading+%22hasher256%3A%3A%5Breset%28%29%5D%22+could+be+improved">report issue</a>]</span>



reset()
.......

.. parsed-literal::
	
	void **reset** ();
	

restore the `hasher`__ state to be as if the `hasher`__ has just been
default constructed.


__ reference-Utility.html#hasher
__ reference-Utility.html#hasher

.. raw:: html

	<a name="bitfield"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:class+bitfield&labels=documentation&body=Documentation+under+heading+%22class+bitfield%22+could+be+improved">report issue</a>]</span>



bitfield
--------

Declared in "`libtorrent/bitfield.hpp`__"


__ include/libtorrent/bitfield.hpp

The `bitfield`__ type stores any number of bits as a `bitfield`__
in a heap allocated array.


__ reference-Utility.html#bitfield
__ reference-Utility.html#bitfield


.. parsed-literal::
	

	struct bitfield
	{
	   **bitfield** (char const\* b, int bits);
	   **bitfield** () noexcept = default;
	   **bitfield** (int bits, bool val);
	   explicit **bitfield** (int bits);
	   **bitfield** (bitfield&& rhs) noexcept = default;
	   **bitfield** (bitfield const& rhs);
	   void **assign** (char const\* b, int const bits);
	   bool **operator[]** (int index) const noexcept;
	   bool **get_bit** (int index) const noexcept;
	   void **set_bit** (int index) noexcept;
	   void **clear_bit** (int index) noexcept;
	   bool **all_set** () const noexcept;
	   bool **none_set** () const noexcept;
	   int **size** () const noexcept;
	   int **num_words** () const noexcept;
	   bool **empty** () const noexcept;
	   char\* **data** () noexcept;
	   char const* **data** () const noexcept;
	   void **swap** (bitfield& rhs) noexcept;
	   int **count** () const noexcept;
	   int **find_first_set** () const noexcept;
	   int **find_last_clear** () const noexcept;
	};

.. raw:: html

	<a name="bitfield()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bbitfield%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bbitfield%28%29%5D%22+could+be+improved">report issue</a>]</span>



bitfield()
..........

.. parsed-literal::
	
	**bitfield** (char const\* b, int bits);
	**bitfield** () noexcept = default;
	**bitfield** (int bits, bool val);
	explicit **bitfield** (int bits);
	**bitfield** (bitfield&& rhs) noexcept = default;
	**bitfield** (bitfield const& rhs);
	

constructs a new `bitfield`__. The default constructor creates an empty
`bitfield`__. ``bits`` is the size of the `bitfield`__ (specified in bits).
``val`` is the value to initialize the bits to. If not specified
all bits are initialized to 0.

The constructor taking a pointer ``b`` and ``bits`` copies a `bitfield`__
from the specified buffer, and ``bits`` number of bits (rounded up to
the nearest byte boundary).


__ reference-Utility.html#bitfield
__ reference-Utility.html#bitfield
__ reference-Utility.html#bitfield
__ reference-Utility.html#bitfield

.. raw:: html

	<a name="assign()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bassign%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bassign%28%29%5D%22+could+be+improved">report issue</a>]</span>



assign()
........

.. parsed-literal::
	
	void **assign** (char const\* b, int const bits);
	

copy `bitfield`__ from buffer ``b`` of ``bits`` number of bits, rounded up to
the nearest byte boundary.


__ reference-Utility.html#bitfield

.. raw:: html

	<a name="get_bit()"></a>
	<a name="operator[]()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bget_bit%28%29+operator%5B%5D%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bget_bit%28%29+operator%5B%5D%28%29%5D%22+could+be+improved">report issue</a>]</span>



get_bit() operator[]()
......................

.. parsed-literal::
	
	bool **operator[]** (int index) const noexcept;
	bool **get_bit** (int index) const noexcept;
	

query bit at ``index``. Returns true if bit is 1, otherwise false.



.. raw:: html

	<a name="clear_bit()"></a>
	<a name="set_bit()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bclear_bit%28%29+set_bit%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bclear_bit%28%29+set_bit%28%29%5D%22+could+be+improved">report issue</a>]</span>



clear_bit() set_bit()
.....................

.. parsed-literal::
	
	void **set_bit** (int index) noexcept;
	void **clear_bit** (int index) noexcept;
	

set bit at ``index`` to 0 (clear_bit) or 1 (set_bit).



.. raw:: html

	<a name="all_set()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Ball_set%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Ball_set%28%29%5D%22+could+be+improved">report issue</a>]</span>



all_set()
.........

.. parsed-literal::
	
	bool **all_set** () const noexcept;
	

returns true if all bits in the `bitfield`__ are set


__ reference-Utility.html#bitfield

.. raw:: html

	<a name="none_set()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bnone_set%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bnone_set%28%29%5D%22+could+be+improved">report issue</a>]</span>



none_set()
..........

.. parsed-literal::
	
	bool **none_set** () const noexcept;
	

returns true if no bit in the `bitfield`__ is set


__ reference-Utility.html#bitfield

.. raw:: html

	<a name="size()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bsize%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bsize%28%29%5D%22+could+be+improved">report issue</a>]</span>



size()
......

.. parsed-literal::
	
	int **size** () const noexcept;
	

returns the size of the `bitfield`__ in bits.


__ reference-Utility.html#bitfield

.. raw:: html

	<a name="num_words()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bnum_words%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bnum_words%28%29%5D%22+could+be+improved">report issue</a>]</span>



num_words()
...........

.. parsed-literal::
	
	int **num_words** () const noexcept;
	

returns the number of 32 bit words are needed to represent all bits in
this `bitfield`__.


__ reference-Utility.html#bitfield

.. raw:: html

	<a name="empty()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bempty%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bempty%28%29%5D%22+could+be+improved">report issue</a>]</span>



empty()
.......

.. parsed-literal::
	
	bool **empty** () const noexcept;
	

returns true if the `bitfield`__ has zero size.


__ reference-Utility.html#bitfield

.. raw:: html

	<a name="data()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bdata%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bdata%28%29%5D%22+could+be+improved">report issue</a>]</span>



data()
......

.. parsed-literal::
	
	char\* **data** () noexcept;
	char const* **data** () const noexcept;
	

returns a pointer to the internal buffer of the `bitfield`__, or
``nullptr`` if it's empty.


__ reference-Utility.html#bitfield

.. raw:: html

	<a name="swap()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bswap%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bswap%28%29%5D%22+could+be+improved">report issue</a>]</span>



swap()
......

.. parsed-literal::
	
	void **swap** (bitfield& rhs) noexcept;
	

swaps the bit-fields two variables refer to



.. raw:: html

	<a name="count()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bcount%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bcount%28%29%5D%22+could+be+improved">report issue</a>]</span>



count()
.......

.. parsed-literal::
	
	int **count** () const noexcept;
	

count the number of bits in the `bitfield`__ that are set to 1.


__ reference-Utility.html#bitfield

.. raw:: html

	<a name="find_first_set()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bfind_first_set%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bfind_first_set%28%29%5D%22+could+be+improved">report issue</a>]</span>



find_first_set()
................

.. parsed-literal::
	
	int **find_first_set** () const noexcept;
	

returns the index of the first set bit in the `bitfield`__, i.e. 1 bit.


__ reference-Utility.html#bitfield

.. raw:: html

	<a name="find_last_clear()"></a>

.. raw:: html

	<span class="report-issue">[<a href="http://github.com/arvidn/libtorrent/issues/new?title=docs:bitfield%3A%3A%5Bfind_last_clear%28%29%5D&labels=documentation&body=Documentation+under+heading+%22bitfield%3A%3A%5Bfind_last_clear%28%29%5D%22+could+be+improved">report issue</a>]</span>



find_last_clear()
.................

.. parsed-literal::
	
	int **find_last_clear** () const noexcept;
	

returns the index to the last cleared bit in the `bitfield`__, i.e. 0 bit.


__ reference-Utility.html#bitfield



.. _`BEP 3`: https://www.bittorrent.org/beps/bep_0003.html
.. _`BEP 17`: https://www.bittorrent.org/beps/bep_0017.html
.. _extensions: manual-ref.html#extensions
.. _`BEP 52`: https://www.bittorrent.org/beps/bep_0052.html
.. _`BEP 19`: https://www.bittorrent.org/beps/bep_0019.html
.. _`BEP 38`: https://www.bittorrent.org/beps/bep_0038.html
.. _`rate based choking`: manual-ref.html#rate-based-choking
.. _`BEP 42`: https://www.bittorrent.org/beps/bep_0042.html
