Password module?

not getpass.....

Page 1 of 1

6 Replies - 1002 Views - Last Post: 07 July 2010 - 05:52 AM Rate Topic: -----

#1 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Password module?

Posted 07 July 2010 - 03:09 AM

I'm trying to plan a big project (well, by my standards as a hobby coder!), and I need a 'login screen', much like almost every DB-based program has. The username part is easy (just check the entered string against an SQL selection statement), but the password part has me a little stumped. I don't really want to have to code this from stratch (with the hashing/encryption), I'm after a 'cookie-cutter' module - a black box. The getpass module in Python doesn't cut it, as I want characters displayed as * or some other ASCII character (just as in every other password box other than the *nix terminal!). Plus, it'll need to be hashed/encrypted as I'll have to send it across the network (the app will be a client-server product)

I'm kind of out of my depth here, but you never become a better swimmer without venturing out a bit!

Is This A Good Question/Topic? 0
  • +

Replies To: Password module?

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,644
  • Joined: 16-October 07

Re: Password module?

Posted 07 July 2010 - 04:21 AM

View Postchemicalfan, on 07 July 2010 - 04:09 AM, said:

other than the *nix terminal


And why exactly would you want to be non standard? Truth is, strict terminal conformance doesn't do anything real fancy. Reading a character at a time isn't normal.

If you need fancy, you'll want the curses library.

For hash part, that's pretty easy. You store a hash, then take the password the user gives you and hash that. Looks something like this:
import hashlib
import getpass

# password for moe is 'password'
users = { 
	'larry' : 'acbd18db4cc2f85cedef654fccc4a4d8',
	'moe' : '5f4dcc3b5aa765d61d8327deb882cf99',
	}

def getHash(password):
	m = hashlib.md5()
	m.update(password)
	return m.hexdigest()
	
def showPass():
	password = getpass.getpass()
	print 'You entered:', password
	print "hash:'%s'" % getHash(password)

def userLogin():
	username = raw_input('Username:')
	password = getpass.getpass()
	if username in users and users[username]==getHash(password):
		print "Welcome ", username
		return True
	print 'Invalid username/password'

# showPass()

userLogin()


Was This Post Helpful? 1
  • +
  • -

#3 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: Password module?

Posted 07 July 2010 - 04:32 AM

My point about the *nix terminal, is that it is non-standard! It's the only place I can think of that doesn't display a character representing a keypress, when a user enters a password. All over Windows, and every website in the world functions this way, which has led me to believe that all of the developers aren't coding from scratch everytime (hence my search for a standard module!)

While your code is very neat and tidy (much better than I'd have come up with for myself on a whim!), I was rather hoping to avoid including that in my source. I'd rather just import the relevant functions & classes from this magical module I'm trying to find (which must exist, surely!)

Edit: Worst case scenario, I guess I could build the module myself from the example code you've given, but there's probably some other functionality I'd need, and I'd rather not re-invent the wheel if possible!

This post has been edited by chemicalfan: 07 July 2010 - 04:35 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,644
  • Joined: 16-October 07

Re: Password module?

Posted 07 July 2010 - 05:05 AM

Windows and Browsers aren't terminals. Actually, if you open up a terminal in windows and fire up something that prompts for a password, you probably wont find an echo. ( I know NET USE doesn't echo. )

So, what you're asking for isn't normally available in environment you're asking about. It you want something using a GUI interface, not locked into a console, then you'll just need to pick a graphic toolkit; python supports most of them. If you want to stay in the terminal, curses is really the only option.
Was This Post Helpful? 1
  • +
  • -

#5 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: Password module?

Posted 07 July 2010 - 05:30 AM

Sorry, I've just thought about that, and I should have mentioned it at the start - this app (the client side) will use Tkinter

Edit: Is it just a case of using event handlers to watch for the keypresses? I've seen some VB.NET code on the internet that suggests that's how it should be done for MSVS apps

This post has been edited by chemicalfan: 07 July 2010 - 05:32 AM

Was This Post Helpful? 0
  • +
  • -

#6 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Password module?

Posted 07 July 2010 - 05:43 AM

View Postbaavgai, on 07 July 2010 - 05:21 AM, said:

For hash part, that's pretty easy. You store a hash, then take the password the user gives you and hash that.


Don't forget to salt the hash!
Was This Post Helpful? 1
  • +
  • -

#7 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: Password module?

Posted 07 July 2010 - 05:52 AM

After a quick Google search, I've found the following example code which should cover the password base for me:
from Tkinter import *
from tkMessageBox import *

class EntryDemo( Frame ):
   def __init__( self ):
      Frame.__init__( self )
      self.pack( expand = YES, fill = BOTH )
      self.master.title( "Testing Entry Components" )
      self.master.geometry( "325x100" )

      self.frame1 = Frame( self )
      self.frame1.pack( pady = 5 )
      
      self.text4 = Entry( self.frame1, name = "text4", show = "*" )
      self.text4.insert( INSERT, "Hidden text" )
      self.text4.bind( "<Return>", self.showContents )
      self.text4.pack( side = LEFT, padx = 5 )


   def showContents( self, event ):
      theName = event.widget.winfo_name()

      theContents = event.widget.get()
      showinfo( "Message", theName + ": " + theContents )

EntryDemo().mainloop()


One problem down, many many more to go I suspect!

Thanks for you help all, rep has been given! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1