aboutsummaryrefslogtreecommitdiff
path: root/gnunet/__init__.py
blob: 5bae6f01ce0974ff0ed1bbf46b934055eecbfa77 (plain) (blame)
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
import gnunet.strings as strings


class GNUNetDaemonError(Exception):
    pass


class _Key:
    def __init__(self, arg):
        if isinstance(arg, type(self)):
            self._data = arg._data
        elif isinstance(arg, str):
            self._data = strings.string_to_data(arg)
        else:
            try:
                self._data = bytearray(arg)
            except TypeError as e:
                # HACK: replace the exception message
                e.args = ("'arg' must be a %s, a string or an array "
                          "of bytes. Not a '%s'." %
                          (type(self).__name__, type(arg).__name__),)
                raise

        if len(self._data) * 8 != self.__bits__:
            raise ValueError("'arg' must be a %s bit hash. Got %s bits."
                             % (self.__bits__, len(self._data) * 8))

    def __str__(self):
        return strings.data_to_string(self._data)


class HashCode(_Key):
    __bits__ = 512

    def __repr__(self):
        return "gnunet.HashCode('" + str(self) + "')"