#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import re

from lib.core.common import intelOpen
from lib.core.datatype import Password
from lib.request.cmd5 import cmd5

class md5(Password):
    """
    The MD5 algorithm is a widely used hash function producing a 128-bit
    hash value, see : https://en.wikipedia.org/wiki/MD5
    
    MD5("admin", 32) == 21232f297a57a5a743894a0e4a801fc3
    MD5("admin", 16) == 7a57a5a743894a0e
    """
    @classmethod
    def identify(cls, password):
        # check length
        if len(password) not in (16, 32):
            return False

        # check format
        if not re.fullmatch(r'[0-9a-fA-F]+', password):
            return False

        return True

    def request(self):
        return any((cmd5(self._password)))

    def crack(self):
        with intelOpen('data/password.txt', 'w') as f:
            f.write(self._password)
        os.system('sudo john --format=Raw-MD5 data/password.txt')
