human
I am working on the file "Bpid.ipynb".
The first task is:
ЭЦП
Реализация упрощенной электронной цифровой подписи.
$$H_i = (H_{i-1} + M_i)^2 \mod n,H_0 = 0$$
$$S = H ^ d \mod n$$
$$H' = S^e \mod n$$
Код алгоритма:
Can you write Python code for it?

=====
Give me a the Python code you requested?
assistant
#Here is the Python code you requested.

import re

class Signature(object):

    def __init__(self, public_key, private_key, n):
        self.public_key = public_key
        self.private_key = private_key
        self.n = n

    def sign(self, message, private_key):
        H = self.hash_function(message)
        signature = pow(H, private_key, self.n)
        return '@' + str(signature) + '@' + message

    def verify(self, message, public_key):
        regex = re.compile('@\d+@')
        match = regex.search(message)

        if match:
            signature = int(match.group(0).strip('@'))
            msg = regex.sub('', message)

            H1 = pow(signature, public_key, self.n)
            H2 = self.hash_function(msg)

            return H1 == H2

    def hash_function(self, message):
        H = 0
        for c in [ord(c) for c in message]:
            H = (H + c)**2 % self.n
        return H

    def get_public_key(self):
        return self.public_key

    def get_private_key(self):
        return self.private_key