| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 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 |
|---|