cipher pylint fixes

This commit is contained in:
lakshyacis 2019-09-23 17:51:58 +05:30
parent fa65b17fc9
commit e0d81bb7e8
No known key found for this signature in database
GPG Key ID: D2C539C8EC63E9EB
2 changed files with 15 additions and 3 deletions

View File

@ -1,5 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""
src/pyelliptic/cipher.py
========================
"""
# Copyright (C) 2011 Yann GUIBET <yannguibet@gmail.com> # Copyright (C) 2011 Yann GUIBET <yannguibet@gmail.com>
# See LICENSE for details. # See LICENSE for details.
@ -7,7 +11,8 @@
from openssl import OpenSSL from openssl import OpenSSL
class Cipher: # pylint: disable=redefined-builtin
class Cipher(object):
""" """
Symmetric encryption Symmetric encryption
@ -44,30 +49,34 @@ class Cipher:
@staticmethod @staticmethod
def get_blocksize(ciphername): def get_blocksize(ciphername):
"""This Method returns cipher blocksize"""
cipher = OpenSSL.get_cipher(ciphername) cipher = OpenSSL.get_cipher(ciphername)
return cipher.get_blocksize() return cipher.get_blocksize()
@staticmethod @staticmethod
def gen_IV(ciphername): def gen_IV(ciphername):
"""Generate random initialization vector"""
cipher = OpenSSL.get_cipher(ciphername) cipher = OpenSSL.get_cipher(ciphername)
return OpenSSL.rand(cipher.get_blocksize()) return OpenSSL.rand(cipher.get_blocksize())
def update(self, input): def update(self, input):
"""Update result with more data"""
i = OpenSSL.c_int(0) i = OpenSSL.c_int(0)
buffer = OpenSSL.malloc(b"", len(input) + self.cipher.get_blocksize()) buffer = OpenSSL.malloc(b"", len(input) + self.cipher.get_blocksize())
inp = OpenSSL.malloc(input, len(input)) inp = OpenSSL.malloc(input, len(input))
if OpenSSL.EVP_CipherUpdate(self.ctx, OpenSSL.byref(buffer), if OpenSSL.EVP_CipherUpdate(self.ctx, OpenSSL.byref(buffer),
OpenSSL.byref(i), inp, len(input)) == 0: OpenSSL.byref(i), inp, len(input)) == 0:
raise Exception("[OpenSSL] EVP_CipherUpdate FAIL ...") raise Exception("[OpenSSL] EVP_CipherUpdate FAIL ...")
return buffer.raw[0:i.value] return buffer.raw[0:i.value] # pylint: disable=invalid-slice-index
def final(self): def final(self):
"""Returning the final value"""
i = OpenSSL.c_int(0) i = OpenSSL.c_int(0)
buffer = OpenSSL.malloc(b"", self.cipher.get_blocksize()) buffer = OpenSSL.malloc(b"", self.cipher.get_blocksize())
if (OpenSSL.EVP_CipherFinal_ex(self.ctx, OpenSSL.byref(buffer), if (OpenSSL.EVP_CipherFinal_ex(self.ctx, OpenSSL.byref(buffer),
OpenSSL.byref(i))) == 0: OpenSSL.byref(i))) == 0:
raise Exception("[OpenSSL] EVP_CipherFinal_ex FAIL ...") raise Exception("[OpenSSL] EVP_CipherFinal_ex FAIL ...")
return buffer.raw[0:i.value] return buffer.raw[0:i.value] # pylint: disable=invalid-slice-index
def ciphering(self, input): def ciphering(self, input):
""" """
@ -77,6 +86,7 @@ class Cipher:
return buff + self.final() return buff + self.final()
def __del__(self): def __del__(self):
# pylint: disable=protected-access
if OpenSSL._hexversion > 0x10100000 and not OpenSSL._libreSSL: if OpenSSL._hexversion > 0x10100000 and not OpenSSL._libreSSL:
OpenSSL.EVP_CIPHER_CTX_reset(self.ctx) OpenSSL.EVP_CIPHER_CTX_reset(self.ctx)
else: else:

View File

@ -27,6 +27,7 @@ def _equals_str(a, b):
def equals(a, b): def equals(a, b):
"""Compare two strings or bytearrays"""
if isinstance(a, str): if isinstance(a, str):
return _equals_str(a, b) return _equals_str(a, b)
else: else:
@ -58,6 +59,7 @@ def hmac_sha512(k, m):
def pbkdf2(password, salt=None, i=10000, keylen=64): def pbkdf2(password, salt=None, i=10000, keylen=64):
"""Compute the salt, key and the message with pbkdf2"""
if salt is None: if salt is None:
salt = OpenSSL.rand(8) salt = OpenSSL.rand(8)
p_password = OpenSSL.malloc(password, len(password)) p_password = OpenSSL.malloc(password, len(password))