<commit_before>from django.db import models
from django.core.exceptions import ValidationError

from mozdns.models import MozdnsRecord, LabelDomainMixin

import reversion

from gettext import gettext as _


def validate_algorithm(number):
    if number not in (1, 2):
        raise ValidationError(
            "Algorithm number must be with 1 (RSA) or 2 (DSA)")


def validate_fingerprint(number):
    if number not in (1,):
        raise ValidationError("Fingerprint type must be 1 (SHA-1)")


class SSHFP(MozdnsRecord, LabelDomainMixin):
    """
    >>> SSHFP(label=label, domain=domain, key=key_data,
    ... algorithm_number=algo_num, fingerprint_type=fing_type)
    """

    id = models.AutoField(primary_key=True)
    key = models.TextField()
    algorithm_number = models.PositiveIntegerField(
        null=False, blank=False, validators=[validate_algorithm],
        help_text="Algorithm number must be with 1 (RSA) or 2 (DSA)")
    fingerprint_type = models.PositiveIntegerField(
        null=False, blank=False, validators=[validate_fingerprint],
        help_text="Fingerprint type must be 1 (SHA-1)")

    template = _("{bind_name:$lhs_just} {ttl} {rdclass:$rdclass_just} "
                 "{rdtype:$rdtype_just} {algorithm_number} {fingerprint_type} "
                 "{key:$rhs_just}.")<commit_msg>added validator for sshfp hash<commit_after>from django.db import models
from django.core.exceptions import ValidationError

from mozdns.models import MozdnsRecord, LabelDomainMixin

import reversion

from gettext import gettext as _
import re


def validate_algorithm(number):
    if number not in (1, 2):
        raise ValidationError(
            "Algorithm number must be with 1 (RSA) or 2 (DSA)")


def validate_fingerprint(number):
    if number not in (1,):
        raise ValidationError("Fingerprint type must be 1 (SHA-1)")


is_sha1 = re.compile("[0-9a-fA-F]{40}")


def validate_sha1(sha1):
    if not is_sha1.match(sha1):
        raise ValidationError("Invalid key.")


class SSHFP(MozdnsRecord, LabelDomainMixin):
    """
    >>> SSHFP(label=label, domain=domain, key=key_data,
    ... algorithm_number=algo_num, fingerprint_type=fing_type)
    """

    id = models.AutoField(primary_key=True)
    key = models.CharField(max_length=256, validators=[validate_sha1])
    algorithm_number = models.PositiveIntegerField(
        null=False, blank=False, validators=[validate_algorithm],
        help_text="Algorithm number must be with 1 (RSA) or 2 (DSA)")
    fingerprint_type = models.PositiveIntegerField(
        null=False, blank=False, validators=[validate_fingerprint],
        help_text="Fingerprint type must be 1 (SHA-1)")

    template = _("{bind_name:$lhs_just} {ttl} {rdclass:$rdclass_just} "
                 "{rdtype:$rdtype_just} {algorithm_number} {fingerprint_type} "
                 "{key:$rhs_just}")
{'fraction_non_alphanumeric': 0.11001757469244289, 'fraction_numerical': 0.012302284710017574, 'mean_word_length': 3.8401360544217686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4083185', 'n_tokens_mistral': 932, 'n_tokens_neox': 873, 'n_words': 216}