Package keyczar :: Module readers
[hide private]
[frames] | no frames]

Source Code for Module keyczar.readers

 1  #!/usr/bin/python2.4 
 2  # 
 3  # Copyright 2008 Google Inc. 
 4  # 
 5  # Licensed under the Apache License, Version 2.0 (the "License"); 
 6  # you may not use this file except in compliance with the License. 
 7  # You may obtain a copy of the License at 
 8  #  
 9  #      http://www.apache.org/licenses/LICENSE-2.0 
10  #  
11  # Unless required by applicable law or agreed to in writing, software 
12  # distributed under the License is distributed on an "AS IS" BASIS, 
13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14  # See the License for the specific language governing permissions and 
15  # limitations under the License. 
16   
17  """ 
18  A Reader supports reading metadata and key info for key sets. 
19   
20  @author: arkajit.dey@gmail.com (Arkajit Dey) 
21  """ 
22   
23  import os                 
24   
25  import util 
26   
27 -class Reader(object):
28 """Interface providing supported methods (no implementation).""" 29
30 - def GetMetadata(self):
31 """ 32 Return the KeyMetadata for the key set being read. 33 34 @return: JSON string representation of KeyMetadata object 35 @rtype: string 36 37 @raise KeyczarError: if unable to read metadata (e.g. IOError) 38 """
39
40 - def GetKey(self, version_number):
41 """ 42 Return the key corresponding to the given version. 43 44 @param version_number: the version number of the desired key 45 @type version_number: integer 46 47 @return: JSON string representation of a Key object 48 @rtype: string 49 50 @raise KeyczarError: if unable to read key info (e.g. IOError) 51 """
52
53 -class FileReader(Reader):
54 """Reader that reads key data from files.""" 55
56 - def __init__(self, location):
57 self._location = location
58
59 - def GetMetadata(self):
60 return util.ReadFile(os.path.join(self._location, "meta"))
61
62 - def GetKey(self, version_number):
63 return util.ReadFile(os.path.join(self._location, str(version_number)))
64
65 -class EncryptedReader(Reader):
66 """Reader that reads encrypted key data from files.""" 67
68 - def __init__(self, reader, crypter):
69 self._reader = reader 70 self._crypter = crypter
71
72 - def GetMetadata(self):
73 return self._reader.GetMetadata()
74
75 - def GetKey(self, version_number):
76 return self._crypter.Decrypt(self._reader.GetKey(version_number))
77