Python Crypto.Random.atfork() Examples

The following are 16 code examples of Crypto.Random.atfork(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module Crypto.Random , or try the search function .
Example #1
Source File: dataset.py    From attention-ocr with MIT License 6 votes vote down vote up
def __getitem__(self, item):
        if self.first_run:
            Random.atfork()
            self.first_run = False

        content = [random.randrange(0, len(self.chars)) for _ in range(self.n_chars)]

        s = ''.join([self.chars[i] for i in content])

        d = self.gen.generate(s)
        d = Image.open(d)

        label = torch.full((self.n_chars + 2, ), self.tokenizer.EOS_token, dtype=torch.long)

        ts = self.tokenizer.tokenize(s)
        label[:ts.shape[0]] = torch.tensor(ts)

        return img_trans(d), label 
Example #2
Source File: pybar.py    From BAR with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def gen_key():
    '''
    Generates a new label or shared key.
    '''

    rpool =  Random.new()
    Random.atfork() 
    return rpool.read(16).encode("hex") 
Example #3
Source File: label.py    From BAR with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def gen_lbl():
    '''
    Generate a new Label.
    '''

    rpool =  Random.new()
    Random.atfork() 
    return rpool.read(16).encode("hex") 
Example #4
Source File: gunicorn_local.py    From quay with Apache License 2.0 5 votes vote down vote up
def post_fork(server, worker):
    # Reset the Random library to ensure it won't raise the "PID check failed." error after
    # gunicorn forks.
    Random.atfork() 
Example #5
Source File: gunicorn_web.py    From quay with Apache License 2.0 5 votes vote down vote up
def post_fork(server, worker):
    # Reset the Random library to ensure it won't raise the "PID check failed." error after
    # gunicorn forks.
    Random.atfork() 
Example #6
Source File: gunicorn_local.py    From quay with Apache License 2.0 5 votes vote down vote up
def post_fork(server, worker):
    # Reset the Random library to ensure it won't raise the "PID check failed." error after
    # gunicorn forks.
    Random.atfork() 
Example #7
Source File: gunicorn_web.py    From quay with Apache License 2.0 5 votes vote down vote up
def post_fork(server, worker):
    # Reset the Random library to ensure it won't raise the "PID check failed." error after
    # gunicorn forks.
    Random.atfork() 
Example #8
Source File: gunicorn_registry.py    From quay with Apache License 2.0 5 votes vote down vote up
def post_fork(server, worker):
    # Reset the Random library to ensure it won't raise the "PID check failed." error after
    # gunicorn forks.
    Random.atfork() 
Example #9
Source File: gunicorn_verbs.py    From quay with Apache License 2.0 5 votes vote down vote up
def post_fork(server, worker):
    # Reset the Random library to ensure it won't raise the "PID check failed." error after
    # gunicorn forks.
    Random.atfork() 
Example #10
Source File: ec_crypto.py    From LDP_Protocols with MIT License 5 votes vote down vote up
def wrap_message(msg):
    Random.atfork()
    aes_key = randfile.read(32)
    aes_obj = AES.new(aes_key, AES.MODE_CBC, "0" * 16)
    enc_msg = aes_obj.encrypt(msg)
    aes_key_int = int.from_bytes(aes_key, byteorder="little")
    c0, c1 = elgamal_enc(pub_key, aes_key_int)

    blob = c0.x.to_bytes(35, byteorder="little", signed=False) \
           + c0.y.to_bytes(35, byteorder="little", signed=False) \
           + c1.to_bytes(42, byteorder="little", signed=False) \
           + enc_msg
    return blob 
Example #11
Source File: ec_crypto.py    From LDP_Protocols with MIT License 5 votes vote down vote up
def enc_all(msg, num_layer):
    Random.atfork()
    blob = wrap_message(msg)
    for _ in range(num_layer - 1):
        new_blob = wrap_message(blob)
        blob = new_blob
    return blob 
Example #12
Source File: transport.py    From imoocc with GNU General Public License v2.0 5 votes vote down vote up
def atfork(self):
        """
        Terminate this Transport without closing the session.  On posix
        systems, if a Transport is open during process forking, both parent
        and child will share the underlying socket, but only one process can
        use the connection (without corrupting the session).  Use this method
        to clean up a Transport object without disrupting the other process.

        @since: 1.5.3
        """
        self.sock.close()
        self.close() 
Example #13
Source File: transport.py    From imoocc with GNU General Public License v2.0 5 votes vote down vote up
def atfork(self):
        """
        Terminate this Transport without closing the session.  On posix
        systems, if a Transport is open during process forking, both parent
        and child will share the underlying socket, but only one process can
        use the connection (without corrupting the session).  Use this method
        to clean up a Transport object without disrupting the other process.

        @since: 1.5.3
        """
        self.sock.close()
        self.close() 
Example #14
Source File: db.py    From zerodb with GNU Affero General Public License v3.0 5 votes vote down vote up
def _init_db(self):
        """We need this to be executed each time we are in a new process"""
        if self._autoreindex:
            subscribers.init()

        Random.atfork()

        self.__conn_refs = {}
        self.__thread_local = threading.local()
        self.__thread_watcher = ThreadWatcher()
        self._storage = client_storage(**self.__storage_kwargs)
        self._db = SubDB(self._storage, **self.__db_kwargs)
        self._conn_open() 
Example #15
Source File: transport.py    From imoocc with GNU General Public License v2.0 4 votes vote down vote up
def start_client(self, event=None):
        """
        Negotiate a new SSH2 session as a client.  This is the first step after
        creating a new L{Transport}.  A separate thread is created for protocol
        negotiation.

        If an event is passed in, this method returns immediately.  When
        negotiation is done (successful or not), the given C{Event} will
        be triggered.  On failure, L{is_active} will return C{False}.

        (Since 1.4) If C{event} is C{None}, this method will not return until
        negotation is done.  On success, the method returns normally.
        Otherwise an SSHException is raised.

        After a successful negotiation, you will usually want to authenticate,
        calling L{auth_password <Transport.auth_password>} or
        L{auth_publickey <Transport.auth_publickey>}.

        @note: L{connect} is a simpler method for connecting as a client.

        @note: After calling this method (or L{start_server} or L{connect}),
            you should no longer directly read from or write to the original
            socket object.

        @param event: an event to trigger when negotiation is complete
            (optional)
        @type event: threading.Event

        @raise SSHException: if negotiation fails (and no C{event} was passed
            in)
        """
        self.active = True
        if event is not None:
            # async, return immediately and let the app poll for completion
            self.completion_event = event
            self.start()
            return

        # synchronous, wait for a result
        self.completion_event = event = threading.Event()
        self.start()
        Random.atfork()
        while True:
            event.wait(0.1)
            if not self.active:
                e = self.get_exception()
                if e is not None:
                    raise e
                raise SSHException('Negotiation failed.')
            if event.isSet():
                break 
Example #16
Source File: transport.py    From imoocc with GNU General Public License v2.0 4 votes vote down vote up
def start_client(self, event=None):
        """
        Negotiate a new SSH2 session as a client.  This is the first step after
        creating a new L{Transport}.  A separate thread is created for protocol
        negotiation.

        If an event is passed in, this method returns immediately.  When
        negotiation is done (successful or not), the given C{Event} will
        be triggered.  On failure, L{is_active} will return C{False}.

        (Since 1.4) If C{event} is C{None}, this method will not return until
        negotation is done.  On success, the method returns normally.
        Otherwise an SSHException is raised.

        After a successful negotiation, you will usually want to authenticate,
        calling L{auth_password <Transport.auth_password>} or
        L{auth_publickey <Transport.auth_publickey>}.

        @note: L{connect} is a simpler method for connecting as a client.

        @note: After calling this method (or L{start_server} or L{connect}),
            you should no longer directly read from or write to the original
            socket object.

        @param event: an event to trigger when negotiation is complete
            (optional)
        @type event: threading.Event

        @raise SSHException: if negotiation fails (and no C{event} was passed
            in)
        """
        self.active = True
        if event is not None:
            # async, return immediately and let the app poll for completion
            self.completion_event = event
            self.start()
            return

        # synchronous, wait for a result
        self.completion_event = event = threading.Event()
        self.start()
        Random.atfork()
        while True:
            event.wait(0.1)
            if not self.active:
                e = self.get_exception()
                if e is not None:
                    raise e
                raise SSHException('Negotiation failed.')
            if event.isSet():
                break