You can edit almost every page by Creating an account and confirming your email.

Storr Python Package

From EverybodyWiki Bios & Wiki


storr
Developer(s)Krishnavyshak
Stable release
1.0.0 / 01 March 2023
Repositoryhttps://github.com/KrishnaVyshak/storr
Written inPython
Engine
    Operating systemCross-platform
    TypeUtility
    LicenseMIT License
    Websitevyshak.me/storr

    Search Storr Python Package on Amazon.

    storr is a Python Package that provides a secure and efficient way to store session data in a file. It simplifies the process of storing and retrieving data, ensuring that sensitive information is not exposed in plain text. This package makes use of the cryptography package, which ensures that only authorized users can access it. The encrypted data is stored in a file that is protected by the operating system's permissions, which adds an extra layer of security.

    Usage

    The library uses the cryptography package to encrypt all stored data with the Fernet symmetric encryption algorithm, ensuring that only authorized parties can access it. Additionally, storr provides an additional layer of security by storing the encrypted data in a file that is protected by the operating system's permissions.

    This Python Package is available in the official package index of Python (PyPI) and the installation can be done by using the command pip install storr

    The basic usage example of this package is as follows:

    from storr import SecureStorage
    
    session_id = "my-session-id"
    storage = SecureStorage(session_id)
    

    To create a SecureStorage object, a session_id parameter is required, which serves as a unique identifier for the session. Optional parameters include file_path to specify the session file location and key for encryption purposes. If the key parameter is not provided, the system will automatically generate a new encryption key.

    To store data in the session using the SecureStorage object, users can make use of the set method. The set method requires two arguments:

    name: The name of the data to be stored, which should be a string.

    value: The value of the data to be stored, which can be of any data type.

    my_var = "John Doe"
    storage.set("name", my_var)
    #           ^^^^^^  ^^^^^^
    #            KEY    VALUE
    

    To retrieve data from a session, the get method of the SecureStorage object can be used. The method requires a string parameter that represents the name of the data to retrieve.

    my_var = storage.get("name") # OUTPUT: John Doe
    

    When using the get method to retrieve data from a session, if the requested data is not found, the method will return a value of None.

    Advanced Usage

    Custom File Path

    One way to customize where session data is stored is by specifying a file path other than the default location. This can be done by including a file_path parameter when setting up the SecureStorage object.

    session_id = "my-session-id"
    file_path = "/path/to/custom/session.json"
    storage = SecureStorage(session_id, file_path)
    

    Custom Encryption Key

    When initializing a SecureStorage object, users have the option to provide a custom encryption key by including a key parameter. This allows for a unique key to be used in encrypting and decrypting data, instead of the default key provided by the system. The use of a custom encryption key can provide an added layer of security for sensitive information stored in the SecureStorage.

    from storr import SecureStorage
    import base64
    import secrets
    
    # create a session ID for the current user (you could use any string here)
    session_id = '123'
    
    # Generate 32 bytes of random data
    data = secrets.token_bytes(32)
    
    # Encode the data using base64url encoding
    key = base64.urlsafe_b64encode(data)
    
    # create an instance of the SecureStorage class for the session
    #create a 32 urlsafe base64-encoded bytes key
    storage = SecureStorage(session_id, key=key)
    

    Examples

    A typical way to use storr can be demonstrated through a simple example:

    from storr import SecureStorage
    
    # initialize the storage with a session ID
    storage = SecureStorage(session_id='123')
    
    # store a value
    storage.set('name', 'John Doe')
    
    # retrieve a value
    name = storage.get('name')
    
    # print the value
    print(name)  # Output: John Doe
    

    In the provided code snippet, a SecureStorage object is created with a session ID of '123'. The name 'John Doe' is stored in the object under the key name. Later on, the stored name is retrieved using the same key.

    Using a custom file path

    It is possible to set a personalized file path for the session data file by providing the file_path parameter when initializing the SecureStorage object. For instance, the following code demonstrates how to set a custom file path:

    import storr
    
    # Create a SecureStorage instance with a custom file path
    storage = storr.SecureStorage('my_session', file_path='/path/to/my/session.json')
    
    # Set a value
    storage.set('name', 'John Doe')
    
    # Get a value
    name = storage.get('name')
    print(name)  # Output: John Doe
    

    Using a custom encryption key

    It is possible to use a custom encryption key for session data by passing it as a parameter to the SecureStorage constructor. An example of how to do this is provided.

    import storr
    import base64
    import secrets
    
    # Generate 32 bytes of random data
    data = secrets.token_bytes(32)
    
    # Encode the data using base64url encoding
    myKey = base64.urlsafe_b64encode(data)
    
    # Create a SecureStorage instance with the custom key
    storage = storr.SecureStorage('my_session', key=myKey)
    
    # Set a value
    storage.set('name', 'John Doe')
    
    # Get a value
    name = storage.get('name')
    print(name)  # Output: John Doe
    

    Handling missing or invalid values

    The get method is used to retrieve a value from a dictionary in Python. If the value being searched for does not exist in the dictionary, the get method will return a default value of None. This can be helpful in situations where the value being searched for may not always exist, as it allows you to handle missing or invalid values in your code without causing errors. An example of using the get method would be checking if a key exists in a dictionary and returning a default value if it doesn't.

    import storr
    
    # Create a SecureStorage instance
    storage = storr.SecureStorage('my_session')
    
    # Try to get a value that doesn't exist
    name = storage.get('name')
    
    if name is None:
        print('Name not found')
    else:
        print('Name:', name)
    

    Security

    storr is a tool that employs the Fernet symmetric encryption algorithm to secure all session data. The encryption key is automatically generated, but users have the option to provide a custom encryption key for additional security.

    Version

    Version Code Release Date
    0.0.1 Nov 2022
    0.5.0 Jan 2023
    1.0.0 (Stable) Feb 2023

    The initial release of storr includes basic features for secure session storage. In subsequent versions, developers plan to introduce additional advanced features to enhance the security and flexibility of the storage system.

    References


    This article "Storr Python Package" is from Wikipedia. The list of its authors can be seen in its historical and/or the page Edithistory:Storr Python Package. Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.