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 derives a cryptographic key from its parameters and uses the ChaCha20-Poly1305 authenticated encryption algorithm for secure encryption and decryption.
§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).expect("Decryption failed");
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 ChaCha20-Poly1305 authenticated encryption algorithm.
This method encrypts the input data using the ChaCha20-Poly1305 cipher, with a key derived from the hasher’s parameters via the generate_key
method. A random nonce is generated for each encryption operation to ensure uniqueness and security. The nonce is prepended to the ciphertext for use during decryption.
§Arguments
data
- A slice of bytes representing the data to encrypt.
§Returns
A Vec<u8>
containing the encrypted data, with the nonce prepended.
§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).expect("Decryption failed");
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]) -> Result<Vec<u8>, &'static str>
pub fn decrypt(&self, encrypted: &[u8]) -> Result<Vec<u8>, &'static str>
Decrypts the provided data using the ChaCha20-Poly1305 authenticated decryption algorithm.
This method decrypts the input data using the ChaCha20-Poly1305 cipher, with a key derived from the hasher’s parameters via the generate_key
method. The nonce used during encryption is expected to be prepended to the encrypted data and is extracted during decryption.
§Arguments
encrypted
- A slice of bytes representing the encrypted data, with the nonce prepended.
§Returns
A Result<Vec<u8>, &'static str>
containing the decrypted data on success, or an error message on failure.
§Errors
Returns an error if the decryption fails, such as when the ciphertext has been tampered with or the parameters do not match those used during encryption.
§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).expect("Decryption failed");
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).expect("Decryption failed");
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
)