Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions management_interface/management_interface/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from .models import CareProviderLocation, CareRecipient, RegisteredManager


from os import getenv

class CareProviderLocationForm(forms.ModelForm):
class Meta:
model = CareProviderLocation
Expand All @@ -24,6 +26,32 @@ class Meta:
}


class HighSecurityHash:
# This version is intentionally slow, derives its security from
# being slow, and must be used as the default.
def encode(self, salt="", data="") -> str:
return scrypt(
data.encode(),
salt=str(salt).encode(),
n=32768,
r=12,
p=6,
maxmem=2**26,
).hex()

class NoSecurityHash:
# This version is fast and offers no security whatsoever, and must
# be actively selected only for testing purposes.
def encode(self, salt="", data="") -> str:
return scrypt(
data.encode(),
salt=str(salt).encode(),
n=16,
r=12,
p=1,
).hex()


class CareRecipientForm(forms.ModelForm):
given_name = forms.CharField(
max_length=64,
Expand Down Expand Up @@ -84,11 +112,9 @@ def _create_subscription(self) -> uuid.UUID:

def _generate_nhs_number_hash(self) -> str:
# https://nhsx.github.io/il-hans-infrastructure/adrs/003-Do-not-use-NEMS-or-MESH
return scrypt(
self.cleaned_data["nhs_number"].encode(),
salt=str(self.cleaned_data["birth_date"]).encode(),
n=32768,
r=12,
p=6,
maxmem=2**26,
).hex()
if getenv("STUPIDLY_HOBBLE_SECURITY") == "I_AM_AN_IDIOT_YES_I_REALLY_MEAN_IT":

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels bad to be doing an env var check here; where should this be?

hasher = NoSecurityHash()
else:
hasher = HighSecurityHash()
return hasher.encode(data=self.cleaned_data["nhs_number"],
salt=self.cleaned_data["birth_date"])