1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 #
18 # $Id: password.py,v 1.3 2001-10-20 11:58:48 richard Exp $
20 import sha, re
22 def encodePassword(plaintext, scheme):
23 '''Encrypt the plaintext password.
24 '''
25 if scheme == 'SHA':
26 s = sha.sha(plaintext).hexdigest()
27 elif scheme == 'plaintext':
28 pass
29 else:
30 raise ValueError, 'Unknown encryption scheme "%s"'%scheme
31 return s
33 class Password:
34 '''The class encapsulates a Password property type value in the database.
36 The encoding of the password is one if None, 'SHA' or 'plaintext'. The
37 encodePassword function is used to actually encode the password from
38 plaintext. The None encoding is used in legacy databases where no
39 encoding scheme is identified.
41 The scheme is stored with the encoded data in the database:
42 {scheme}data
44 Example usage:
45 >>> p = Password('sekrit')
46 >>> p == 'sekrit'
47 1
48 >>> p != 'not sekrit'
49 1
50 >>> 'sekrit' == p
51 1
52 >>> 'not sekrit' != p
53 1
54 '''
56 default_scheme = 'SHA' # new encryptions use this scheme
57 pwre = re.compile(r'{(\w+)}(.+)')
59 def __init__(self, plaintext=None):
60 '''Call setPassword if plaintext is not None.'''
61 if plaintext is not None:
62 self.password = encodePassword(plaintext, self.default_scheme)
63 self.scheme = self.default_scheme
64 else:
65 self.password = None
66 self.scheme = self.default_scheme
68 def unpack(self, encrypted):
69 '''Set the password info from the scheme:<encryted info> string
70 (the inverse of __str__)
71 '''
72 m = self.pwre.match(encrypted)
73 if m:
74 self.scheme = m.group(1)
75 self.password = m.group(2)
76 else:
77 # currently plaintext - encrypt
78 self.password = encodePassword(encrypted, self.default_scheme)
79 self.scheme = self.default_scheme
81 def setPassword(self, plaintext):
82 '''Sets encrypts plaintext.'''
83 self.password = encodePassword(plaintext, self.scheme)
85 def __cmp__(self, other):
86 '''Compare this password against another password.'''
87 # check to see if we're comparing instances
88 if isinstance(other, Password):
89 if self.scheme != other.scheme:
90 return
91 return cmp(self.password, other.password)
93 # assume password is plaintext
94 if self.password is None:
95 raise ValueError, 'Password not set'
96 return cmp(self.password, encodePassword(other, self.scheme))
98 def __str__(self):
99 '''Stringify the encrypted password for database storage.'''
100 if self.password is None:
101 raise ValueError, 'Password not set'
102 return '{%s}%s'%(self.scheme, self.password)
104 def test():
105 p = Password('sekrit')
106 assert p == 'sekrit'
107 assert p != 'not sekrit'
108 assert 'sekrit' == p
109 assert 'not sekrit' != p
111 if __name__ == '__main__':
112 test()
114 #
115 # $Log: not supported by cvs2svn $
116 # Revision 1.2 2001/10/09 23:58:10 richard
117 # Moved the data stringification up into the hyperdb.Class class' get, set
118 # and create methods. This means that the data is also stringified for the
119 # journal call, and removes duplication of code from the backends. The
120 # backend code now only sees strings.
121 #
122 # Revision 1.1 2001/10/09 07:25:59 richard
123 # Added the Password property type. See "pydoc roundup.password" for
124 # implementation details. Have updated some of the documentation too.
125 #
126 #
127 #
128 # vim: set filetype=python ts=4 sw=4 et si