1

I need to encrypt the database with a password. If anyone tries to run the code to select data from the database, they will need to enter the password. If the password is correct, the database should be accessible otherwise, it should not. The password is hashed and the hashed format is stored in a separate file, the program should hash any password given and compare it with the stored hashed password. I am using the GridDB container to store the database. Now, I can access the database without a password. How do I add this encryption? The code below checks the password file and compares the input to the stored hashed password. How do I connect this to my GridDB container?

#...
import hashlib
def store(password, output_src):
    string_password = "%s" %(password)
    bytes_password = str.encode(string_password)
    hashed = hashlib.sha224(bytes_password).hexdigest()
    with open(output_src, "w") as out:
        out.write(hashed)
    print("Done!")
    return output_src

print(store("p@$$w0rd", "password.hashed"))

def validate(password, stored_password_src_file):
    string_password = "%s" %(password)
    bytes_password = str.encode(string_password)
    hashed = hashlib.sha224(bytes_password).hexdigest()    
    with open(stored_password_src_file, "r") as stored_pass:
        if stored_pass.readline() == hashed:
            return True
        else:
            return False

print(validate("p@$$word", "password.hashed"))
# Returns False since they are not the same

The code below represents a GridDB container storing my database:

import griddb_python as griddb
import pandas as pd

factory = griddb.StoreFactory.get_instance()

# Initialize container
try:
    gridstore = factory.get_store(host="127.0.0.1", port="8900", 
            cluster_name="MyApp", username="root", 
            password="")

    conInfo = griddb.ContainerInfo("Reviews",
                    [["email", griddb.Type.STRING], #”example@example.com”
       [“phoneNumber”, griddb.Type.INTEGER], #without “+”, with country code
                     ["review", griddb.Type.STRING], # Ex: “This is such a nice app”
       ["gender", griddb.Type.STRING], # Ex: “M”
                     ["sentiment", griddb.Type.STRING], # Ex: “Positive”
                    ],
                    griddb.ContainerType.COLLECTION, True)
    cont = gridstore.put_container(conInfo)   
    data = pd.read_csv("AppReviews.csv")
    #Add data
    for i in range(len(data)):
        ret = cont.put(data.iloc[i, :])
    print("You have successfully added the data!")

except griddb.GSException as e:
    print(f”An Error Occurred! {e}”)

# The programmer should provide a password before accessing the container
sql_statement = (“SELECT * FROM Reviews”)
sql_query = pd.read_sql_query(sql_statement, cont)
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39

0 Answers0