Skip to content

Commit

Permalink
Added support for BigInteger fields - ref #169
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfarrell committed Nov 28, 2020
1 parent 36bcf18 commit eaa2eb4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Updated requirements_dev.txt
* Dropped support for Python 3.5
* Dropped support for Django below 2.2.x LTS release
* Added support for BigIntegerFields (#169)

## 2.5.2

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Supported PGP public key fields are:
- `DateTimePGPPublicKeyField`
- `TimePGPPublicKeyField`
- `IntegerPGPPublicKeyField`
- `BigIntegerPGPPublicKeyField`
- `DecimalPGPPublicKeyField`
- `FloatPGPPublicKeyField`

Expand All @@ -181,6 +182,7 @@ Supported PGP symmetric key fields are:
- `DateTimePGPSymmetricKeyField`
- `TimePGPSymmetricKeyField`
- `IntegerPGPSymmetricKeyField`
- `BigIntegerPGPSymerticKeyField`
- `DecimalPGPSymmetricKeyField`
- `FloatPGPSymmetricKeyField`

Expand All @@ -198,6 +200,7 @@ Encrypt and decrypt the data with `settings.PGCRYPTO_KEY` which acts like a pass
| `DateTimeField` | `DateTimePGPPublicKeyField` | `DateTimePGPSymmetricKeyField` |
| `TimeField` | `TimePGPPublicKeyField` | `TimePGPSymmetricKeyField` |
| `IntegerField` | `IntegerPGPPublicKeyField` | `IntegerPGPSymmetricKeyField` |
| `BigIntegerField` | `BigIntegerPGPPublicKeyField` | `BigIntegerPGPSymmetricKeyField` |
| `DecimalField` | `DecimalPGPPublicKeyField` | `DecimalPGPSymmetricKeyField` |
| `FloatField` | `FloatPGPPublicKeyField` | `FloatPGPSymmetricKeyField` |

Expand Down
12 changes: 12 additions & 0 deletions pgcrypto/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class IntegerPGPPublicKeyField(PGPPublicKeyFieldMixin, models.IntegerField):
cast_type = 'INT4'


class BigIntegerPGPPublicKeyField(PGPPublicKeyFieldMixin, models.IntegerField):
"""BigInteger PGP public key encrypted field."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'BIGINT'


class TextPGPPublicKeyField(PGPPublicKeyFieldMixin, models.TextField):
"""Text PGP public key encrypted field."""

Expand Down Expand Up @@ -78,6 +84,12 @@ class IntegerPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.IntegerField
cast_type = 'INT4'


class BigIntegerPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.IntegerField):
"""BigInteger PGP symmetric key encrypted field."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'BIGINT'


class TextPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.TextField):
"""Text PGP symmetric key encrypted field for postgres."""

Expand Down
2 changes: 2 additions & 0 deletions tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class EncryptedModelFactory(factory.django.DjangoModelFactory):

email_pgp_pub_field = factory.Sequence('email{}@public.key'.format)
integer_pgp_pub_field = 42
biginteger_pgp_pub_field = 9223372036854775807
pgp_pub_field = factory.Sequence('Text with public key {}'.format)
char_pub_field = factory.Sequence('Text {}'.format)

Expand All @@ -32,6 +33,7 @@ class EncryptedModelFactory(factory.django.DjangoModelFactory):

email_pgp_sym_field = factory.Sequence('email{}@symmetric.key'.format)
integer_pgp_sym_field = 43
biginteger_pgp_sym_field = 9223372036854775807
pgp_sym_field = factory.Sequence('Text with symmetric key {}'.format)
char_sym_field = factory.Sequence('Text {}'.format)

Expand Down
4 changes: 4 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class EncryptedModel(models.Model):
email_pgp_pub_field = fields.EmailPGPPublicKeyField(blank=True, null=True,
unique=True)
integer_pgp_pub_field = fields.IntegerPGPPublicKeyField(blank=True, null=True)
biginteger_pgp_pub_field = fields.BigIntegerPGPPublicKeyField(blank=True, null=True)
pgp_pub_field = fields.TextPGPPublicKeyField(blank=True, null=True)
char_pub_field = fields.CharPGPPublicKeyField(blank=True, null=True, max_length=15)
date_pgp_pub_field = fields.DatePGPPublicKeyField(blank=True, null=True)
Expand All @@ -43,6 +44,9 @@ class EncryptedModel(models.Model):

