Computing Skein hashes — PySkein v0.5.2 documentation

### Navigation

- index
- next |
- previous |
- PySkein v0.5.2 documentation »

# Computing Skein hashes

The interface for hashing with Skein is conveniently similar to the `hashlib` module of Python’s standard library. A new hash object is created by one of the following three functions:

`skein.` `skein256`(init=b'', digest_bits=256, mac=b'', pers=b'', nonce=b'')

`skein.` `skein512`(init=b'', digest_bits=512, mac=b'', pers=b'', nonce=b'')

`skein.` `skein1024`(init=b'', digest_bits=1024, mac=b'', pers=b'', nonce=b'')

  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 and the desired digest length in bits (must be a multiple of 8).

  Further optional parameters are a message authentication code (MAC), a personalization string and a nonce value, which are all included in subsequent hash computations according to the Skein specification.

A hash object has the following methods:

`update`(data)
  Hash the given chunk of (bytes) data into the internal state. (String data has to be encoded to bytes first.) Repeated calls are equivalent to a single call with the concatenation of all the arguments.

`digest`()
  Return the digest of all data processed so far. This is a bytes object of length `digest_size`.

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

`copy`()
  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`
  Name of the algorithm, i.e. `'Skein-256'`, `'Skein-512'`, or `'Skein-1024'`.

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

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

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

`digest_size`
  Digest size in bytes.

## Examples

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()
    b'\xad\xd2\xc32\x8a \xa6\xfc\xeb\x89i\xcbr\x0e\xa3\x83~>\x9e\x1e%\xfb\x17\x8f\xa8h8\xbb\x8f\xac\x8f\xf6\x84\xbb~\x02\x0cZ\xdbK\x89:b\x84\x9dWq\x0c\x9f\x8c\xc65\xc6k>HZ\x9a\xbd$\x9f39\x1e'
    >>> h.digest_size, h.digest_bits
    (64, 512)
    >>> h.block_size, h.block_bits
    (64, 512)

Similarly for Skein-1024-384:

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

Hashing with or without message authentication code (MAC):

    >>> skein256(b'foo', mac=b'bar').hexdigest()
    'a86f4593da5c734a3f2de66e8e418c2d3c91cc4e6c6aa69a171aba6dab02711c'
    >>> skein256(b'foo').hexdigest()
    '8a62e0aa350e48167888bce63cbe19dbe6f7050a741b9aea9a71fcadae3135bd'

### Navigation

- index
- next |
- previous |
- PySkein v0.5.2 documentation »