<commit_before># -*- coding: utf-8 -*-
"""
Contains functions for cryptographically signing / verifying.
"""

# Copyright (C) 2012-2013 <NAME>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from Crypto.PublicKey import RSA
from Crypto.Hash import SHA512
from Crypto.Signature import PKCS1_v1_5
import msgpack


def construct_hash(value, timestamp, expires, name, meta):
    """
    The hash is a SHA512 hash of the combined SHA512 hashes of the msgpack 
    encoded 'value', 'timetamp, 'expires', 'name' and 'meta' fields (in 
    that order).

    It ensures that the 'value', 'timestamp', 'expires', 'name' and 'meta'
    fields have not been tampered with.
    """
    hashes = []
    for item in (value, timestamp, expires, name, meta):
        packed = msgpack.packb(item)
        hashed = SHA512.new(packed).digest()
        hashes.append(hashed)
    compound_hashes = ''.join(hashes)
    return SHA512.new(compound_hashes)


def construct_key(public_key, name=''):
    """
    Given a string representation of a user's public key and the human 
    readable string to use as a key in the DHT this function will return a 
    digest of the SHA512 hash to use as the actual key to use within the DHT.

    This ensures that the provenance (public key) and meaning of the key
    determine its value in the DHT.
    """
    # Simple normalisation: no spaces or newlines around the public key
    key_hash = SHA512.new(public_key.strip())
    if name:
        # If the key has a meaningful name, create a compound key based upon
        # the SHA512 values of both the public_key and name.
        name_hash = SHA512.new(name)
        compound_key = key_hash.digest() + name_hash.digest()<commit_msg>Update crypto.py<commit_after># -*- coding: utf-8 -*-
"""
Contains functions for cryptographically signing / verifying.
"""

# Copyright (C) 2012-2013 <NAME>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from Crypto.PublicKey import RSA
from Crypto.Hash import SHA512
from Crypto.Signature import PKCS1_v1_5
import msgpack


def construct_hash(value, timestamp, expires, name, meta):
    """
    The hash is a SHA512 hash of the concatenated SHA512 hashes of the 
    msgpack encoded 'value', 'timetamp, 'expires', 'name' and 'meta' 
    fields (in that order).

    It ensures that the 'value', 'timestamp', 'expires', 'name' and 'meta'
    fields have not been tampered with.
    """
    hashes = []
    for item in (value, timestamp, expires, name, meta):
        packed = msgpack.packb(item)
        hashed = SHA512.new(packed).digest()
        hashes.append(hashed)
    compound_hashes = ''.join(hashes)
    return SHA512.new(compound_hashes)


def construct_key(public_key, name=''):
    """
    Given a string representation of a user's public key and the human 
    readable string to use as a key in the DHT this function will return a 
    digest of the SHA512 hash to use as the actual key to use within the DHT.

    This ensures that the provenance (public key) and meaning of the key
    determine its value in the DHT.
    """
    # Simple normalisation: no spaces or newlines around the public key
    key_hash = SHA512.new(public_key.strip())
    if name:
        # If the key has a meaningful name, create a compound key based upon
        # the SHA512 values of both the public_key and name.
        name_hash = SHA512.new(name)
        compound_key = key_hash.digest() + name_hash.digest()
{'fraction_non_alphanumeric': 0.08000862626698296, 'fraction_numerical': 0.017252533965926246, 'mean_word_length': 3.3878902554399244, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '187418', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1278, 'n_words': 598}