Skip to main content

nautilus_cryptography/python/
signing.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use nautilus_core::python::to_pyvalue_err;
17use pyo3::prelude::*;
18
19use crate::signing::{ed25519_signature, hmac_signature, rsa_signature};
20
21/// Generates an HMAC-SHA256 signature for the given data using the provided secret.
22///
23/// This function creates a cryptographic hash-based message authentication code (HMAC)
24/// using SHA-256 as the underlying hash function. The resulting signature is returned
25/// as a lowercase hexadecimal string.
26///
27/// # Errors
28///
29/// Returns an error if signature generation fails due to key or cryptographic errors.
30#[pyfunction(name = "hmac_signature")]
31#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.cryptography")]
32pub fn py_hmac_signature(secret: &str, data: &str) -> PyResult<String> {
33    hmac_signature(secret, data).map_err(to_pyvalue_err)
34}
35
36/// Signs `data` using RSA PKCS#1 v1.5 SHA-256 with the provided private key in PEM format.
37///
38/// # Errors
39///
40/// Returns an error if:
41/// - `data` is empty.
42/// - `private_key_pem` is not a valid PEM-encoded PKCS#8 RSA private key or cannot be parsed.
43/// - Signature generation fails due to key or cryptographic errors.
44#[pyfunction(name = "rsa_signature")]
45#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.cryptography")]
46pub fn py_rsa_signature(private_key_pem: &str, data: &str) -> PyResult<String> {
47    rsa_signature(private_key_pem, data).map_err(to_pyvalue_err)
48}
49
50/// Signs `data` using Ed25519 with the provided private key seed.
51#[pyfunction(name = "ed25519_signature")]
52#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.cryptography")]
53pub fn py_ed25519_signature(
54    #[gen_stub(override_type(type_repr = "bytes"))] private_key: &[u8],
55    data: &str,
56) -> PyResult<String> {
57    ed25519_signature(private_key, data).map_err(to_pyvalue_err)
58}