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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! Cryptographic utility functions and macros for secure operations.
//!
//! This module provides essential cryptographic utilities including:
//! - Random byte generation
//! - Buffer concatenation
//! - SHA-512 hashing
//!
//! # Features
//!
//! - Secure random number generation using the `rand` crate
//! - Efficient buffer concatenation with pre-allocation
//! - SHA-512 hash computation using the `sha2` crate
//!
//! # Examples
//!
//! Generate random bytes:
//! ```rust,ignore
//! // Unable to run because function is private
//! use umya_spreadsheet::helper::crypt::generate_random_bytes;
//!
//! generate_random_bytes!(salt, 16);
//! // `salt` now contains 16 cryptographically secure random bytes
//! ```
//!
//! Concatenate buffers and compute hash:
//! ```rust,ignore
//! // Unable to run because function is private
//! use umya_spreadsheet::helper::crypt::{
//! buffer_concat,
//! hash_concatenated,
//! };
//!
//! let data1 = b"password";
//! let data2 = b"salt";
//!
//! // Concatenate buffers
//! let combined = buffer_concat(&[data1, data2]);
//!
//! // Compute SHA-512 hash
//! let hash = hash_concatenated(&[data1, data2]);
//! ```
//!
//! # Implementation Details
//!
//! The module uses:
//! - Thread-local random number generator for secure byte generation
//! - Optimized buffer concatenation with pre-allocated capacity
//! - SHA-512 implementation from the `sha2` crate
//!
//! # Security Considerations
//!
//! - Uses cryptographically secure random number generation
//! - Implements standard SHA-512 hashing
//! - Ensures efficient memory management for large buffers
//!
//! # Note
//!
//! This module is intended for internal use within the crate, as indicated
//! by the `pub(crate)` visibility modifiers.
use ;
/// A macro that generates an array of random bytes.
///
/// This macro initializes a variable with the specified name and fills it
/// with random bytes of the given size using the `rand` crate.
///
/// # Parameters
///
/// - `$var_name`: The name of the variable to hold the generated random bytes.
/// - `$size`: The size of the byte array to be generated.
///
/// # Example
///
/// ```rust,ignore
/// // Unable to run because macro is private
/// generate_random_bytes!(random_bytes, 16);
/// // `random_bytes` now contains 16 random bytes.
/// ```
/// Concatenates multiple byte slices into a single `Vec<u8>`.
///
/// This function takes a slice of byte slices and computes the total length
/// of the resulting vector. It preallocates the vector with the total length
/// and extends it with each input buffer.
///
/// # Parameters
///
/// - `buffers`: A slice of byte slices to be concatenated.
///
/// # Returns
///
/// A `Vec<u8>` containing all the concatenated byte slices.
///
/// # Example
///
/// ```rust,ignore
/// // Unable to run because function is private
/// let buffer1 = b"Hello, ";
/// let buffer2 = b"world!";
/// let result = buffer_concat(&[buffer1, buffer2]);
/// assert_eq!(result, b"Hello, world!");
/// ```
pub
/// Computes the SHA-512 hash of concatenated byte slices.
///
/// This function takes a slice of byte slices, concatenates them, and computes
/// their SHA-512 hash. The resulting hash is returned as a vector of bytes.
///
/// # Parameters
///
/// - `buffers`: A slice of byte slices to be hashed.
///
/// # Returns
///
/// A `Vec<u8>` containing the SHA-512 hash of the concatenated input buffers.
///
/// # Example
///
/// ```rust,ignore
/// // Unable to run because function is private
/// let buffer1 = b"Hello, ";
/// let buffer2 = b"world!";
/// let hash = hash_concatenated(&[buffer1, buffer2]);
/// // `hash` now contains the SHA-512 hash of "Hello, world!".
/// ```
pub
/// Re-exports the `generate_random_bytes` macro for use in other modules.
pub use generate_random_bytes;