335a7a8707bf6d565bcef4892d77d0f210869555
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.2 2001-10-09 23:58:10 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, plaintext):
86 '''Compare this password against the plaintext.'''
87 if self.password is None:
88 raise ValueError, 'Password not set'
89 return cmp(self.password, encodePassword(plaintext, self.scheme))
91 def __str__(self):
92 '''Stringify the encrypted password for database storage.'''
93 if self.password is None:
94 raise ValueError, 'Password not set'
95 return '{%s}%s'%(self.scheme, self.password)
97 def test():
98 p = Password('sekrit')
99 assert p == 'sekrit'
100 assert p != 'not sekrit'
101 assert 'sekrit' == p
102 assert 'not sekrit' != p
104 if __name__ == '__main__':
105 test()
107 #
108 # $Log: not supported by cvs2svn $
109 # Revision 1.1 2001/10/09 07:25:59 richard
110 # Added the Password property type. See "pydoc roundup.password" for
111 # implementation details. Have updated some of the documentation too.
112 #
113 #
114 #
115 # vim: set filetype=python ts=4 sw=4 et si