simpleblockchain-py


Namesimpleblockchain-py JSON
Version 0.1.1 PyPI version JSON
download
home_page
SummaryThe simplest Python blockchain library
upload_time2024-02-04 18:13:31
maintainer
docs_urlNone
authorXenonPy
requires_python>=3.9
licenseMIT License Copyright (c) 2024 XenonPydev Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords blockchain crypto simpleblockchain
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # simpleblockchain
A simple library to create and manage blockchains easily.

- 🔨 Quickly Customizable
- 💿 JSON Serialization
- 🔒 Proof-Of-Work
- 🔑 SHA-256 and MD5 Hashing


> *Note: MD5 Hashing is not considered secure*
## Functions

### `get_random_bytes(n)`

Generates a random byte string of length `n`.

#### Parameters:

- `n` (int): Length of the random byte string.

#### Returns:

- `bytes`: Random byte string.

### `calculate_hash(algorithm, *args)`

Calculates the hash based on the specified algorithm and input arguments.

#### Parameters:

- `algorithm` (str): Hashing algorithm ('sha256', 'md5').
- `*args`: Variable number of arguments for hashing.

#### Returns:

- `str`: Calculated hash.

### `encrypt_data(data, key)`

Encrypts data using AES encryption.

#### Parameters:

- `data` (str): Data to be encrypted.
- `key` (bytes): Encryption key.

#### Returns:

- `str`: Hexadecimal representation of the encrypted data.

### `proof_of_work(algorithm, *args, difficulty=4)`

Performs proof-of-work to find a valid nonce that satisfies the specified algorithm and difficulty level.

#### Parameters:

- `algorithm` (str): Hashing algorithm ('sha256', 'md5').
- `*args`: Variable number of arguments for hashing.
- `difficulty` (int): Difficulty level for proof-of-work.

#### Returns:

- `Tuple[int, str]`: Nonce and hash that meet the proof-of-work criteria.

## Classes

### `Block` Class

Represents an individual block in the blockchain.

#### Attributes:

- `block_id` (str): Unique identifier for the block.
- `previous_hash` (str): Hash of the previous block in the chain.
- `timestamp` (float): Time of block creation.
- `data` (str): Data to be stored in the block.
- `hash` (str): Hash of the block.
- `nonce` (int): Nonce value for proof-of-work.

#### Methods:

- `__init__(self, block_id, previous_hash, timestamp, data, hash, nonce)`: Initializes a new block.

### `Blockchain` Class

Represents the entire blockchain.

#### Attributes:

- `chain` (list): List of blocks in the blockchain.
- `hashing_algorithm` (str): Hashing algorithm ('sha256', 'md5').
- `encryption_key` (bytes): Encryption key for block data.
- `difficulty` (int): Difficulty level for proof-of-work.

#### Methods:

- `__init__(self, hashing_algorithm='sha256', encryption_key=None, difficulty=4)`: Initializes a new blockchain.
- `create_genesis_block()`: Creates the genesis block and adds it to the blockchain.
- `add_block(data, block_id=None)`: Adds a new block to the blockchain.
- `to_json()`: Serializes the blockchain to JSON format.
- `from_json(json_data)`: Deserializes JSON data and reconstructs the blockchain.

## Example Usage

