Skein hash

A new hash object is created by one of the following three functions:

skein.skein256(init=b'', digest_bits=256, **params)
skein.skein512(init=b'', digest_bits=512, **params)
skein.skein1024(init=b'', digest_bits=1024, **params)

These constructor functions return a corresponding hash object for Skein-256, Skein-512, or Skein-1024 (i.e. 256, 512, or 1024 bits internal state). They optionally take an initial chunk of data to hash (init) and the desired digest length in bits (digest_bits, must be < 2**31).

Further optional (keyword-only) parameters are:

  • key: private key, arbitrary bytes
  • pers: personalization string, arbitrary bytes
  • public_key: public key, arbitrary bytes
  • key_id: key identifier, arbitrary bytes
  • nonce: nonce value, arbitrary bytes
  • tree: tree hashing parameters, a tuple (leaf, fan_out, max_height)

For details about the meaning of these parameters, please consult the Skein specification.

Hash objects

Hash objects have the following methods:

hash.update(message, bits=None)

Hash the given message (of type bytes) into the internal state. (Strings have to be encoded to bytes first.) Repeated calls are equivalent to a single call with the concatenation of all the arguments.

If given, the argument bits has to be <=8*len(message) and specifies how many bits of the message are hashed. Specifically, the first bits//8 full bytes and the bits%8 most significant bits of the following byte are hashed. If omitted, bits defaults to 8*len(message).

Caveat: If the number of hashed bits so far is not a multiple of 8, then bits must be specified with a value of at most 8-hashed_bits%8. Otherwise a ValueError will be raised. This ensures proper byte alignment of subsequent hashing operations.

hash.digest([start, stop])

Return the digest of all data processed so far. Usually, start and stop are omitted, and this is a bytes object of length digest_size.

If start and stop are specified, the result is the same as that of digest()[start:stop], but computed much more efficiently for small slices of large digests. This is useful, e.g., for turning Skein into a stream cipher.


Like digest(), but returning the digest as a string of hexadecimal digits.


Return a clone of the hash object, e.g. to efficiently compute hashes of data sharing a common prefix.

In addition each hash object has the following attributes:

Name of the algorithm, i.e. 'Skein-256', 'Skein-512', or 'Skein-1024'.


Internal state size in bits, i.e. 256, 512, or 1024.


Internal state size in bytes (conforming to hashlib), i.e. 32, 64, or 128.


Output digest length in bits, i.e. the value given to the constructor function (or default).


Digest size in bytes (rounded up).


Number of message bits hashed so far.

Note: Hash objects are picklable, but the pickled data exposes a buffer with up to one block of still unhashed data.

Examples of simple hashing

Make a Skein-512 hash object with default digest length (512 bits) and hash some data:

>>> from skein import skein256, skein512, skein1024
>>> h = skein512()
>>> h.update(b'Nobody inspects')
>>> h.update(b' the spammish repetition')
>>> h.digest()
>>> h.digest_size, h.digest_bits
(64, 512)
>>> h.block_size, h.block_bits
(64, 512)
>>> h.hashed_bits

Similarly for Skein-1024-384:

>>> h = skein1024(b'Nobody inspects the spammish repetition', digest_bits=384)
>>> h.hexdigest()
>>> h.digest_size, h.digest_bits
(48, 384)
>>> h.block_size, h.block_bits
(128, 1024)
>>> h.hashed_bits

Examples of input parameter usage

To generate a message authentication code (MAC), use the key input:

>>> skein256(b'message', key=b'secret').hexdigest()

You may specify a personalization string for personalized hashing:

>>> skein256(b'message', pers=b'20100101').hexdigest()

Similarly, a nonce may be specified for randomized hashing:

>>> skein256(b'message', nonce=b'foobar').hexdigest()

For digital signatures, the public key may be hashed into the digest:

>>> skein256(b'message', public_key=b'mypubkey').hexdigest()

Finally, to use Skein as a key derivation function (KDF), you may specify the master key as key and the key identifier as key_id:

>>> skein256(key=b'mastersecret', key_id=b'email', digest_bits=128).hexdigest()

Tree hashing

Tree hashing uses the same incremental interface as sequential hashing. To hash with a leaf size of 2**L blocks, a fan-out of 2**F children per node and a maximum tree height of M, simply specify a tree parameter of (L, F, M):

>>> h = skein256(tree=(5, 2, 255))
>>> for _ in range(1000):
...     h.update(b'\0' * 10**6)
>>> h.update(b'foobar')
>>> h.hexdigest()

In tree hashing mode, PySkein will use two threads to speed up hashing on multi-core systems. Note that the digests produced in tree hashing differ from those produced in sequential hashing, and also depend on the tree parameter. If you are not restricted by interoperability issues, you can try different leaf sizes to find the value leading to optimal performance on your machine.

Table Of Contents

Previous topic

PySkein 1.0 - The Skein Hash Algorithm for Python

Next topic

Threefish block cipher

This Page