[Pymoul-svn] SF.net SVN: pymoul: [284] pymoul/trunk/src/moul/crypt/stream.py
Status: Alpha
Brought to you by:
tiran
From: <ti...@us...> - 2007-04-18 21:43:17
|
Revision: 284 http://pymoul.svn.sourceforge.net/pymoul/?rev=284&view=rev Author: tiran Date: 2007-04-18 14:43:14 -0700 (Wed, 18 Apr 2007) Log Message: ----------- Added encrypted stream class Added Paths: ----------- pymoul/trunk/src/moul/crypt/stream.py Added: pymoul/trunk/src/moul/crypt/stream.py =================================================================== --- pymoul/trunk/src/moul/crypt/stream.py (rev 0) +++ pymoul/trunk/src/moul/crypt/stream.py 2007-04-18 21:43:14 UTC (rev 284) @@ -0,0 +1,72 @@ +# pyMoul - Python interface to Myst Online URU Live +# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de> + +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., 59 +# Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +"""Encrypted file stream +""" +__author__ = "Christian Heimes" +__version__ = "$Id: elf.py 275 2007-03-21 12:39:25Z tiran $" +__revision__ = "$Revision: 275 $" + +import os + +from moul.crypt.binary import BinaryFile + +class HeaderError(ValueError): + pass + +class EncryptedStream(object): + """Encrypted stream + """ + magic = 12 * '\x00' + cipherClass = None + blockSize = 1024 + + def __init__(self, fdname, key, **kwargs): + if isinstane(fdname, basestring): + fd = BinaryFile(fdname) + elif isinstance(fdname, BinaryFile): + fd = fdname + else: + raise TypeError(fdname) + header = fd.read(12) + if header != self.magic: + HeaderError(header) + self._size = fd.read32() + fd.seek(0, 2) + fsize = fd.tell() + fd.seek(16) + if fsize-16 < self._size: + raise ValueError("Size mismatch %i/%i" % (fsize, self._size)) + self._cipher = self.cipherClass(key, **kwargs) + self._fd = fd + + def read(self): + fd.seek(16) + remain = self._size + bs = self.blockSize + fd = self._fd + decrypt = self._cipher.decrypt + buf = [] + while True: + if remain == 0: + break + if remain < bs: + bs = remain + data = fd.read(bs) + buf.append(decrypt(data)) + return ''.join(buf) + Property changes on: pymoul/trunk/src/moul/crypt/stream.py ___________________________________________________________________ Name: svn:keywords + 'Id Revision' Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |