# Generated by Django 2.0 on 2018-02-20 10:53
from hashlib import sha256

from cryptography.fernet import Fernet
from django.conf import settings
from django.db import migrations


def backfill_plaintext_hashes(apps, schema_editor):
    SecretRevision = apps.get_model('secrets', 'SecretRevision')
    f = Fernet(settings.TEAMVAULT_SECRET_KEY)
    for srev in SecretRevision.objects.all():
        plaintext_data = f.decrypt(srev.encrypted_data.tobytes())
        srev.plaintext_data_sha256 = sha256(plaintext_data).hexdigest()
        srev.save()


class Migration(migrations.Migration):

    dependencies = [
        ('secrets', '0015_secretrevision_plaintext_data_sha256'),
    ]

    operations = [
        migrations.RunPython(backfill_plaintext_hashes),
    ]
