nautilus_cryptography/python/
signing.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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/// HMAC-SHA256 signature of `data` using the provided `secret`.
22///
23/// # Errors
24///
25/// Returns an error if signature generation fails due to key or cryptographic errors.
26#[pyfunction(name = "hmac_signature")]
27pub fn py_hmac_signature(secret: &str, data: &str) -> PyResult<String> {
28    hmac_signature(secret, data).map_err(to_pyvalue_err)
29}
30
31/// RSA PKCS#1 SHA-256 signature of `data` using the provided private key in PEM format.
32///
33/// # Errors
34///
35/// Returns an error if signature generation fails, e.g., due to empty data or invalid key PEM.
36#[pyfunction(name = "rsa_signature")]
37pub fn py_rsa_signature(private_key_pem: &str, data: &str) -> PyResult<String> {
38    rsa_signature(private_key_pem, data).map_err(to_pyvalue_err)
39}
40
41/// Ed25519 signature of `data` using the provided private key seed.
42///
43/// # Errors
44///
45/// Returns an error if the private key seed is invalid or signature creation fails.
46#[pyfunction(name = "ed25519_signature")]
47pub fn py_ed25519_signature(private_key: &[u8], data: &str) -> PyResult<String> {
48    ed25519_signature(private_key, data).map_err(to_pyvalue_err)
49}