pygcrypt
Advanced tools
+1
-1
| Metadata-Version: 1.1 | ||
| Name: pygcrypt | ||
| Version: 0.9.9.post26 | ||
| Version: 0.9.9.post28 | ||
| Summary: libgcrypt in Python | ||
@@ -5,0 +5,0 @@ Home-page: https://phab.okhin.fr/diffusion/PGCRY/ |
| Metadata-Version: 1.1 | ||
| Name: pygcrypt | ||
| Version: 0.9.9.post26 | ||
| Version: 0.9.9.post28 | ||
| Summary: libgcrypt in Python | ||
@@ -5,0 +5,0 @@ Home-page: https://phab.okhin.fr/diffusion/PGCRY/ |
+49
-34
@@ -110,42 +110,57 @@ #!/usr/bin/env python | ||
| def __setattr__(self, item, value): | ||
| """ | ||
| Let's define some parameters for the context or the cipher. | ||
| """ | ||
| error = ffi.cast("gcry_error_t", 0) | ||
| if item == 'cipher': | ||
| if not isinstance(value, ciphers.Cipher): | ||
| raise KeyError("{} must be an instance of ciphers.Cipher, {} given.".format(item, type(value))) | ||
| super(Context, self).__setattr__(item, value) | ||
| return | ||
| @property | ||
| def version(self): | ||
| return ffi.string(lib.gcry_check_version(ffi.NULL)).decode() | ||
| if item in ['key', 'iv', 'ctr']: | ||
| # Let's define the key used by the cipher" | ||
| if self.cipher: | ||
| self.cipher.item = value | ||
| return | ||
| super(Context, self).__setattr__(item, value) | ||
| @property | ||
| def algo(self): | ||
| if self.cipher: | ||
| return self.cipher.algo | ||
| else: | ||
| raise KeyError("No ciphers initialized.") | ||
| def __getattr__(self, item): | ||
| """ | ||
| Let's get a specific item for the class | ||
| """ | ||
| if item == 'version': | ||
| return ffi.string(lib.gcry_check_version(ffi.NULL)).decode() | ||
| @property | ||
| def mode(self): | ||
| if self.cipher: | ||
| return self.cipher.mode | ||
| else: | ||
| raise KeyError("No ciphers initailized.") | ||
| if item == 'algo': | ||
| if self.cipher: | ||
| return self.cipher.algo | ||
| else: | ||
| raise KeyError("No ciphers initialized.") | ||
| @property | ||
| def key(self): | ||
| return getattr(self.cipher, 'key', None) | ||
| if item == 'mode': | ||
| if self.cipher: | ||
| return self.cipher.mode | ||
| else: | ||
| raise KeyError("No ciphers initailized.") | ||
| @key.setter | ||
| def key(self, value): | ||
| if self.cipher: | ||
| self.cipher.key = value | ||
| if item in ['key', 'iv', 'ctr']: | ||
| return getattr(self.cipher, item, None) | ||
| @property | ||
| def iv(self): | ||
| return getattr(self.cipher, 'iv', None) | ||
| @iv.setter | ||
| def iv(self, value): | ||
| if self.cipher: | ||
| self.cipher.iv = value | ||
| @property | ||
| def ctr(self): | ||
| return getattr(self.cipher, 'ctr', None) | ||
| @ctr.setter | ||
| def ctr(self, value): | ||
| if self.cipher: | ||
| self.cipher.ctr = value | ||
| @property | ||
| def cipher(self): | ||
| return self._cipher | ||
| @cipher.setter | ||
| def cipher(self, value): | ||
| if not isinstance(value, ciphers.Cipher): | ||
| raise KeyError("{} must be an instance of ciphers.Cipher, {} given.".format(item, type(value))) | ||
| self._cipher = value | ||
| def mpi(self, obj): | ||
@@ -152,0 +167,0 @@ """ |
@@ -55,5 +55,5 @@ #!/usr/bin/env python | ||
| assert ((point * a).x, (point * a).y, (point * a).z) == (-(x * 8), -(y * 8), a) | ||
| assert ((point * a).x.value, (point * a).y.value, (point * a).z.value) == (6277101735386680763835789423207666416083908700390324961271, 6277101735386680763835789423207666416083908700390324961271, 2) | ||
| point *= a | ||
| assert (point.x, point.y, point.z) == (-(x * 8), -(y * 8), a) | ||
| assert (point.x.value, point.y.value, point.z.value) == (6277101735386680763835789423207666416083908700390324961271, 6277101735386680763835789423207666416083908700390324961271, 2) | ||
@@ -72,5 +72,5 @@ def test_add(context): | ||
| assert ((point_a + point_b).x, (point_a + point_b).y, (point_a + point_b).z) == (-(x * 8), -(y * 8), (z * 2)) | ||
| assert ((point_a + point_b).x.value, (point_a + point_b).y.value, (point_a + point_b).z.value) == (6277101735386680763835789423207666416083908700390324961271, 6277101735386680763835789423207666416083908700390324961271, 2) | ||
| point_a += point_b | ||
| assert (point_a.x, point_a.y, point_a.z) == (-(x * 8), -(y * 8), (z * 2)) | ||
| assert (point_a.x.value, point_a.y.value, point_a.z.value) == (6277101735386680763835789423207666416083908700390324961271, 6277101735386680763835789423207666416083908700390324961271, 2) | ||
@@ -88,3 +88,3 @@ def test_double(context): | ||
| point.double() | ||
| assert (point.x, point.y, point.z) == (-(x*8), -(y*8), z*2) | ||
| assert (point.x.value, point.y.value, point.z.value) == (6277101735386680763835789423207666416083908700390324961271, 6277101735386680763835789423207666416083908700390324961271, 2) | ||
@@ -91,0 +91,0 @@ def test_oncurve(context): |
@@ -26,3 +26,2 @@ #!/usr/bin/env python | ||
| assert public.issane() == False | ||
| assert True == False | ||
@@ -55,5 +54,5 @@ def test_algo(context, private, public): | ||
| sig = private.makesign(data) | ||
| assert str(sig) == '(sig-val \n (rsa \n (s #0DB6FD3B8915538E0C3619B32F47BBB2990E36F3E0ACBBA80E58BF317079928575EB06ED86247FD988A25F906BC4547983422DF30ACB64DE416D1144EEAEC08AD8DCDBA5E7A21956CECE6549CBBFA5B1D8E80994EAAEA266C4FEEBD7A613BDA1636487FA4F3FED102BF79779B8B6056F02D76F79BE96F64B555B578A0D8816535C2F6915D88ADE601FDD5D03862CB8240C4DA4A66AAE510B995EB5C76D6B62867FBB27D1BBEFE4A5BA77D0357035873F189B2FFE718F3BD164ACCCB5355D316C1A80B8BFF324AAB148854D4E1F40DF224DF7DFD7ADA056D343A0B81F9E6DCA6AD9C92924BD36C6C2D7688C94EC8DB45F3B3E38BFCA1B7475E29BF9442DCDEC154AD816AFEC1F7EE23A9228E40DB20248951FE2CE6E4AF9C546C7C108A862B58C99BD70AE72D73A8BCA00CC8A4501DC7FF5EEA7533BC065454FDFB270242DE7981B050DAFBE682A9F498F445AB6EA1ED20B1AF5BFBEE646214DF2CC214E6D3B6E59925600CDED856A49C0F5899669107A5D957E675BC94A53C32E476CE64B0DB77DDB4320179B5D36682F017FE9D8FA5D18A9C9955210F60AA4A18737AECAF9CA2DC9D938A1F73ECF274276FE3F8B9BC4879B75617F2E7AD8485FB71C7B10B2F0DCE6F2F0CE3FF1303145B7D11EE281B6A18A6F02B8E787A4956046BBE5AC5E47BF695712BAE9FC32A8881CB53D8881973B6C70AA648289A5DDB98EFF5C14A061#)\n )\n )\n' | ||
| assert sig == SExpression(b'(sig-val \n (rsa \n (s #0DB6FD3B8915538E0C3619B32F47BBB2990E36F3E0ACBBA80E58BF317079928575EB06ED86247FD988A25F906BC4547983422DF30ACB64DE416D1144EEAEC08AD8DCDBA5E7A21956CECE6549CBBFA5B1D8E80994EAAEA266C4FEEBD7A613BDA1636487FA4F3FED102BF79779B8B6056F02D76F79BE96F64B555B578A0D8816535C2F6915D88ADE601FDD5D03862CB8240C4DA4A66AAE510B995EB5C76D6B62867FBB27D1BBEFE4A5BA77D0357035873F189B2FFE718F3BD164ACCCB5355D316C1A80B8BFF324AAB148854D4E1F40DF224DF7DFD7ADA056D343A0B81F9E6DCA6AD9C92924BD36C6C2D7688C94EC8DB45F3B3E38BFCA1B7475E29BF9442DCDEC154AD816AFEC1F7EE23A9228E40DB20248951FE2CE6E4AF9C546C7C108A862B58C99BD70AE72D73A8BCA00CC8A4501DC7FF5EEA7533BC065454FDFB270242DE7981B050DAFBE682A9F498F445AB6EA1ED20B1AF5BFBEE646214DF2CC214E6D3B6E59925600CDED856A49C0F5899669107A5D957E675BC94A53C32E476CE64B0DB77DDB4320179B5D36682F017FE9D8FA5D18A9C9955210F60AA4A18737AECAF9CA2DC9D938A1F73ECF274276FE3F8B9BC4879B75617F2E7AD8485FB71C7B10B2F0DCE6F2F0CE3FF1303145B7D11EE281B6A18A6F02B8E787A4956046BBE5AC5E47BF695712BAE9FC32A8881CB53D8881973B6C70AA648289A5DDB98EFF5C14A061#)\n )\n )\n') | ||
| assert public.verify(sig, data) == True | ||
| with pytest.raises(NotImplementedError): | ||
| private.verify(sig, data) |
@@ -6,3 +6,3 @@ #!/usr/bin/env python | ||
| from pygcrypt.types.sexpression import SExpression | ||
| from pygcrypt.types.mpi import MPIopaque | ||
| from pygcrypt.types.mpi import MPIopaque, MPIint | ||
@@ -53,6 +53,4 @@ def test_init(context): | ||
| iter_expr = [sexp for sexp in s_expr] | ||
| print(iter_expr) | ||
| assert iter_expr == [SExpression(b'(test)'), SExpression(b'(inside (a "123") (b "-123"))'), SExpression(b'(longitem "Ohai world")')] | ||
| def test_contains(context): | ||
@@ -75,8 +73,20 @@ s_expr = SExpression(b'(test (a "123") (b "-123") (longitem "Ohai world"))') | ||
| def test_eq(context): | ||
| s_expr_a = SExpression(b'(test (a "123") (b"-123"))') | ||
| s_expr_b = SExpression(b'(test (a "123") (b"-123"))') | ||
| s_expr_c = SExpression(b'(test (a "123") (b"-124"))') | ||
| assert s_expr_a == s_expr_b | ||
| assert s_expr_a != s_expr_c | ||
| assert s_expr_a['a'] == s_expr_c['a'] | ||
| def test_fromdict(context): | ||
| dict_a = {'a': MPIint(1)} | ||
| dict_b = {'a': "123"} | ||
| dict_c = {'a': "Hello World"} | ||
| dict_d = {'a': {'b': "Test", 'c': 123}} | ||
| assert str(SExpression.from_dict(dict_a)) == str(SExpression(b'(a %m)', MPIint(1))) | ||
| assert str(SExpression.from_dict(dict_b)) == str(SExpression(b'(a %s)', "123")) | ||
| assert str(SExpression.from_dict(dict_c)) == str(SExpression(b'(a %b)', 11, "Hello World")) | ||
| assert str(SExpression.from_dict(dict_d)) == str(SExpression(b'(a (b %b)(c %d))', 4, "Test", 123)) | ||
| def test_todict(context): | ||
| dict_a = {'a': MPIint(1)} | ||
| dict_b = {'a': "123"} | ||
| dict_c = {'a': "Hello World"} | ||
| dict_d = {'a': {'b': "Test", 'c': 123}} | ||
| assert dict_a == SExpression(b'(a %m)', MPIint(1)).to_dict() | ||
| assert dict_b == SExpression(b'(a %s)', "123").to_dict() | ||
| assert dict_c == SExpression(b'(a %b)', 11, "Hello World").to_dict() | ||
| assert dict_d == SExpression(b'(a (b %b)(c %d))', 4, "Test", 123).to_dict() |
@@ -27,3 +27,3 @@ #!/usr/bin/env python | ||
| def __del__(self): | ||
| lib.gcry_mpi_point_release(self.__point) | ||
| lib.gcry_mpi_point_release(self.point) | ||
@@ -67,3 +67,3 @@ def __repr__(self): | ||
| lib.gcry_mpi_point_set(self.point, value.mpi, self.y.mpi, self.z.mpi) | ||
| return | ||
| return | ||
| elif key == 'y': | ||
@@ -73,3 +73,3 @@ if not isinstance(value, MPIint): | ||
| lib.gcry_mpi_point_set(self.point, self.x.mpi, value.mpi, self.z.mpi) | ||
| return | ||
| return | ||
| elif key == 'z': | ||
@@ -79,3 +79,3 @@ if not isinstance(value, MPIint): | ||
| lib.gcry_mpi_point_set(self.point, self.x.mpi, self.y.mpi, value.mpi) | ||
| return | ||
| return | ||
| super(Point, self).__setattr__(key, value) | ||
@@ -124,3 +124,3 @@ | ||
| if not isinstance(mpi, MPIint): | ||
| raise TypeError("Can only multuply a point by a MPIint, got {} instead.".format(type(point))) | ||
| raise TypeError("Can only multiply a point by a MPIint, got {} instead.".format(type(point))) | ||
@@ -127,0 +127,0 @@ point_dest = lib.gcry_mpi_point_new(0) |
@@ -41,7 +41,12 @@ #!/usr/bin/env python | ||
| try: | ||
| sub_sexp = sexp['public-key'] | ||
| self.type = u'public' | ||
| self.sexp = SExpression(b'(public-key %S)', sub_sexp) | ||
| except: | ||
| raise TypeError("The provided S-Expression does not contains key data.") | ||
| sub_sexp = sexp['protected-private-key'] | ||
| self.type = u'private' | ||
| self.sexp = SExpression(b'(protected-private-key %S)', sub_sexp) | ||
| except KeyError: | ||
| try: | ||
| sub_sexp = sexp['public-key'] | ||
| self.type = u'public' | ||
| self.sexp = SExpression(b'(public-key %S)', sub_sexp) | ||
| except: | ||
| raise TypeError("The provided S-Expression does not contains key data.") | ||
| except: | ||
@@ -48,0 +53,0 @@ raise |
+50
-57
@@ -223,36 +223,34 @@ #!/usr/bin/env python | ||
| def __getattr__(self, attr): | ||
| if attr == 'value': | ||
| #This will return the value of the MPI object as an int. | ||
| error = ffi.cast("gcry_error_t", 0) | ||
| fmt = getattr(lib, u'GCRYMPI_FMT_' + self.fmt) | ||
| size = ffi.new("size_t *", 0) | ||
| error = lib.gcry_mpi_print(fmt, ffi.NULL, 0, size, self.mpi) | ||
| if error > 0: | ||
| raise errors.GcryptException(ffi.string(lib.gcry_strerror(error)).decode(), error) | ||
| @property | ||
| def value(self): | ||
| #This will return the value of the MPI object as an int. | ||
| error = ffi.cast("gcry_error_t", 0) | ||
| fmt = getattr(lib, u'GCRYMPI_FMT_' + self.fmt) | ||
| size = ffi.new("size_t *", 0) | ||
| error = lib.gcry_mpi_print(fmt, ffi.NULL, 0, size, self.mpi) | ||
| if error > 0: | ||
| raise errors.GcryptException(ffi.string(lib.gcry_strerror(error)).decode(), error) | ||
| out = ffi.new("unsigned char [{}]".format(size[0]), b'') | ||
| error = lib.gcry_mpi_print(fmt, out, size[0], size, self.mpi) | ||
| if error > 0: | ||
| raise errors.GcryptException(ffi.string(lib.gcry_strerror(error)).decode(), error) | ||
| out = ffi.new("unsigned char [{}]".format(size[0]), b'') | ||
| error = lib.gcry_mpi_print(fmt, out, size[0], size, self.mpi) | ||
| if error > 0: | ||
| raise errors.GcryptException(ffi.string(lib.gcry_strerror(error)).decode(), error) | ||
| value = 0 | ||
| for item in struct.unpack('{}B'.format(size[0]), ffi.buffer(out, size[0])): | ||
| value <<= 8 | ||
| value ^= item | ||
| if lib.gcry_mpi_is_neg(ffi.cast("const gcry_mpi_t", self.mpi)) == 1: | ||
| return -value | ||
| return value | ||
| return super(MPIint, self).__getattr__(attr) | ||
| value = 0 | ||
| for item in struct.unpack('{}B'.format(size[0]), ffi.buffer(out, size[0])): | ||
| value <<= 8 | ||
| value ^= item | ||
| if lib.gcry_mpi_is_neg(ffi.cast("const gcry_mpi_t", self.mpi)) == 1: | ||
| return -value | ||
| return value | ||
| def __setattr__(self, attr, value): | ||
| if attr == 'value': | ||
| # We're creating a new MPI assigned with value, and then we move our current mpi value | ||
| # to its cdata object | ||
| if value >= 0: | ||
| self.mpi = lib.gcry_mpi_set_ui(self.mpi, ffi.cast("unsigned int", value)) | ||
| else: | ||
| self.mpi = lib.gcry_mpi_set_ui(self.mpi, ffi.cast("unsigned int", -value)) | ||
| self.__invert__() | ||
| super(MPIint, self).__setattr__(attr, value) | ||
| @value.setter | ||
| def value(self, value): | ||
| # We're creating a new MPI assigned with value, and then we move our current mpi value | ||
| # to its cdata object | ||
| if value >= 0: | ||
| self.mpi = lib.gcry_mpi_set_ui(self.mpi, ffi.cast("unsigned long", value)) | ||
| else: | ||
| self.mpi = lib.gcry_mpi_set_ui(self.mpi, ffi.cast("unsigned long", -value)) | ||
| self.__invert__() | ||
@@ -479,27 +477,22 @@ def __invert__(self): | ||
| def __setattr__(self, attr, value): | ||
| if attr == 'value': | ||
| #Set the value of the MPI to hold the arbitrary bit pattern to be | ||
| #a reflection of the python object opaque. The number of bits used | ||
| #by the object is passed to the functions. | ||
| _mpi = lib.gcry_mpi_set_opaque(self.mpi, value, ffi.cast("unsigned int", len(value)*8)) | ||
| self.mpi = _mpi | ||
| return | ||
| super(MPIopaque, self).__setattr__(attr, value) | ||
| def __getattr__(self, attr): | ||
| super(MPIopaque, self).__getattr__(attr) | ||
| if attr == 'value': | ||
| if not self.opaque: | ||
| raise TypeError("Trying to get an opaque value from a non opaque MPI") | ||
| bits = ffi.new("unsigned int *", 0) | ||
| _mpi = self.mpi | ||
| # The function return a string containing the adress of the data | ||
| void = ffi.cast("char *", lib.gcry_mpi_get_opaque(_mpi, bits)) | ||
| nbytes = bits[0] // 8 | ||
| if bits[0] % 8 != 0: | ||
| nbytes += 1 | ||
| ret_val = ffi.buffer(void, nbytes)[0:nbytes] | ||
| return ret_val | ||
| return super(MPIopaque, self).__getattr__(attr) | ||
| @property | ||
| def value(self): | ||
| if not self.opaque: | ||
| raise TypeError("Trying to get an opaque value from a non opaque MPI") | ||
| bits = ffi.new("unsigned int *", 0) | ||
| _mpi = self.mpi | ||
| # The function return a string containing the adress of the data | ||
| void = ffi.cast("char *", lib.gcry_mpi_get_opaque(_mpi, bits)) | ||
| nbytes = bits[0] // 8 | ||
| if bits[0] % 8 != 0: | ||
| nbytes += 1 | ||
| ret_val = ffi.buffer(void, nbytes)[0:nbytes] | ||
| return ret_val | ||
| @value.setter | ||
| def value(self, value): | ||
| #Set the value of the MPI to hold the arbitrary bit pattern to be | ||
| #a reflection of the python object opaque. The number of bits used | ||
| #by the object is passed to the functions. | ||
| _mpi = lib.gcry_mpi_set_opaque(self.mpi, value, ffi.cast("unsigned int", len(value)*8)) | ||
| self.mpi = _mpi |
@@ -191,2 +191,32 @@ #!/usr/bien/env python | ||
| @classmethod | ||
| def dict_constructor(cls, expression, constructor=b''): | ||
| for key in expression: | ||
| # Let's get the type of the item | ||
| if isinstance(expression[key], MPI): | ||
| constructor += b'(%s #%s#)' % (key.encode(), expression[key].to_bytes().hex().encode()) | ||
| elif isinstance(expression[key], str): | ||
| constructor += b'(%s "%s")' % (key.encode(), expression[key].encode()) | ||
| elif isinstance(expression[key], bytes): | ||
| constructor += b'(%s "%s")' % (key.encode(), expression[key]) | ||
| elif isinstance(expression[key], int): | ||
| constructor += b'(%s "%s")' % (key.encode(), repr(expression[key]).encode()) | ||
| elif isinstance(expression[key], dict): | ||
| constructor += cls.dict_constructor(expression[key], constructor) | ||
| constructor = b'(%s %s)' % (key.encode(), constructor) | ||
| else: | ||
| raise Exception("Unsupported data type {}".format(expression[key],)) | ||
| return constructor | ||
| @classmethod | ||
| def from_dict(cls, expression): | ||
| """ | ||
| Let's try to create a SExpression from a dict. The function will parse the (key, value) pair | ||
| of the expression given as a dict, and try to assemble a string suitable to be given as a | ||
| SExpression constructor, usually a bytes of the form b'(key value)' | ||
| The function will then return a SExpression. | ||
| """ | ||
| return cls(cls.dict_constructor(expression)) | ||
| def dump(self): | ||
@@ -193,0 +223,0 @@ """ |
+1
-1
| [egg_info] | ||
| tag_build = | ||
| tag_date = 0 | ||
| tag_svn_revision = 0 | ||
| tag_build = | ||
+1
-1
@@ -28,3 +28,3 @@ #!/usr/bin/env python | ||
| # https://packaging.python.org/en/latest/single_source_version.html | ||
| version='0.9.9-26', | ||
| version='0.9.9-28', | ||
@@ -31,0 +31,0 @@ description='libgcrypt in Python', |
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
226708
1.07%4039
1.13%