1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use ;
use crateJwkError;
use ;
/// Generates a new RSA key pair with the specified key size and returns it in PEM format.
///
/// This function creates a fresh RSA key pair of the given bit length (e.g., 2048 or 4096 bits),
/// and returns both the private and public keys as PEM-encoded strings.
/// These keys can then be used for JWT signing, TLS, or converted into JWK format.
///
/// # Parameters
/// - `bits`: The desired size of the RSA key in bits. Common values are 2048 or 4096.
///
/// # Returns
/// A `Result` containing a tuple of `(private_key_pem, public_key_pem)` as `String`s on success,
/// or a `JwkError` on failure. The keys are returned in PEM format and are ready to be stored,
/// used directly, or converted into a JWK.
///
/// # Errors
/// Returns a `JwkError` if:
/// - Key generation fails.
/// - The keys cannot be serialized to PEM format.
///
/// # Example
/// ```rust
/// use jwk_kit::generator::rsa::generate_rsa_keypair_pem;
/// match generate_rsa_keypair_pem(2048) {
/// Ok((private_pem, public_pem)) => {
/// println!("Private Key:\n{}", private_pem);
/// println!("Public Key:\n{}", public_pem);
/// },
/// Err(e) => {
/// eprintln!("RSA key generation failed: {:?}", e);
/// },
/// }
/// ```
///
/// # Note
/// To convert the generated public key to a JWK, you can use [`extract_rsa_n_e`].
/// Extracts the modulus (N) and exponent (E) from a PEM-encoded RSA key.
///
/// This function takes a PEM-encoded RSA key (either public or private) and extracts
/// the modulus (N) and exponent (E), which are essential components of an RSA public key
/// as part of the JWK (JSON Web Key) representation.
///
/// # Parameters
/// - `pem_data`: A string slice containing the PEM-encoded RSA key (can be either public or private key).
///
/// # Returns
/// A `Result` containing a tuple with the modulus (`N`) and exponent (`E`) as `String`s in case of success,
/// or a `JwkError` in case of failure. The modulus and exponent are returned as base64url-encoded strings,
/// which is the standard format for JWKs.
///
/// # Errors
/// This function returns a `JwkError` if:
/// - The input PEM data is not a valid RSA key.
/// - The key does not contain valid RSA modulus and exponent values.
/// - Any other error occurs while extracting the modulus and exponent.
///
/// # Example
/// ```rust
/// use jwk_kit::generator::rsa::extract_rsa_n_e;
/// let pem_data = "-----BEGIN PUBLIC KEY-----\n..."; // PEM-encoded RSA public key
/// match extract_rsa_n_e(pem_data) {
/// Ok((n, e)) => {
/// println!("Modulus (N): {}, Exponent (E): {}", n, e);
/// },
/// Err(e) => {
/// eprintln!("Failed to extract N and E: {:?}", e);
/// },
/// }
/// ```
///
/// # Note
/// This function is useful for extracting the public key components for RSA keys,
/// particularly when generating a JWK for use in web-based authentication systems (e.g., JWT, OAuth).