Python hashlib.sha3_256() Examples

The following are 30 code examples of hashlib.sha3_256(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module hashlib , or try the search function .
Example #1
Source File: utils.py    From indy-plenum with Apache License 2.0 6 votes vote down vote up
def sha3(seed):
    return sha3_256(to_string(seed))


# assert encode_hex(sha3(b'')) == b'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'


#def normalize_address(x, allow_blank=False):
#    if is_numeric(x):
#        return int_to_addr(x)
#    if allow_blank and x in {'', b''}:
#        return b''
#    if len(x) in (42, 50) and x[:2] in {'0x', b'0x'}:
#        x = x[2:]
#    if len(x) in (40, 48):
#        x = decode_hex(x)
#    if len(x) == 24:
#        assert len(x) == 24 and sha3(x[:20])[:4] == x[-4:]
#        x = x[:20]
#    if len(x) != 20:
#        raise Exception("Invalid address format: %r" % x)
#    return x 
Example #2
Source File: signed_object.py    From py-ipv8 with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _sign(self,
              private_key: typing.Optional[PrivateKey] = None,
              signature: typing.Optional[bytes] = None) -> None:
        """
        Add a signature to this data.
        Supply either your private key for signing or pass an existing signature.

        :param private_key: the private key to sign with.
        :param signature: the signature to adapt.
        """
        if private_key is not None and signature is None:
            self.signature = private_key.signature(self.get_plaintext())
        elif private_key is None and signature is not None:
            self.signature = signature
        else:
            raise RuntimeError("Specify either private_key or signature!")
        self._hash = hashlib.sha3_256(self.get_plaintext_signed()).digest() 
Example #3
Source File: signature.py    From loopchain with Apache License 2.0 6 votes vote down vote up
def sign(self, data, is_hash: bool):
        if is_hash:
            if isinstance(data, str):
                try:
                    data = data.split("0x")[1] if data.startswith("0x") else data
                    data = binascii.unhexlify(data)
                except Exception as e:
                    logging.error(f"hash data must hex string or bytes \n exception : {e}")
                    return None

        if not isinstance(data, (bytes, bytearray)):
            logging.error(f"data must be bytes \n")
            return None

        raw_sig = self.private_key.ecdsa_sign_recoverable(msg=data,
                                                          raw=is_hash,
                                                          digest=hashlib.sha3_256)
        serialized_sig, recover_id = self.private_key.ecdsa_recoverable_serialize(raw_sig)
        return serialized_sig + bytes((recover_id, )) 
Example #4
Source File: tree.py    From py-ipv8 with GNU Lesser General Public License v3.0 6 votes vote down vote up
def __init__(self, public_key: Optional[PublicKey] = None, private_key: Optional[PrivateKey] = None) -> None:
        """
        Create a new view of another's chain by specifying a public key or create your own chain by supplying
        a private key.

        :param public_key: the public key of the owner of this chain.
        :param private_key: the private key to use to add tokens to this chain.
        """
        super(TokenTree, self).__init__()
        self._logger = logging.getLogger(self.__class__.__name__)

        self.elements = {}
        self.unchained = OrderedDict()
        self.unchained_max_size = 100

        if public_key is not None and private_key is None:
            self.public_key = public_key.pub()
            self.private_key = None
        elif public_key is None and private_key is not None:
            self.private_key = private_key
            self.public_key = private_key.pub()
        else:
            raise RuntimeError("Specify either public_key or private_key!")

        self.genesis_hash = sha3_256(self.public_key.key_to_bin()).digest() 
Example #5
Source File: block_prover.py    From loopchain with Apache License 2.0 6 votes vote down vote up
def to_hash32(self, value: Union[Hash32, bytes, bytearray, int, bool, dict]):
        if value is None:
            return Hash32.empty()
        elif isinstance(value, Hash32):
            return value
        elif isinstance(value, (bytes, bytearray)) and len(value) == 32:
            return Hash32(value)

        if isinstance(value, bool):
            value = b'\x01' if value else b'\x00'
        elif isinstance(value, int):
            if value < 0:
                raise RuntimeError(f"value : {value} is negative.")
            value = value.to_bytes((value.bit_length() + 7) // 8, "big")
        elif isinstance(value, dict):
            if self.type == BlockProverType.Receipt:
                value = dict(value)
                value.pop("failure", None)
                value.pop("blockHash", None)

            hash_generator = self.get_hash_generator()
            value = hash_generator.generate_salted_origin(value)
            value = value.encode()
        return Hash32(hashlib.sha3_256(value).digest()) 
Example #6
Source File: output.py    From slither with GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(self, info, additional_fields=None, markdown_root='', standard_format=True):
        if additional_fields is None:
            additional_fields = {}

        # Allow info to be a string to simplify the API
        if isinstance(info, str):
            info = [info]

        self._data = OrderedDict()
        self._data['elements'] = []
        self._data['description'] = ''.join(_convert_to_description(d) for d in info)
        self._data['markdown'] = ''.join(_convert_to_markdown(d, markdown_root) for d in info)

        id_txt = ''.join(_convert_to_id(d) for d in info)
        self._data['id'] = hashlib.sha3_256(id_txt.encode('utf-8')).hexdigest()

        if standard_format:
            to_add = [i for i in info if not isinstance(i, str)]

            for add in to_add:
                self.add(add)

        if additional_fields:
            self._data['additional_fields'] = additional_fields 
Example #7
Source File: test_transaction.py    From Decentralized-Internet with MIT License 6 votes vote down vote up
def test_validate_tx_simple_create_signature(user_input, user_output, user_priv,
                                             asset_definition):
    from bigchaindb.common.transaction import Transaction
    from .utils import validate_transaction_model

    tx = Transaction(Transaction.CREATE, asset_definition, [user_input], [user_output])
    expected = deepcopy(user_output)
    tx_dict = tx.to_dict()
    tx_dict['inputs'][0]['fulfillment'] = None
    serialized_tx = json.dumps(tx_dict, sort_keys=True,
                               separators=(',', ':'), ensure_ascii=True)
    message = sha3_256(serialized_tx.encode()).digest()
    expected.fulfillment.sign(message, b58decode(user_priv))
    tx.sign([user_priv])

    assert tx.inputs[0].to_dict()['fulfillment'] == \
        expected.fulfillment.serialize_uri()
    assert tx.inputs_valid() is True

    validate_transaction_model(tx) 
Example #8
Source File: test_transaction.py    From bigchaindb with Apache License 2.0 6 votes vote down vote up
def test_validate_tx_simple_create_signature(user_input, user_output, user_priv,
                                             asset_definition):
    from bigchaindb.common.transaction import Transaction
    from .utils import validate_transaction_model

    tx = Transaction(Transaction.CREATE, asset_definition, [user_input], [user_output])
    expected = deepcopy(user_output)
    tx_dict = tx.to_dict()
    tx_dict['inputs'][0]['fulfillment'] = None
    serialized_tx = json.dumps(tx_dict, sort_keys=True,
                               separators=(',', ':'), ensure_ascii=True)
    message = sha3_256(serialized_tx.encode()).digest()
    expected.fulfillment.sign(message, b58decode(user_priv))
    tx.sign([user_priv])

    assert tx.inputs[0].to_dict()['fulfillment'] == \
        expected.fulfillment.serialize_uri()
    assert tx.inputs_valid() is True

    validate_transaction_model(tx) 
Example #9
Source File: hidden_service.py    From stem with GNU Lesser General Public License v3.0 6 votes vote down vote up
def address_from_identity_key(key: Union[bytes, 'cryptography.hazmat.primitives.asymmetric.ed25519.Ed25519PublicKey', 'cryptography.hazmat.primitives.asymmetric.ed25519.Ed25519PrivateKey'], suffix: bool = True) -> str:  # type: ignore
    """
    Converts a hidden service identity key into its address. This accepts all
    key formats (private, public, or public bytes).

    :param key: hidden service identity key
    :param suffix: includes the '.onion' suffix if true, excluded otherwise

    :returns: **str** hidden service address

    :raises: **ImportError** if key is a cryptographic type and ed25519 support
      is unavailable
    """

    key = stem.util._pubkey_bytes(key)  # normalize key into bytes

    version = stem.client.datatype.Size.CHAR.pack(3)
    checksum = hashlib.sha3_256(CHECKSUM_CONSTANT + key + version).digest()[:2]
    onion_address = base64.b32encode(key + checksum + version)

    return stem.util.str_tools._to_unicode(onion_address + b'.onion' if suffix else onion_address).lower() 
Example #10
Source File: hidden_service.py    From stem with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _layer_cipher(constant: bytes, revision_counter: int, subcredential: bytes, blinded_key: bytes, salt: bytes) -> Tuple['cryptography.hazmat.primitives.ciphers.Cipher', Callable[[bytes], bytes]]:  # type: ignore
  try:
    from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
    from cryptography.hazmat.backends import default_backend
  except ImportError:
    raise ImportError('Layer encryption/decryption requires the cryptography module')

  kdf = hashlib.shake_256(blinded_key + subcredential + struct.pack('>Q', revision_counter) + salt + constant)
  keys = kdf.digest(S_KEY_LEN + S_IV_LEN + MAC_LEN)

  secret_key = keys[:S_KEY_LEN]
  secret_iv = keys[S_KEY_LEN:S_KEY_LEN + S_IV_LEN]
  mac_key = keys[S_KEY_LEN + S_IV_LEN:]

  cipher = Cipher(algorithms.AES(secret_key), modes.CTR(secret_iv), default_backend())
  mac_prefix = struct.pack('>Q', len(mac_key)) + mac_key + struct.pack('>Q', len(salt)) + salt

  return cipher, lambda ciphertext: hashlib.sha3_256(mac_prefix + ciphertext).digest() 
Example #11
Source File: iroha.py    From iroha-python with Apache License 2.0 6 votes vote down vote up
def hash(proto_with_payload):
        """
        Calculates hash of payload of proto message
        :proto_with_payload: proto transaction or query
        :return: bytes representation of hash
        """
        obj = None
        if hasattr(proto_with_payload, 'payload'):
            obj = getattr(proto_with_payload, 'payload')
        # hash of meta is implemented for block streaming queries,
        # because they do not have a payload in their schema
        elif hasattr(proto_with_payload, 'meta'):
            obj = getattr(proto_with_payload, 'meta')

        bytes = obj.SerializeToString()
        hash = hashlib.sha3_256(bytes).digest()
        return hash 
Example #12
Source File: sha3.py    From app-monero with Apache License 2.0 5 votes vote down vote up
def sha3(value: bytes) -> bytes:
    return hashlib.sha3_256(value).digest() 
Example #13
Source File: flag.py    From ctf-gameserver with ISC License 5 votes vote down vote up
def _gen_mac(secret, protected_data):

    # Keccak does not need an HMAC construction, the secret can simply be prepended
    sha3 = hashlib.sha3_256()
    sha3.update(secret)
    sha3.update(protected_data)
    return sha3.digest()[:MAC_LEN] 
Example #14
Source File: flag.py    From ctf-gameserver with ISC License 5 votes vote down vote up
def _gen_mac(secret, protected_data):

    # Keccak does not need an HMAC construction, the secret can simply be prepended
    sha3 = hashlib.sha3_256()
    sha3.update(secret)
    sha3.update(protected_data)
    return sha3.digest()[:MAC_LEN] 
Example #15
Source File: test_transaction_structure.py    From Decentralized-Internet with MIT License 5 votes vote down vote up
def test_tx_serialization_hash_function(signed_create_tx):
    tx = signed_create_tx.to_dict()
    tx['id'] = None
    payload = json.dumps(tx, skipkeys=False, sort_keys=True,
                         separators=(',', ':'))
    assert sha3.sha3_256(payload.encode()).hexdigest() == signed_create_tx.id 
Example #16
Source File: flag.py    From ctf-gameserver with ISC License 5 votes vote down vote up
def _gen_mac(secret, protected_data):

    # Keccak does not need an HMAC construction, the secret can simply be prepended
    sha3 = hashlib.sha3_256()
    sha3.update(secret)
    sha3.update(protected_data)
    return sha3.digest()[:MAC_LEN] 
Example #17
Source File: flag.py    From ctf-gameserver with ISC License 5 votes vote down vote up
def _gen_mac(secret, protected_data):

    # Keccak does not need an HMAC construction, the secret can simply be prepended
    sha3 = hashlib.sha3_256()
    sha3.update(secret)
    sha3.update(protected_data)
    return sha3.digest()[:MAC_LEN] 
Example #18
Source File: test_transactions.py    From Decentralized-Internet with MIT License 5 votes vote down vote up
def test_post_create_transaction_with_invalid_signature(mock_logger,
                                                        b,
                                                        client):
    from bigchaindb.common.exceptions import InvalidSignature
    from bigchaindb.models import Transaction
    user_priv, user_pub = crypto.generate_key_pair()

    tx = Transaction.create([user_pub], [([user_pub], 1)]).to_dict()
    tx['inputs'][0]['fulfillment'] = 64 * '0'
    tx['id'] = sha3_256(
        json.dumps(
            tx,
            sort_keys=True,
            separators=(',', ':'),
            ensure_ascii=False,
        ).encode(),
    ).hexdigest()

    res = client.post(TX_ENDPOINT, data=json.dumps(tx))
    expected_status_code = 400
    expected_error_message = (
        'Invalid transaction ({}): Fulfillment URI '
        'couldn\'t been parsed'
    ).format(InvalidSignature.__name__)
    assert res.status_code == expected_status_code
    assert res.json['message'] == expected_error_message
    assert mock_logger.error.called
    assert (
        'HTTP API error: %(status)s - %(method)s:%(path)s - %(message)s' in
        mock_logger.error.call_args[0]
    )
    assert (
        {
            'message': expected_error_message, 'status': expected_status_code,
            'method': 'POST', 'path': TX_ENDPOINT
        } in mock_logger.error.call_args[0]
    )
    # TODO put back caplog based asserts once possible
    # assert caplog.records[0].args['status'] == expected_status_code
    # assert caplog.records[0].args['message'] == expected_error_message 
Example #19
Source File: utils.py    From kaos with Apache License 2.0 5 votes vote down vote up
def hash_file(path):
    hasher = hashlib.sha3_256()
    with open(path, 'rb') as f:
        buf = f.read()
        hasher.update(buf)
    return hasher.hexdigest() 
Example #20
Source File: flag.py    From ctf-gameserver with ISC License 5 votes vote down vote up
def _gen_mac(secret, protected_data):

    # Keccak does not need an HMAC construction, the secret can simply be prepended
    sha3 = hashlib.sha3_256()
    sha3.update(secret)
    sha3.update(protected_data)
    return sha3.digest()[:MAC_LEN] 
Example #21
Source File: test_transaction_structure.py    From Decentralized-Internet with MIT License 5 votes vote down vote up
def test_create_tx_no_asset_data(b, create_tx, alice):
    tx_body = create_tx.to_dict()
    del tx_body['asset']['data']
    tx_serialized = json.dumps(
        tx_body, skipkeys=False, sort_keys=True, separators=(',', ':'))
    tx_body['id'] = sha3.sha3_256(tx_serialized.encode()).hexdigest()
    validate_raises(tx_body)


################################################################################
# Inputs 
Example #22
Source File: test_lib.py    From Decentralized-Internet with MIT License 5 votes vote down vote up
def test_get_utxoset_merkle_root_when_no_utxo(b):
    assert b.get_utxoset_merkle_root() == sha3_256(b'').hexdigest() 
Example #23
Source File: transaction.py    From Decentralized-Internet with MIT License 5 votes vote down vote up
def _sign_simple_signature_fulfillment(cls, input_, message, key_pairs):
        """Signs a Ed25519Fulfillment.

            Args:
                input_ (:class:`~bigchaindb.common.transaction.
                    Input`) The input to be signed.
                message (str): The message to be signed
                key_pairs (dict): The keys to sign the Transaction with.
        """
        # NOTE: To eliminate the dangers of accidentally signing a condition by
        #       reference, we remove the reference of input_ here
        #       intentionally. If the user of this class knows how to use it,
        #       this should never happen, but then again, never say never.
        input_ = deepcopy(input_)
        public_key = input_.owners_before[0]
        message = sha3_256(message.encode())
        if input_.fulfills:
            message.update('{}{}'.format(
                input_.fulfills.txid, input_.fulfills.output).encode())

        try:
            # cryptoconditions makes no assumptions of the encoding of the
            # message to sign or verify. It only accepts bytestrings
            input_.fulfillment.sign(
                message.digest(), base58.b58decode(key_pairs[public_key].encode()))
        except KeyError:
            raise KeypairMismatchException('Public key {} is not a pair to '
                                           'any of the private keys'
                                           .format(public_key))
        return input_ 
Example #24
Source File: tendermint_utils.py    From Decentralized-Internet with MIT License 5 votes vote down vote up
def merkleroot(hashes):
    """Computes the merkle root for a given list.

    Args:
        hashes (:obj:`list` of :obj:`bytes`): The leaves of the tree.

    Returns:
        str: Merkle root in hexadecimal form.

    """
    # XXX TEMPORARY -- MUST REVIEW and possibly CHANGE
    # The idea here is that the UTXO SET would be empty and this function
    # would be invoked to compute the merkle root, and since there is nothing,
    # i.e. an empty list, then the hash of the empty string is returned.
    # This seems too easy but maybe that is good enough? TO REVIEW!
    if not hashes:
        return sha3_256(b'').hexdigest()
    # XXX END TEMPORARY -- MUST REVIEW ...
    if len(hashes) == 1:
        return hexlify(hashes[0]).decode()
    if len(hashes) % 2 == 1:
        hashes.append(hashes[-1])
    parent_hashes = [
        sha3_256(hashes[i] + hashes[i+1]).digest()
        for i in range(0, len(hashes)-1, 2)
    ]
    return merkleroot(parent_hashes) 
Example #25
Source File: tendermint_utils.py    From Decentralized-Internet with MIT License 5 votes vote down vote up
def calculate_hash(key_list):
    if not key_list:
        return ''

    full_hash = sha3_256()
    for key in key_list:
        full_hash.update(key.encode('utf8'))

    return full_hash.hexdigest() 
Example #26
Source File: crypto.py    From bigchaindb with Apache License 2.0 5 votes vote down vote up
def hash_data(data):
    """Hash the provided data using SHA3-256"""
    return sha3_256(data.encode()).hexdigest() 
Example #27
Source File: transaction.py    From bigchaindb with Apache License 2.0 5 votes vote down vote up
def _sign_simple_signature_fulfillment(cls, input_, message, key_pairs):
        """Signs a Ed25519Fulfillment.

            Args:
                input_ (:class:`~bigchaindb.common.transaction.
                    Input`) The input to be signed.
                message (str): The message to be signed
                key_pairs (dict): The keys to sign the Transaction with.
        """
        # NOTE: To eliminate the dangers of accidentally signing a condition by
        #       reference, we remove the reference of input_ here
        #       intentionally. If the user of this class knows how to use it,
        #       this should never happen, but then again, never say never.
        input_ = deepcopy(input_)
        public_key = input_.owners_before[0]
        message = sha3_256(message.encode())
        if input_.fulfills:
            message.update('{}{}'.format(
                input_.fulfills.txid, input_.fulfills.output).encode())

        try:
            # cryptoconditions makes no assumptions of the encoding of the
            # message to sign or verify. It only accepts bytestrings
            input_.fulfillment.sign(
                message.digest(), base58.b58decode(key_pairs[public_key].encode()))
        except KeyError:
            raise KeypairMismatchException('Public key {} is not a pair to '
                                           'any of the private keys'
                                           .format(public_key))
        return input_ 
Example #28
Source File: tendermint_utils.py    From bigchaindb with Apache License 2.0 5 votes vote down vote up
def merkleroot(hashes):
    """Computes the merkle root for a given list.

    Args:
        hashes (:obj:`list` of :obj:`bytes`): The leaves of the tree.

    Returns:
        str: Merkle root in hexadecimal form.

    """
    # XXX TEMPORARY -- MUST REVIEW and possibly CHANGE
    # The idea here is that the UTXO SET would be empty and this function
    # would be invoked to compute the merkle root, and since there is nothing,
    # i.e. an empty list, then the hash of the empty string is returned.
    # This seems too easy but maybe that is good enough? TO REVIEW!
    if not hashes:
        return sha3_256(b'').hexdigest()
    # XXX END TEMPORARY -- MUST REVIEW ...
    if len(hashes) == 1:
        return hexlify(hashes[0]).decode()
    if len(hashes) % 2 == 1:
        hashes.append(hashes[-1])
    parent_hashes = [
        sha3_256(hashes[i] + hashes[i+1]).digest()
        for i in range(0, len(hashes)-1, 2)
    ]
    return merkleroot(parent_hashes) 
Example #29
Source File: test_utils.py    From bigchaindb with Apache License 2.0 5 votes vote down vote up
def test_merkleroot():
    from bigchaindb.tendermint_utils import merkleroot
    hashes = [sha3_256(i.encode()).digest() for i in 'abc']
    assert merkleroot(hashes) == (
        '78c7c394d3158c218916b7ae0ebdea502e0f4e85c08e3b371e3dfd824d389fa3') 
Example #30
Source File: test_lib.py    From bigchaindb with Apache License 2.0 5 votes vote down vote up
def test_get_utxoset_merkle_root_when_no_utxo(b):
    assert b.get_utxoset_merkle_root() == sha3_256(b'').hexdigest()