Pycryptodome RSA解密导致大规模性能降级(RPS)
Pycryptodome RSA decryption causes massive performance downgrade (RPS)
我正在测试我的 Flask 应用端点并测量单台机器上的性能。该特定端点有一个名为 decrypt_request
的装饰器。这个装饰器的实现如下所示:
1. Read X-Session-Key header (encrypted by public key)
2. Import RSA key
3. Create a cryptor and decrypt the session key (RSA)
4. Read data from the request body (which is encrypted by the above session key)
5. Decrypt the request body using the session key (AES)
端点看起来像这样:
@app.route('/test', methods=['POST'])
@decrypt_request
def view_function():
# do something here
在执行一些负载测试后,我发现平均 RPS 在 50 左右(这肯定不好,但硬件资源目前受到限制)。我做的一件事是禁用装饰器,我发现 RPS 大幅增加(大约 500 RPS)。最后,我刚刚注释掉装饰器 i-e 中的 public 键操作:我希望 header 中有一个干净的 session 键,并且只执行 AES 操作。 RPS 再次达到相同的 500 RPS 左右。这是确定public键操作非常慢。我正在使用的 Pycryptodome
库声明:
PyCryptodome is not a wrapper to a separate C library like OpenSSL. To the largest possible extent, algorithms are implemented in pure Python. Only the pieces that are extremely critical to performance (e.g. block ciphers) are implemented as C extensions.
而且我认为这是操作非常缓慢的真正原因。有什么办法可以让这些操作快如闪电吗?
对于 PyCryptodome,没有。
PyCryptoDome 的 RSA 模块完全在 python 中实现,这意味着不幸的是,您确实会损失巨大的性能(pebble-rockslide 类型)。相反,如果您想要大幅提升性能,我建议您使用 cryptography
模块。 cryptography
包装了 OpenSSL 的 RSA 实现,并且比 RSA 的 PyCryptoDome 快几倍。
我正在测试我的 Flask 应用端点并测量单台机器上的性能。该特定端点有一个名为 decrypt_request
的装饰器。这个装饰器的实现如下所示:
1. Read X-Session-Key header (encrypted by public key)
2. Import RSA key
3. Create a cryptor and decrypt the session key (RSA)
4. Read data from the request body (which is encrypted by the above session key)
5. Decrypt the request body using the session key (AES)
端点看起来像这样:
@app.route('/test', methods=['POST'])
@decrypt_request
def view_function():
# do something here
在执行一些负载测试后,我发现平均 RPS 在 50 左右(这肯定不好,但硬件资源目前受到限制)。我做的一件事是禁用装饰器,我发现 RPS 大幅增加(大约 500 RPS)。最后,我刚刚注释掉装饰器 i-e 中的 public 键操作:我希望 header 中有一个干净的 session 键,并且只执行 AES 操作。 RPS 再次达到相同的 500 RPS 左右。这是确定public键操作非常慢。我正在使用的 Pycryptodome
库声明:
PyCryptodome is not a wrapper to a separate C library like OpenSSL. To the largest possible extent, algorithms are implemented in pure Python. Only the pieces that are extremely critical to performance (e.g. block ciphers) are implemented as C extensions.
而且我认为这是操作非常缓慢的真正原因。有什么办法可以让这些操作快如闪电吗?
对于 PyCryptodome,没有。
PyCryptoDome 的 RSA 模块完全在 python 中实现,这意味着不幸的是,您确实会损失巨大的性能(pebble-rockslide 类型)。相反,如果您想要大幅提升性能,我建议您使用 cryptography
模块。 cryptography
包装了 OpenSSL 的 RSA 实现,并且比 RSA 的 PyCryptoDome 快几倍。