Struct spatial_hasher::Spha256
source · pub struct Spha256 { /* private fields */ }
Expand description
A hasher that uses a 3D point and a rotation axis to encrypt and decrypt data.
The Spha256
struct provides methods for encrypting and decrypting data based on a deterministic algorithm. It utilizes a pseudo-random number generator (PRNG) seeded with a hash of its parameters.
§Examples
use spatial_hasher::{Point3D, RotationAxis, Spha256};
let point = Point3D { x: 1.0, y: 2.0, z: 3.0 };
let axis = RotationAxis { x: 0.0, y: 1.0, z: 0.0 };
let hasher = Spha256::new(point, axis, 10, 0.1);
Implementations§
source§impl Spha256
impl Spha256
sourcepub fn new(
point: Point3D,
rotation_axis: RotationAxis,
iterations: u32,
strength: f64,
) -> Self
pub fn new( point: Point3D, rotation_axis: RotationAxis, iterations: u32, strength: f64, ) -> Self
Creates a new Spha256
instance with the specified parameters.
§Arguments
point
- APoint3D
specifying the starting point in 3D space.rotation_axis
- ARotationAxis
specifying the axis of rotation.iterations
- The number of iterations to perform in the hashing process.strength
- A floating-point value representing the strength of the transformation.
§Returns
A new Spha256
instance configured with the provided parameters.
§Examples
use spatial_hasher::{Point3D, RotationAxis, Spha256};
let point = Point3D { x: 1.0, y: 2.0, z: 3.0 };
let axis = RotationAxis { x: 0.0, y: 1.0, z: 0.0 };
let hasher = Spha256::new(point, axis, 10, 0.1);
Examples found in repository?
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
fn main() {
// Define the starting point and rotation axis
let point = Point3D {
x: 1.0,
y: 2.0,
z: 3.0,
};
let axis = RotationAxis {
x: 0.0,
y: 1.0,
z: 0.0,
};
// Create a new SpaceHasher instance
let hasher = Spha256::new(point, axis, 10, 0.1);
// Original data to be encrypted
let original_data = b"Hello, World!";
println!(
"Original Data: {:?}",
String::from_utf8_lossy(original_data)
);
// Encrypt the data
let encrypted = hasher.encrypt(original_data);
println!("Encrypted Data: {:?}", encrypted);
// Decrypt the data
let decrypted = hasher.decrypt(&encrypted);
println!("Decrypted Data: {:?}", String::from_utf8_lossy(&decrypted));
// Verify that the decrypted data matches the original data
assert_eq!(original_data, &decrypted[..], "Decryption failed");
}
sourcepub fn encrypt(&self, data: &[u8]) -> Vec<u8>
pub fn encrypt(&self, data: &[u8]) -> Vec<u8>
Encrypts the provided data using the hasher’s parameters.
This method encrypts the input data by XORing each byte with a byte generated from a pseudo-random number generator (PRNG). The PRNG is seeded using the generate_seed
method, ensuring that the encryption is deterministic based on the hasher’s parameters.
§Arguments
data
- A slice of bytes representing the data to encrypt.
§Returns
A Vec<u8>
containing the encrypted data.
§Examples
use spatial_hasher::{Spha256, Point3D, RotationAxis};
let point = Point3D { x: 1.0, y: 2.0, z: 3.0 };
let axis = RotationAxis { x: 0.0, y: 1.0, z: 0.0 };
let hasher = Spha256::new(point, axis, 10, 0.1);
let data = b"Secret Message";
let encrypted = hasher.encrypt(data);
Examples found in repository?
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
fn main() {
// Define the starting point and rotation axis
let point = Point3D {
x: 1.0,
y: 2.0,
z: 3.0,
};
let axis = RotationAxis {
x: 0.0,
y: 1.0,
z: 0.0,
};
// Create a new SpaceHasher instance
let hasher = Spha256::new(point, axis, 10, 0.1);
// Original data to be encrypted
let original_data = b"Hello, World!";
println!(
"Original Data: {:?}",
String::from_utf8_lossy(original_data)
);
// Encrypt the data
let encrypted = hasher.encrypt(original_data);
println!("Encrypted Data: {:?}", encrypted);
// Decrypt the data
let decrypted = hasher.decrypt(&encrypted);
println!("Decrypted Data: {:?}", String::from_utf8_lossy(&decrypted));
// Verify that the decrypted data matches the original data
assert_eq!(original_data, &decrypted[..], "Decryption failed");
}
sourcepub fn decrypt(&self, encrypted: &[u8]) -> Vec<u8>
pub fn decrypt(&self, encrypted: &[u8]) -> Vec<u8>
Decrypts the provided data using the hasher’s parameters.
The decryption process is identical to the encryption process since the XOR operation is symmetric. The method uses the same PRNG sequence as in encrypt
, ensuring that the original data is recovered when the same parameters are used.
§Arguments
encrypted
- A slice of bytes representing the encrypted data.
§Returns
A Vec<u8>
containing the decrypted data.
§Examples
use spatial_hasher::{Spha256, Point3D, RotationAxis};
let point = Point3D { x: 1.0, y: 2.0, z: 3.0 };
let axis = RotationAxis { x: 0.0, y: 1.0, z: 0.0 };
let hasher = Spha256::new(point, axis, 10, 0.1);
let encrypted = hasher.encrypt(b"Secret Message");
let decrypted = hasher.decrypt(&encrypted);
assert_eq!(decrypted, b"Secret Message");
Examples found in repository?
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
fn main() {
// Define the starting point and rotation axis
let point = Point3D {
x: 1.0,
y: 2.0,
z: 3.0,
};
let axis = RotationAxis {
x: 0.0,
y: 1.0,
z: 0.0,
};
// Create a new SpaceHasher instance
let hasher = Spha256::new(point, axis, 10, 0.1);
// Original data to be encrypted
let original_data = b"Hello, World!";
println!(
"Original Data: {:?}",
String::from_utf8_lossy(original_data)
);
// Encrypt the data
let encrypted = hasher.encrypt(original_data);
println!("Encrypted Data: {:?}", encrypted);
// Decrypt the data
let decrypted = hasher.decrypt(&encrypted);
println!("Decrypted Data: {:?}", String::from_utf8_lossy(&decrypted));
// Verify that the decrypted data matches the original data
assert_eq!(original_data, &decrypted[..], "Decryption failed");
}
Trait Implementations§
source§impl<'de> Deserialize<'de> for Spha256
impl<'de> Deserialize<'de> for Spha256
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for Spha256
impl RefUnwindSafe for Spha256
impl Send for Spha256
impl Sync for Spha256
impl Unpin for Spha256
impl UnwindSafe for Spha256
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)