```python
from simpleblockchain import Blockchain, calculate_hash, encrypt_data, proof_of_work, get_random_bytes
import json

# Create a custom class based on the Blockchain class 
class AcademicBlockchain(Blockchain):
    def __init__(self, institution_name, encryption_key=None, difficulty=4):
        super().__init__(hashing_algorithm='sha256', encryption_key=encryption_key, difficulty=difficulty)
        self.institution_name = institution_name

    def create_academic_record(self, student_name, degree, graduation_year):
        data = {
            'student_name': student_name,
            'degree': degree,
            'graduation_year': graduation_year,
            'institution': self.institution_name
        }

        # Convert data to JSON and encrypt it
        data_json = json.dumps(data)
        encrypted_data = encrypt_data(data_json, self.encryption_key)

        # Add the encrypted academic record to the blockchain
        self.add_block(encrypted_data)

encryption_key = get_random_bytes(16)  # 16 bytes for AES encryption key
university_blockchain = AcademicBlockchain(institution_name='University of Decentralization', encryption_key=encryption_key)

# Create academic records
university_blockchain.create_academic_record("Alice Doe", "Computer Science", 2022)
university_blockchain.create_academic_record("Bob Smith", "Electrical Engineering", 2023)

# Serialize the blockchain for storage or transmission
serialized_data = university_blockchain.to_json()

# ... Store or transmit the serialized_data

# Deserialize the blockchain from stored or transmitted data
new_university_blockchain = AcademicBlockchain(institution_name='University of Decentralization', encryption_key=encryption_key)
new_university_blockchain.from_json(serialized_data)

# Verify academic records
for block in new_university_blockchain.chain:
    decrypted_data = encrypt_data(block.data, new_university_blockchain.encryption_key, decrypt=True)
    print(f"Decrypted Academic Record: {decrypted_data}")
```



            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "simpleblockchain-py",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "blockchain,crypto,simpleblockchain",
    "author": "XenonPy",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/b4/ae/c6aa34dee7ca763d251147defafde0ab4491190441fa3b8f24f29ce92980/simpleblockchain_py-0.1.1.tar.gz",
    "platform": null,
    "description": "# simpleblockchain\nA simple library to create and manage blockchains easily.\n\n- \ud83d\udd28 Quickly Customizable\n- \ud83d\udcbf JSON Serialization\n- \ud83d\udd12 Proof-Of-Work\n- \ud83d\udd11 SHA-256 and MD5 Hashing\n\n\n> *Note: MD5 Hashing is not considered secure*\n## Functions\n\n### `get_random_bytes(n)`\n\nGenerates a random byte string of length `n`.\n\n#### Parameters:\n\n- `n` (int): Length of the random byte string.\n\n#### Returns:\n\n- `bytes`: Random byte string.\n\n### `calculate_hash(algorithm, *args)`\n\nCalculates the hash based on the specified algorithm and input arguments.\n\n#### Parameters:\n\n- `algorithm` (str): Hashing algorithm ('sha256', 'md5').\n- `*args`: Variable number of arguments for hashing.\n\n#### Returns:\n\n- `str`: Calculated hash.\n\n### `encrypt_data(data, key)`\n\nEncrypts data using AES encryption.\n\n#### Parameters:\n\n- `data` (str): Data to be encrypted.\n- `key` (bytes): Encryption key.\n\n#### Returns:\n\n- `str`: Hexadecimal representation of the encrypted data.\n\n### `proof_of_work(algorithm, *args, difficulty=4)`\n\nPerforms proof-of-work to find a valid nonce that satisfies the specified algorithm and difficulty level.\n\n#### Parameters:\n\n- `algorithm` (str): Hashing algorithm ('sha256', 'md5').\n- `*args`: Variable number of arguments for hashing.\n- `difficulty` (int): Difficulty level for proof-of-work.\n\n#### Returns:\n\n- `Tuple[int, str]`: Nonce and hash that meet the proof-of-work criteria.\n\n## Classes\n\n### `Block` Class\n\nRepresents an individual block in the blockchain.\n\n#### Attributes:\n\n- `block_id` (str): Unique identifier for the block.\n- `previous_hash` (str): Hash of the previous block in the chain.\n- `timestamp` (float): Time of block creation.\n- `data` (str): Data to be stored in the block.\n- `hash` (str): Hash of the block.\n- `nonce` (int): Nonce value for proof-of-work.\n\n#### Methods:\n\n- `__init__(self, block_id, previous_hash, timestamp, data, hash, nonce)`: Initializes a new block.\n\n### `Blockchain` Class\n\nRepresents the entire blockchain.\n\n#### Attributes:\n\n- `chain` (list): List of blocks in the blockchain.\n- `hashing_algorithm` (str): Hashing algorithm ('sha256', 'md5').\n- `encryption_key` (bytes): Encryption key for block data.\n- `difficulty` (int): Difficulty level for proof-of-work.\n\n#### Methods:\n\n- `__init__(self, hashing_algorithm='sha256', encryption_key=None, difficulty=4)`: Initializes a new blockchain.\n- `create_genesis_block()`: Creates the genesis block and adds it to the blockchain.\n- `add_block(data, block_id=None)`: Adds a new block to the blockchain.\n- `to_json()`: Serializes the blockchain to JSON format.\n- `from_json(json_data)`: Deserializes JSON data and reconstructs the blockchain.\n\n## Example Usage\n\n```python\nfrom simpleblockchain import Blockchain, calculate_hash, encrypt_data, proof_of_work, get_random_bytes\nimport json\n\n# Create a custom class based on the Blockchain class \nclass AcademicBlockchain(Blockchain):\n    def __init__(self, institution_name, encryption_key=None, difficulty=4):\n        super().__init__(hashing_algorithm='sha256', encryption_key=encryption_key, difficulty=difficulty)\n        self.institution_name = institution_name\n\n    def create_academic_record(self, student_name, degree, graduation_year):\n        data = {\n            'student_name': student_name,\n            'degree': degree,\n            'graduation_year': graduation_year,\n            'institution': self.institution_name\n        }\n\n        # Convert data to JSON and encrypt it\n        data_json = json.dumps(data)\n        encrypted_data = encrypt_data(data_json, self.encryption_key)\n\n        # Add the encrypted academic record to the blockchain\n        self.add_block(encrypted_data)\n\nencryption_key = get_random_bytes(16)  # 16 bytes for AES encryption key\nuniversity_blockchain = AcademicBlockchain(institution_name='University of Decentralization', encryption_key=encryption_key)\n\n# Create academic records\nuniversity_blockchain.create_academic_record(\"Alice Doe\", \"Computer Science\", 2022)\nuniversity_blockchain.create_academic_record(\"Bob Smith\", \"Electrical Engineering\", 2023)\n\n# Serialize the blockchain for storage or transmission\nserialized_data = university_blockchain.to_json()\n\n# ... Store or transmit the serialized_data\n\n# Deserialize the blockchain from stored or transmitted data\nnew_university_blockchain = AcademicBlockchain(institution_name='University of Decentralization', encryption_key=encryption_key)\nnew_university_blockchain.from_json(serialized_data)\n\n# Verify academic records\nfor block in new_university_blockchain.chain:\n    decrypted_data = encrypt_data(block.data, new_university_blockchain.encryption_key, decrypt=True)\n    print(f\"Decrypted Academic Record: {decrypted_data}\")\n```\n\n\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 XenonPydev  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "The simplest Python blockchain library",
    "version": "0.1.1",
    "project_urls": {
        "Homepage": "https://github.com/XenonPy/simpleblockchain"
    },
    "split_keywords": [
        "blockchain",
        "crypto",
        "simpleblockchain"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "432dd56fb545a06011afa603a10f40f3e461f71acfc63777e619492ff7cc2619",
                "md5": "90b016b513a88aee2cffbb33c769c14d",
                "sha256": "b2473bc00540104d7e5dba9f25609d695bc679100303ff9c80d876e0f7e10c11"
            },
            "downloads": -1,
            "filename": "simpleblockchain_py-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "90b016b513a88aee2cffbb33c769c14d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 4100,
            "upload_time": "2024-02-04T18:12:49",
            "upload_time_iso_8601": "2024-02-04T18:12:49.787414Z",
            "url": "https://files.pythonhosted.org/packages/43/2d/d56fb545a06011afa603a10f40f3e461f71acfc63777e619492ff7cc2619/simpleblockchain_py-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b4aec6aa34dee7ca763d251147defafde0ab4491190441fa3b8f24f29ce92980",
                "md5": "060019493993a42bd688f57977bf573d",
                "sha256": "4efb0454bcfd19ea37c7ac2199339dbda33df8b0eab15ae831ad207bd1166cea"
            },
            "downloads": -1,
            "filename": "simpleblockchain_py-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "060019493993a42bd688f57977bf573d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 4318,
            "upload_time": "2024-02-04T18:13:31",
            "upload_time_iso_8601": "2024-02-04T18:13:31.812512Z",
            "url": "https://files.pythonhosted.org/packages/b4/ae/c6aa34dee7ca763d251147defafde0ab4491190441fa3b8f24f29ce92980/simpleblockchain_py-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-04 18:13:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "XenonPy",
    "github_project": "simpleblockchain",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "simpleblockchain-py"
}
        
Elapsed time: 0.18188s