MCPcopy Index your code
hub / github.com/HelloZeroNet/ZeroNet / Cipher

Class Cipher

src/lib/pyelliptic/cipher.py:10–84  ·  view source on GitHub ↗

Symmetric encryption import pyelliptic iv = pyelliptic.Cipher.gen_IV('aes-256-cfb') ctx = pyelliptic.Cipher("secretkey", iv, 1, ciphername='aes-256-cfb') ciphertext = ctx.update('test1') ciphertext += ctx.update('test2') ciphertext += ctx.final()

Source from the content-addressed store, hash-verified

8
9
10class Cipher:
11 """
12 Symmetric encryption
13
14 import pyelliptic
15 iv = pyelliptic.Cipher.gen_IV('aes-256-cfb')
16 ctx = pyelliptic.Cipher("secretkey", iv, 1, ciphername='aes-256-cfb')
17 ciphertext = ctx.update('test1')
18 ciphertext += ctx.update('test2')
19 ciphertext += ctx.final()
20
21 ctx2 = pyelliptic.Cipher("secretkey", iv, 0, ciphername='aes-256-cfb')
22 print ctx2.ciphering(ciphertext)
23 """
24 def __init__(self, key, iv, do, ciphername='aes-256-cbc'):
25 """
26 do == 1 => Encrypt; do == 0 => Decrypt
27 """
28 self.cipher = OpenSSL.get_cipher(ciphername)
29 self.ctx = OpenSSL.EVP_CIPHER_CTX_new()
30 if do == 1 or do == 0:
31 k = OpenSSL.malloc(key, len(key))
32 IV = OpenSSL.malloc(iv, len(iv))
33 OpenSSL.EVP_CipherInit_ex(
34 self.ctx, self.cipher.get_pointer(), 0, k, IV, do)
35 else:
36 raise Exception("RTFM ...")
37
38 @staticmethod
39 def get_all_cipher():
40 """
41 static method, returns all ciphers available
42 """
43 return OpenSSL.cipher_algo.keys()
44
45 @staticmethod
46 def get_blocksize(ciphername):
47 cipher = OpenSSL.get_cipher(ciphername)
48 return cipher.get_blocksize()
49
50 @staticmethod
51 def gen_IV(ciphername):
52 cipher = OpenSSL.get_cipher(ciphername)
53 return OpenSSL.rand(cipher.get_blocksize())
54
55 def update(self, input):
56 i = OpenSSL.c_int(0)
57 buffer = OpenSSL.malloc(b"", len(input) + self.cipher.get_blocksize())
58 inp = OpenSSL.malloc(input, len(input))
59 if OpenSSL.EVP_CipherUpdate(self.ctx, OpenSSL.byref(buffer),
60 OpenSSL.byref(i), inp, len(input)) == 0:
61 raise Exception("[OpenSSL] EVP_CipherUpdate FAIL ...")
62 return buffer.raw[0:i.value]
63
64 def final(self):
65 i = OpenSSL.c_int(0)
66 buffer = OpenSSL.malloc(b"", self.cipher.get_blocksize())
67 if (OpenSSL.EVP_CipherFinal_ex(self.ctx, OpenSSL.byref(buffer),

Callers 2

raw_encryptMethod · 0.85
decryptMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected