Ejemplo n.º 1
0
 def test_public_key_serialization(self):
     # pylint: disable=protected-access
     priv = signer.generate_private_key()
     pub = signer.generate_public_key(priv)
     raw_pub = signer._decode_public_key(pub, 'hex')
     pub2 = signer._encode_public_key(raw_pub, 'hex')
     self.assertTrue(str(pub) == str(pub2))
Ejemplo n.º 2
0
 def test_basic_ops(self):
     msg = 'this is a message'
     priv = signer.generate_private_key()
     pub = signer.generate_public_key(priv)
     sig = signer.sign(msg, priv)
     ver = signer.verify(msg, sig, pub)
     self.assertTrue(ver)
Ejemplo n.º 3
0
 def test_invalid_signature(self):
     msg = "This is a message"
     priv = signer.generate_private_key()
     priv2 = signer.generate_private_key()
     sig = signer.sign(msg, priv)
     pub = signer.generate_public_key(priv2)
     ver = signer.verify(msg, sig, pub)
     self.assertFalse(ver)
Ejemplo n.º 4
0
    def __init__(self, base_url, keyfile):
        self._base_url = base_url
        try:
            with open(keyfile) as fd:
                self._private_key = fd.read().strip()
                fd.close()
        except:
            raise IOError("Failed to read keys.")

        self._public_key = signing.generate_public_key(self._private_key)
Ejemplo n.º 5
0
    def __init__(self, url, keyfile=None):
        self.url = url

        if keyfile is not None:
            try:
                with open(keyfile) as fd:
                    self._private_key = fd.read().strip()
                    fd.close()
            except OSError as err:
                raise IntkeyClientException(
                    'Failed to read private key: {}'.format(str(err)))

            self._public_key = signing.generate_public_key(self._private_key)
Ejemplo n.º 6
0
    def __init__(self, base_url, keyfile=None):

        self._base_url = base_url

        if keyfile is None:
            self._private_key = None
            self._public_key = None
            return

        try:
            with open(keyfile) as fd:
                self._private_key = fd.read().strip()
                fd.close()
        except OSError as err:
            raise XoException('Failed to read private key {}: {}'.format(
                keyfile, str(err)))

        self._public_key = signing.generate_public_key(self._private_key)
Ejemplo n.º 7
0
def do_init(args, config):
    username = config.get('DEFAULT', 'username')
    if args.username is not None:
        username = args.username

    url = config.get('DEFAULT', 'url')
    if args.url is not None:
        url = args.url

    config.set('DEFAULT', 'username', username)
    print("set username: {}".format(username))
    config.set('DEFAULT', 'url', url)
    print("set url: {}".format(url))

    save_config(config)

    priv_filename = config.get('DEFAULT', 'key_file')
    if priv_filename.endswith(".priv"):
        addr_filename = priv_filename[0:-len(".priv")] + ".addr"
    else:
        addr_filename = priv_filename + ".addr"

    if not os.path.exists(priv_filename):
        try:
            if not os.path.exists(os.path.dirname(priv_filename)):
                os.makedirs(os.path.dirname(priv_filename))

            private_key = signing.generate_private_key()
            public_key = signing.generate_public_key(private_key)
            addr = signing.generate_identifier(public_key)

            with open(priv_filename, "w") as priv_fd:
                print("writing file: {}".format(priv_filename))
                priv_fd.write(private_key)
                priv_fd.write("\n")

            with open(addr_filename, "w") as addr_fd:
                print("writing file: {}".format(addr_filename))
                addr_fd.write(addr)
                addr_fd.write("\n")
        except IOError as ioe:
            raise SupplyChainException("IOError: {}".format(str(ioe)))
Ejemplo n.º 8
0
def do_init(args, config):
    username = args.username \
        if args.username else config.get('DEFAULT', 'username')
    url = args.url if args.url else config.get('DEFAULT', 'url')

    config.set('DEFAULT', 'username', username)
    config.set('DEFAULT', 'url', url)

    print("set username: %s" % username)
    print("set url: %s" % url)

    save_config(config)

    priv_filename = config.get('DEFAULT', 'key_file')
    if priv_filename.endswith(".priv"):
        public_key_filename = priv_filename[0:-len(".priv")] + ".pub"
    else:
        public_key_filename = priv_filename + ".pub"

    if not os.path.exists(priv_filename):
        try:
            if not os.path.exists(os.path.dirname(priv_filename)):
                os.makedirs(os.path.dirname(priv_filename))

            private_key = signing.generate_private_key()
            public_key = signing.generate_public_key(private_key)

            with open(priv_filename, "w") as priv_fd:
                print("writing file: {}".format(priv_filename))
                priv_fd.write(private_key)
                priv_fd.write("\n")

            with open(public_key_filename, "w") as public_key_fd:
                print("writing file: {}".format(public_key_filename))
                public_key_fd.write(public_key)
                public_key_fd.write("\n")
        except IOError as ioe:
            raise BattleshipException("IOError: {}".format(str(ioe)))
Ejemplo n.º 9
0
    def __init__(self, base_url, keyfile, wait=None):
        """
        Member variables:
            _base_url
            _private_key
            _public_key
            _transaction_family
            _family_version
            _wait
        """
        self._base_url = base_url

        try:
            with open(keyfile) as fd:
                self._private_key = fd.read().strip()
                fd.close()
        except:
            raise IOError("Failed to read keys.")

        self._public_key = signing.generate_public_key(self._private_key)
        self._transaction_family = "battleship"
        self._family_version = "1.0"
        self._wait = wait
Ejemplo n.º 10
0
    def __init__(self,
                 private_key,
                 payload_encoder=lambda x: x,
                 batcher_public_key=None,
                 dependencies=None,
                 family_name=None,
                 family_version=None,
                 inputs=None,
                 outputs=None):
        self._private_key = private_key
        self._public_key = generate_public_key(private_key,
                                               private_key_format='bytes')

        self.payload_encoder = payload_encoder

        # Set default headers as attributes so they can be modified later
        self.batcher_public_key = (self._public_key
                                   if batcher_public_key is None else
                                   batcher_public_key)
        self.dependencies = dependencies
        self.family_name = family_name
        self.family_version = family_version
        self.inputs = inputs
        self.outputs = outputs
Ejemplo n.º 11
0
 def __init__(self, private_key):
     self._private_key = private_key
     self._public_key = generate_public_key(private_key,
                                            private_key_format='bytes')