|
| 1 | +//! Privacy and GDPR compliance utilities. |
| 2 | +
|
| 3 | +use crate::ScrybeError; |
| 4 | +use sha2::{Digest, Sha256}; |
| 5 | + |
| 6 | +/// Hash an IP address with salt for privacy-preserving storage. |
| 7 | +/// |
| 8 | +/// This ensures IP addresses are never stored in plain text, |
| 9 | +/// complying with GDPR data minimization principles. |
| 10 | +/// |
| 11 | +/// # Arguments |
| 12 | +/// |
| 13 | +/// * `ip` - IP address to hash |
| 14 | +/// * `salt` - Salt for hashing (should be unique per deployment) |
| 15 | +/// |
| 16 | +/// # Returns |
| 17 | +/// |
| 18 | +/// SHA-256 hash of the IP address as hex string |
| 19 | +/// |
| 20 | +/// # Example |
| 21 | +/// |
| 22 | +/// ``` |
| 23 | +/// use scrybe_core::privacy::hash_ip; |
| 24 | +/// |
| 25 | +/// let salt = b"deployment-specific-salt"; |
| 26 | +/// let hashed = hash_ip("192.168.1.1", salt); |
| 27 | +/// assert_eq!(hashed.len(), 64); // SHA-256 produces 64 hex chars |
| 28 | +/// ``` |
| 29 | +pub fn hash_ip(ip: &str, salt: &[u8]) -> String { |
| 30 | + let mut hasher = Sha256::new(); |
| 31 | + hasher.update(ip.as_bytes()); |
| 32 | + hasher.update(salt); |
| 33 | + let result = hasher.finalize(); |
| 34 | + hex::encode(result) |
| 35 | +} |
| 36 | + |
| 37 | +/// Validate that no PII (Personally Identifiable Information) is present. |
| 38 | +/// |
| 39 | +/// Checks for common PII patterns that should never be collected. |
| 40 | +/// |
| 41 | +/// # Returns |
| 42 | +/// |
| 43 | +/// `ScrybeError::ValidationError` if PII is detected |
| 44 | +pub fn validate_no_pii(data: &str) -> Result<(), ScrybeError> { |
| 45 | + // Check for email patterns |
| 46 | + if data.contains('@') && data.contains('.') { |
| 47 | + return Err(ScrybeError::validation_error( |
| 48 | + "data", |
| 49 | + "no PII", |
| 50 | + "potential email address detected", |
| 51 | + )); |
| 52 | + } |
| 53 | + |
| 54 | + // Check for phone number patterns (simple check) |
| 55 | + let digit_count = data.chars().filter(|c| c.is_numeric()).count(); |
| 56 | + if digit_count >= 10 { |
| 57 | + return Err(ScrybeError::validation_error( |
| 58 | + "data", |
| 59 | + "no PII", |
| 60 | + "potential phone number detected", |
| 61 | + )); |
| 62 | + } |
| 63 | + |
| 64 | + Ok(()) |
| 65 | +} |
| 66 | + |
| 67 | +/// GDPR consent status. |
| 68 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 69 | +pub enum ConsentStatus { |
| 70 | + /// User has given explicit consent |
| 71 | + Given, |
| 72 | + /// User has not given consent |
| 73 | + NotGiven, |
| 74 | + /// User has withdrawn consent |
| 75 | + Withdrawn, |
| 76 | +} |
| 77 | + |
| 78 | +/// GDPR data subject rights. |
| 79 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 80 | +pub enum DataSubjectRight { |
| 81 | + /// Right of access (Article 15) |
| 82 | + Access, |
| 83 | + /// Right to rectification (Article 16) |
| 84 | + Rectification, |
| 85 | + /// Right to erasure (Article 17) |
| 86 | + Erasure, |
| 87 | + /// Right to restriction of processing (Article 18) |
| 88 | + Restriction, |
| 89 | + /// Right to data portability (Article 20) |
| 90 | + Portability, |
| 91 | + /// Right to object (Article 21) |
| 92 | + Objection, |
| 93 | +} |
| 94 | + |
| 95 | +#[cfg(test)] |
| 96 | +mod tests { |
| 97 | + use super::*; |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_hash_ip_deterministic() { |
| 101 | + let salt = b"test-salt"; |
| 102 | + let hash1 = hash_ip("192.168.1.1", salt); |
| 103 | + let hash2 = hash_ip("192.168.1.1", salt); |
| 104 | + assert_eq!(hash1, hash2, "IP hashes should be deterministic"); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn test_hash_ip_different_salts() { |
| 109 | + let hash1 = hash_ip("192.168.1.1", b"salt1"); |
| 110 | + let hash2 = hash_ip("192.168.1.1", b"salt2"); |
| 111 | + assert_ne!( |
| 112 | + hash1, hash2, |
| 113 | + "Different salts should produce different hashes" |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_hash_ip_different_ips() { |
| 119 | + let salt = b"test-salt"; |
| 120 | + let hash1 = hash_ip("192.168.1.1", salt); |
| 121 | + let hash2 = hash_ip("192.168.1.2", salt); |
| 122 | + assert_ne!( |
| 123 | + hash1, hash2, |
| 124 | + "Different IPs should produce different hashes" |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn test_validate_no_pii_clean_data() { |
| 130 | + assert!(validate_no_pii("Mozilla/5.0").is_ok()); |
| 131 | + assert!(validate_no_pii("en-US").is_ok()); |
| 132 | + } |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn test_validate_no_pii_detects_email() { |
| 136 | + let result = validate_no_pii("user@example.com"); |
| 137 | + assert!(result.is_err()); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn test_validate_no_pii_detects_phone() { |
| 142 | + let result = validate_no_pii("1234567890"); |
| 143 | + assert!(result.is_err()); |
| 144 | + } |
| 145 | +} |
0 commit comments