Day 072 - Project 72 : Unique Hashed + Salted password for Diary

If you have any questions, comments or issues with this project please post them here!

This is a weird issue, but I thought I had finished this challenge because it was working while I was logged in.

But when I ran it logged out of my account from my public profile page, I got an error message. I tried it again being logged in to the project, and I got no error message.

This is my project for reference: https://replit.com/@libraryguyeli/72100-Diary-cont#main.py

It works perfectly for me!

That’s so strange. It’s still not working for me logged out. Thanks for letting me know, though! I appreciate you checking it.

1 Like

Hi @libraryguyeli I opened the above link in an incognito tab. I get the following error:

image

Is this the error you see?

Yes, that’s the message I’m seeing. The person that commented above got it to work, so I wonder if it’s an incognito issue?

Thanks @libraryguyeli I’ve reached out to support as I don’t think that should be happening when you run the Repl.

Thank you! Much appreciated. :slight_smile:

Hello guys! I am not happy with David’s solution here. Does it totally neglect the fact that Secret Diary is supposed to be used by several users?

In order to view the diary entries only for the specific user logged in at the time, you would have to take the username into account when viewing the diary entries!

I have tried so hard to do that with my code but I can’t get it to work…

Can someone please try to create two users and make some entries (storing and everything works fine) but I cannot figure out how to only show the timestamp (key) : entry (values) from the logged in user (dictionary nested within db).

from replit import db
import datetime, os, time, random

for key, value in db.items():
  print(f"{key}: \t{value}")

username = ""
loggedIn = False


def create():
  username = input("Create Username: ")
  password = input("Create Password: ")
  salt = random.randint(0, 9999)
  password_hashed = hash(f"{salt}{password}")
  db[username] = {"salt": salt, "password": password_hashed}
def login():
  global username
  global loggedIn
  print("  PRIVATE DIARY")
  print("==================")
  print("LOGIN SYSTEM\n\n")
  username = input("Enter Username: ")
  password = input("Enter Password: ")
  if username not in db.keys():
    print("No such user!")
  salt = db[username]["salt"]
  password_hashed = hash(f"{salt}{password}")
  if username in db.keys() and password_hashed == db[username]["password"]:
    print(f"Welcome {username}!")
    loggedIn = True
  else:
    print("Incorrect Password")
def display(username):
  for key in db.keys: 
    if key == username:
      keys = username.prefix("2023")
      sorted(keys, reverse=True)
      for key in keys:
        print(f"{key}:\t{db[key]}")
        print()
        opt = input("Next or exit? > ")
        if(opt.lower()[0]=="e"):
          break
      print("\nEnd of Diary")
def access():
  print("  PRIVATE DIARY")
  print("==================")
  if len(db.keys()) == 0:
    create()
  select = input("1: Add User\n2: Login\n> ")
  if select == "1":
    create()
    login()
  elif select == "2":
    login()
  else:
    print("No valid option! Try again!")
    access()
def add(username, entry):
  timestamp = str(datetime.datetime.now())
  db[username][timestamp] = entry


while True:
  #os.system("clear")
  if loggedIn == False:
    access()
  #os.time(3)
  #os.system("clear")
  print("   DIARY")
  print("=============\n")
  select = input("Add, View or Search?\n")
  if select[0] == "a":
    entry = input("Type your entry here:\n")
    add(username, entry)
  elif select[0] == "v":
    display(username)
  elif select[0] == "s":
    day = int(input("Enter Day: "))
    month = int(input("Enter Month: "))
    #search(day, month)
  input("\n\npress enter to return")
  continue

Please help :cry: I’ve had a hard day :sob:

The problem is (assuming I understand how it works in Replit as I do not share projects myself) that when a user runs your Replit a new database is created for that user.
The only way to avoid this is to do a web always on (maybe there is a lesson about it) or use a central database not part of the repl.

2 Likes

The way I understood the assignment was that db dictionary gets filled with as many user dictionaries as desired (nested dictionaries!). So that each user has their own dictionary containing only their entries (and for that matter also their password).

Here’s an example of what I mean:

db = {
username1= {“password”:password_hased, “salt”:salt, timestamp1:entry1, timestamp2:entry2},
username2= {“password”:password_hased, “salt”:salt, timestamp1:entry1, timestamp2:entry3},
and so on
}

each user is the key and their value is a whole dictionary containing all the diary entries and their password.

So how can you display only the nested user dictionary (excluding the password key-pair) ?

I am no longer understanding the question.
I thought your problem was that if a user runs your repl they will not see the user created by another user running the same repl.

Ok, let me rephrase my question perhaps to get a more conceptual solution:

How can I access a specific key in a dictionary and then within that key, display only the key-value pairs that I want?

You mean like db[key1][key2]?

yes… but I guess something like that:

db[username] [prefix"2023"]

because I don’t want all the keys in db[username]

Maybe what you want is to make a deep copy of dp[username], delete the key password and use this copy?

I guess that could do, yeah. But I was hoping for a shortcut there.
Anyways, I’ll try that! Thanks :smiley:

If it is for displaying you can turn a dict into a f-string also, just use the ones you want to show.

Or make a selective copy

original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
keys_to_exclude = ['a', 'c']

new_dict = {k:v for k,v in original_dict.items() if k not in keys_to_exclude}

2 Likes

Thank You! Now I got the right perspective here :slight_smile: Thanks!

1 Like

Hello. @IanAtCSTeach, I am having the same issue as @libraryguyeli was having. Do you have any suggestions on what to do next? The code works otherwise when running in my browser.
here is my code for reference – > https://replit.com/@kotacoded/DearSecretDiary-72100

1 Like