Speed up formatting the cipher nonce (#477)

This commit is contained in:
J. Nick Koston 2023-07-15 09:12:33 -10:00 committed by GitHub
parent 8306058703
commit 5dd12169f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import logging
from abc import abstractmethod
from enum import Enum
from functools import partial
from struct import Struct
from typing import TYPE_CHECKING, Any, Callable, Optional, cast
import async_timeout
@ -34,12 +35,19 @@ SOCKET_ERRORS = (
TimeoutError,
)
PACK_NONCE = partial(Struct("<LQ").pack, 0)
class ChaCha20CipherReuseable(ChaCha20Cipher): # type: ignore[misc]
"""ChaCha20 cipher that can be reused."""
@property
def klass(self): # type: ignore[no-untyped-def]
return ChaCha20Poly1305Reusable
def format_nonce(self, n: int) -> bytes:
return PACK_NONCE(n)
class ESPHomeNoiseBackend(DefaultNoiseBackend): # type: ignore[misc]
def __init__(self, *args: Any, **kwargs: Any) -> None: