PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/gdata/tlslite/SharedKeyDB.py

http://radioappz.googlecode.com/
Python | 58 lines | 23 code | 7 blank | 28 comment | 2 complexity | 542ece8c3690716ac3b9ced5505a59a4 MD5 | raw file
 1"""Class for storing shared keys."""
 2
 3from utils.cryptomath import *
 4from utils.compat import *
 5from mathtls import *
 6from Session import Session
 7from BaseDB import BaseDB
 8
 9class SharedKeyDB(BaseDB):
10    """This class represent an in-memory or on-disk database of shared
11    keys.
12
13    A SharedKeyDB can be passed to a server handshake function to
14    authenticate a client based on one of the shared keys.
15
16    This class is thread-safe.
17    """
18
19    def __init__(self, filename=None):
20        """Create a new SharedKeyDB.
21
22        @type filename: str
23        @param filename: Filename for an on-disk database, or None for
24        an in-memory database.  If the filename already exists, follow
25        this with a call to open().  To create a new on-disk database,
26        follow this with a call to create().
27        """
28        BaseDB.__init__(self, filename, "shared key")
29
30    def _getItem(self, username, valueStr):
31        session = Session()
32        session._createSharedKey(username, valueStr)
33        return session
34
35    def __setitem__(self, username, sharedKey):
36        """Add a shared key to the database.
37
38        @type username: str
39        @param username: The username to associate the shared key with.
40        Must be less than or equal to 16 characters in length, and must
41        not already be in the database.
42
43        @type sharedKey: str
44        @param sharedKey: The shared key to add.  Must be less than 48
45        characters in length.
46        """
47        BaseDB.__setitem__(self, username, sharedKey)
48
49    def _setItem(self, username, value):
50        if len(username)>16:
51            raise ValueError("username too long")
52        if len(value)>=48:
53            raise ValueError("shared key too long")
54        return value
55
56    def _checkItem(self, value, username, param):
57        newSession = self._getItem(username, param)
58        return value.masterSecret == newSession.masterSecret