email_pgp_sym_field = fields.EmailPGPSymmetricKeyField(blank=True, null=True)
integer_pgp_sym_field = fields.IntegerPGPSymmetricKeyField(blank=True, null=True)
biginteger_pgp_sym_field = fields.BigIntegerPGPSymmetricKeyField(
blank=True, null=True
)
pgp_sym_field = fields.TextPGPSymmetricKeyField(blank=True, null=True)
char_sym_field = fields.CharPGPPublicKeyField(blank=True, null=True, max_length=15)
date_pgp_sym_field = fields.DatePGPSymmetricKeyField(blank=True, null=True)
Expand Down
32 changes: 28 additions & 4 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ def test_fields(self):
'hmac_with_original_field',
'email_pgp_pub_field',
'integer_pgp_pub_field',
'biginteger_pgp_pub_field',
'pgp_pub_field',
'char_pub_field',
'decimal_pgp_pub_field',
'email_pgp_sym_field',
'integer_pgp_sym_field',
'biginteger_pgp_sym_field',
'pgp_sym_field',
'char_sym_field',
'date_pgp_sym_field',
Expand All @@ -113,12 +115,14 @@ def test_value_returned_is_not_bytea(self):

self.assertIsInstance(instance.email_pgp_pub_field, str)
self.assertIsInstance(instance.integer_pgp_pub_field, int)
self.assertIsInstance(instance.biginteger_pgp_pub_field, int)
self.assertIsInstance(instance.pgp_pub_field, str)
self.assertIsInstance(instance.date_pgp_pub_field, date)
self.assertIsInstance(instance.datetime_pgp_pub_field, datetime)

self.assertIsInstance(instance.email_pgp_sym_field, str)
self.assertIsInstance(instance.integer_pgp_sym_field, int)
self.assertIsInstance(instance.biginteger_pgp_sym_field, int)
self.assertIsInstance(instance.pgp_sym_field, str)
self.assertIsInstance(instance.date_pgp_sym_field, date)
self.assertIsInstance(instance.datetime_pgp_sym_field, datetime)
Expand Down Expand Up @@ -311,19 +315,39 @@ def test_update_one_attribute(self):
self.assertEqual(updated_instance.first(), instance)

def test_pgp_public_key_negative_number(self):
"""Assert negative value is saved with an `IntegerPGPPublicKeyField` field."""
expected = -1
"""
Assert negative value is saved with Public Key integer fields.
* `IntegerPGPPublicKeyField`
* `BigIntegerPGPSymmetricKeyField`
"""
expected = -2147483648
instance = EncryptedModelFactory.create(integer_pgp_pub_field=expected)

self.assertEqual(instance.integer_pgp_pub_field, expected)

expected = -9223372036854775808
instance = EncryptedModelFactory.create(biginteger_pgp_pub_field=expected)

self.assertEqual(instance.biginteger_pgp_pub_field, expected)

def test_pgp_symmetric_key_negative_number(self):
"""Assert negative value is saved with an `IntegerPGPSymmetricKeyField` field."""
expected = -1
"""
Assert negative value is saved with Symmetric Key fields.
* `IntegerPGPSymmetricKeyField`
* `BigIntegerPGPSymmetricKeyField`
"""
expected = -2147483648
instance = EncryptedModelFactory.create(integer_pgp_sym_field=expected)

self.assertEqual(instance.integer_pgp_sym_field, expected)

expected = -9223372036854775808
instance = EncryptedModelFactory.create(biginteger_pgp_sym_field=expected)

self.assertEqual(instance.biginteger_pgp_sym_field, expected)

def test_pgp_symmetric_key_date(self):
"""Assert date is save with an `DatePGPSymmetricKeyField` field."""
expected = date.today()
Expand Down

0 comments on commit eaa2eb4

Please sign in to comment.