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

Thanks @kotacoded I’ve reached out to support to investigate but think that the issue is that when using incognito mode you are not logged into Replit so the ReplDB doesn’t have any account to authenticate against. When using it in a normal browser window you are logged into Replit so ReplDB can match against your user. I might be over simplifying it here but think this might be the reason for the error. @ShaneAtReplit would you agree with this?

1 Like

I think that’s the case, although I’m not too sure. I’ll send this through our bug triaging process!

2 Likes

The team has let me know that Replit Database can’t work on the cover page of a Repl for logged-out users due to how our authentication system works with Replit DB.

We have other priorities that we need to address, but we appreciate your feedback and will fix this in future updates of Replit Database.

3 Likes

Hi guys! I have an issue with the viewEntry()… even though I put a prefix to the entries it still shows me the password as the last one and then goes back in time, from last entry to the first… here’s my def or a link to the repl…

def viewEntry():
  matches = db.prefix("ent")
  matches = sorted(db.keys(), reverse=True)
  for key in matches:
    print(f"""{key}: {db[key]}""")
    print()
    next = input("Show next entry?\n> ")
    if next == "yes":
      key =+ 1
      continue
    else:
      break

Okay… I figured that for some reason the “sorted” func is not working but with "
matches = matches[::-1]" it gives the prefix… now an issue of showing only the logged user entries remains… how do I show only the entries of one user and not the others as well…
Any comment/help is much appreciated :slight_smile:

1 Like

Top of my mind, quick fix that I can think of is try to insert another identifier in the prefix.

Here is a portion of my code I would like help with. In the original implementation of the diary, this code simply works backwards and if there are no entries then it returns with a message saying “no entries to display.”

However, if the first entry in the DB is the hash and salt, then this will need to be amended.

		else:
			index = 0
			while True:
				sorted_keys = sorted(keys, reverse = True)
				try:
					date = sorted_keys[index]
					entry = db[date]
				except:
					print("An error occurred. You reached the earliest journal entry, or attempted to access an entry without making one.")
					time.sleep(1)
					os.system("clear")
					break

Hi @EmeraldMoss how can we help you with the code? Can you explain a little more about what you want it to do?

1 Like
from replit import db
import datetime,os,time
import random
from getpass import getpass

def add():
  add = input("Input your thought > ")
  timestamp = datetime.datetime.now()
  db[timestamp]= add
  time.sleep(1)
  os.system("clear")
  menu()

def view():
  keys = db.prefix('2')
  for key in keys:
    time.sleep(1)
    os.system("clear")
    print(f"{key}: {db[key]}")
    print()
    ask = input("Next/Exit > ").capitalize()
    if ask=='Next':
      os.system("clear")
    elif ask=='Exit':
      os.system("clear")
      menu()
      break

def menu():
  print("~Welcome To Secret Place Of Thoughts~")
  print()
  print("""1. Add
2. View""")
  print()
  enter = input("> ")
  if enter=='1':
    add()
  elif enter=='2':
    view()

def newUser():
  keys = db.keys()
  while True:
    if len(keys)==0:
      print("Create account.")
      print()
      name = input("Please enter your username > ")
      pw = input("Please enter your password > ")
      salt = random.randint(1000,9999)
      newPw = f"{pw}{salt}"
      newPw = hash(newPw)
      db[name] = {"Password": newPw, "salt":salt}
      os.system("clear")
      menu()
    elif len(keys)>=1:
      print("Log in")
      print()
      name = input("Please enter your username > ")
      pw = getpass("Please enter your password > ")
      if name not in db:
        print("Icorrect login.")
        exit()
      elif db[name]["Password"]!==newPW:
        print("Incorrect login.")
        exit()
      elif name in db:
        salt = db[name]['salt']
        newPW = f"{pw}{salt}"
        newPW = hash(newPW)
        if newPW==db[name]['Password']:
          print()
          print("Welcome back ^^")
          time.sleep(2)
          os.system("clear")
          menu()
        else:
          print("Try again.")
          time.sleep(1)
          os.system("clear")
while True:
  newUser()

Instead of using len to check if it is the first run or not,

Solution and slight shortcut?
keys = db.keys()
if len(keys)<1:     #<---
  print("First Run > Create account")
  ...
else:
  print("Log in")


I just used,

if db.keys():  #<-----
  print("Returning user")
else:
  print("New user")

1 Like

I’m getting this to compile, but there are some bugs I can’t work out, including how it prints after adding a user, and when a user logs in successfully, it shows “Incorrect Username”… would love any insight. Thanks in advance.

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

def createuser():
  print("ADD USER")
  user = input("Username: ")
  pw = input("Password: ")
  salt = random.randint(1000,9999)
  newpw = f"{pw}{salt}"
  newpw = hash(newpw)

  db[user] = {"password": newpw, "salt": salt} 
  print("User created")
  time.sleep(2)
  os.system("clear")

def login():
  print("LOGIN")
  user = input("Username: ")
  pw = input("Password: ")
  keys = db.keys()
  for key in keys:
    if key == user:
      newpw = f"{pw}{db[user]['salt']}"
      hashnewpw = hash(newpw)
      if hashnewpw == db[user]['password']:
        print("Login Successful")
      else: 
        print("Password Incorrect")
        return
    else:
      print("Username Incorrect")
      return
  time.sleep(3)
  os.system("clear")
  
def addentry():
  entry = input("Input entry: ")
  timestamp = datetime.datetime.now()
  db[timestamp] = entry
  print("Entry added.")
  time.sleep(2)
  os.system("clear")
  keys = db.keys()
  for key in keys: 
    print(f"""{key}: {db[key]}""")
    time.sleep(5)
  print()
  os.system("clear")

def viewentry():
  keys = db.keys()
  for key in keys:
    time.sleep(1)
    os.system("clear")
    print(f"""{key}
    {db[key]}""")
    print()
    opt = input("Next or exit? > ")
    if(opt.lower()[0]=="e"):
      break

while True: 
  keys = db.keys()
  counter = 0 
  for key in keys: 
    counter += 1
  if counter==0:
    createuser()
  else:
    login()
  menu = input("1. Add\n2. View\nSelection: > ")
  if menu == "1":
    addentry()
  elif menu == "2":
    viewentry()
  else: 
    break