diff --git a/fck/cktype/__init__.py b/fck/cktype/__init__.py index ee85f18..8622dfe 100644 --- a/fck/cktype/__init__.py +++ b/fck/cktype/__init__.py @@ -3,8 +3,10 @@ from typing import Dict, Optional, Type from fck.cktype.cktypeinterface import CKTYPEINTERFACE as CKTYPEINTERFACE from .crc32 import CRC32 +from .md5 import MD5 CKTYPES: Dict[str, Type[MD5 | CRC32]] = { + "MD5": (MD5), "CRC32": (CRC32), } diff --git a/fck/cktype/md5.py b/fck/cktype/md5.py new file mode 100644 index 0000000..a15caf4 --- /dev/null +++ b/fck/cktype/md5.py @@ -0,0 +1,26 @@ +from typing import List +from re import compile, Pattern +from hashlib import md5 + + +class MD5(object): + NAME: str = "MD5" + EXT: List[str] = [".md5"] + SYNTAX: List[Pattern] = [compile(r"^;*$"), compile(r"^.* [0-9a-fA-F]{8}$")] + REGEX: Pattern = compile(r"[0-9a-fA-F]{32}") + NULL: str = "D41D8CD98F00B204E9800998ECF8427E" + + def __init__(self): + self.cksum = md5(usedforsecurity=False) + + def gensum(self, data: bytes): + self.cksum.update(data) + + def reset(self): + self.__init__() + + def format(self, fpath) -> str: + return " ".join([self.__repr__(), fpath]) + + def __repr__(self) -> str: + return self.cksum.hexdigest().upper()