File: quickstart.rst

package info (click to toggle)
python-eth-hash 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 308 kB
  • sloc: python: 659; makefile: 233
file content (82 lines) | stat: -rw-r--r-- 2,332 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Quickstart
============

Choose a hashing backend
---------------------------

If you're not sure, choose "pycryptodome" because it supports pypy3.

You can find a full list of each currently supported backend in :mod:`eth_hash.backends`.

Install
----------

Put the backend you would like to use in brackets during install, like:

.. code-block:: shell

  python -m pip install "eth-hash[pycryptodome]"

Compute a Keccak256 Hash
-----------------------------

.. doctest::

  >>> from eth_hash.auto import keccak
  >>> keccak(b'')
  b"\xc5\xd2F\x01\x86\xf7#<\x92~}\xb2\xdc\xc7\x03\xc0\xe5\x00\xb6S\xca\x82';{\xfa\xd8\x04]\x85\xa4p"


You may also compute hashes incrementally

.. doctest::

  >>> from eth_hash.auto import keccak
  >>> preimage = keccak.new(b'part-a')
  >>> preimage.update(b'part-b')
  >>> preimage.digest()
  b'6\x91l\xdd50\xd6[\x7f\xf9B\xff\xc9SW\x98\xc3\xaal\xd9\xde\xdd6I\xb7\x91\x9e\xf4`pl\x08'

The preimage object returned may be copied as well.

.. doctest::

  >>> from eth_hash.auto import keccak
  >>> preimage = keccak.new(b'part-a')
  >>> preimage_copy = preimage.copy()
  >>> preimage.update(b'part-b')
  >>> preimage.digest()
  b'6\x91l\xdd50\xd6[\x7f\xf9B\xff\xc9SW\x98\xc3\xaal\xd9\xde\xdd6I\xb7\x91\x9e\xf4`pl\x08'
  >>> preimage_copy.update(b'part-c')
  >>> preimage_copy.digest()
  b'\xffcy45\xea\xdd\xdf\x8e(\x1c\xfcF\xf3\xd4\xa1S\x0f\xdf\xd8\x01!\xb2(\xe1\xc7\xc6\xa3\x08\xc3\n\x0b'


Select one of many installed backends
---------------------------------------

If you have several backends installed, you may want to
explicitly specify which one to load. You can specify
in an environment variable, or at runtime.

Specify backend by environment variable
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: shell

  $ ETH_HASH_BACKEND="pysha3" python
  >>> from eth_hash.auto import keccak
  # This runs with the pysha3 backend
  >>> keccak(b'')
  b"\xc5\xd2F\x01\x86\xf7#<\x92~}\xb2\xdc\xc7\x03\xc0\xe5\x00\xb6S\xca\x82';{\xfa\xd8\x04]\x85\xa4p"

Specify backend at runtime
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: python

  >>> from eth_hash.backends import pysha3
  >>> from eth_hash import Keccak256
  >>> keccak = Keccak256(pysha3)
  >>> keccak(b'')
  b"\xc5\xd2F\x01\x86\xf7#<\x92~}\xb2\xdc\xc7\x03\xc0\xe5\x00\xb6S\xca\x82';{\xfa\xd8\x04]\x85\xa4p"