forked from vllm-project/vllm
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f3cf42
commit 4934eeb
Showing
4 changed files
with
67 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import hashlib | ||
import hmac | ||
import secrets | ||
|
||
import zmq | ||
import zmq.asyncio | ||
|
||
# TODO: switch to SECRET_KEY = secrets.token_bytes(16) | ||
# and pass the SECRET_KEY to the background process. | ||
SECRET_KEY = b"my_key" | ||
|
||
def sign(msg: bytes) -> bytes: | ||
"""Compute the HMAC digest of msg, given signing key `key`""" | ||
return hmac.HMAC( | ||
SECRET_KEY, | ||
msg, | ||
digestmod=hashlib.sha256, | ||
).digest() | ||
|
||
def check_signed(sig: bytes, msg: bytes) -> bool: | ||
correct_sig = sign(msg) | ||
return hmac.compare_digest(sig, correct_sig) | ||
|
||
def send_signed(socket: zmq.Socket, msg: bytes): | ||
"""Send signed message to socket.""" | ||
|
||
sig = sign(msg) | ||
socket.send_multipart((sig, msg), copy=False) | ||
|
||
def recv_signed(socket: zmq.Socket): | ||
"""Get signed message from socket.""" | ||
|
||
sig, msg = socket.recv_multipart(copy=False) | ||
if not check_signed(sig, msg): | ||
raise ValueError("Message signature is invalid.") | ||
return msg | ||
|
||
async def send_signed_async(socket: zmq.asyncio.Socket, msg: bytes): | ||
"""Send signed message to asyncio socket.""" | ||
|
||
sig = sign(msg) | ||
await socket.send_multipart((sig, msg), copy=False) | ||
|
||
async def recv_signed_async(socket: zmq.asyncio.Socket): | ||
"""Get signed message from asyncio socket.""" | ||
|
||
sig, msg = await socket.recv_multipart(copy=False) | ||
if not check_signed(sig, msg): | ||
raise ValueError("Message signature is invalid.") | ||
return msg |