root/museek+/trunk/sources/Mucipher/python/mucipher.py

Revision 2, 2.1 kB (checked in by daelstorm, 2 years ago)

Inital commit

Line 
1 # Mucipher - Cryptograhic library for Museek
2 #
3 # Copyright (C) 2003-2004 Hyriand <hyriand@thegraveyard.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19 import mucipherc
20
21 class CipherContext:
22         def __init__(self, key, alg = "SHA256"):
23                 self.__context__ = mucipherc.malloc_CipherContext()
24                
25                 if alg not in ["SHA256", "MD5"]:
26                         raise RuntimeError, "Invalid key generation algorithm"
27                
28                 if alg == "MD5":
29                         mucipherc.cipherKeyMD5(self.__context__, key)
30                 else:
31                         mucipherc.cipherKeySHA256(self.__context__, key)
32        
33         def __call__(self):
34                 return self.__context__
35        
36         def __del__(self):
37                 import mucipherc
38                 mucipherc.free_CipherContext(self.__context__)
39
40 class Cipher:
41         def __init__(self, key, alg = "SHA256"):
42                 self.ctx_encode = CipherContext(key, alg)
43                 self.ctx_decode = CipherContext(key, alg)
44                
45         def cipher(self, data):
46                 return mucipherc._blockCipher(self.ctx_encode(), data)
47        
48         def decipher(self, data):
49                 return mucipherc._blockDecipher(self.ctx_decode(), data)
50
51 class shaBlock:
52         hextab = "0123456789abcdef"
53         hasher = mucipherc.shaBlock
54        
55         def __init__(self, text):
56                 self.text = text
57        
58         def __str__(self):
59                 return self.hasher(self.text)
60        
61         def hexdigest(self):
62                 ret = ""
63                 temp = str(self)
64                 for i in temp:
65                         ret = ret + self.hextab[ord(i) >> 4] + self.hextab[ord(i) & 0x0f]
66                 return ret
67        
68         def __repr__(self):
69                 return self.hexdigest()
70
71 class md5Block(shaBlock):
72         hasher = mucipherc.md5Block
73
74 class sha256Block(shaBlock):
75         hasher = mucipherc.sha256Block
Note: See TracBrowser for help on using the browser.