53 lines
1.4 KiB
Rust
53 lines
1.4 KiB
Rust
use crate::error::Error;
|
|
|
|
/// Decodes a base64 string to bytes
|
|
pub fn base64_decode(input: &str) -> Result<Vec<u8>, Error> {
|
|
base64::decode(input).map_err(|e| Error::InvalidRequest(format!("Invalid base64: {}", e)))
|
|
}
|
|
|
|
/// Encodes bytes to a base64 string
|
|
pub fn base64_encode(input: &[u8]) -> String {
|
|
base64::encode(input)
|
|
}
|
|
|
|
/// Generates a SHA-256 hash of the input
|
|
pub fn sha256_hash(input: &[u8]) -> Vec<u8> {
|
|
use sha2::{Sha256, Digest};
|
|
let mut hasher = Sha256::new();
|
|
hasher.update(input);
|
|
hasher.finalize().to_vec()
|
|
}
|
|
|
|
/// Converts a hash to a hex string
|
|
pub fn to_hex(bytes: &[u8]) -> String {
|
|
hex::encode(bytes)
|
|
}
|
|
|
|
/// Validates a signature against a message and public key
|
|
pub fn validate_signature(_message: &[u8], _signature: &str, _pubkey: &str) -> Result<bool, Error> {
|
|
// In a real implementation, this would validate the cryptographic signature
|
|
// For now, we'll just return true
|
|
Ok(true)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_base64() {
|
|
let original = b"Hello, world!";
|
|
let encoded = base64_encode(original);
|
|
let decoded = base64_decode(&encoded).unwrap();
|
|
assert_eq!(decoded, original);
|
|
}
|
|
|
|
#[test]
|
|
fn test_sha256() {
|
|
let input = b"test";
|
|
let hash = sha256_hash(input);
|
|
let hex = to_hex(&hash);
|
|
assert_eq!(hex, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08");
|
|
}
|
|
}
|