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

source

pub fn new( point: Point3D, rotation_axis: RotationAxis, iterations: u32, strength: f64, ) -> Self

Creates a new Spha256 instance with the specified parameters.

§Arguments
  • point - A Point3D specifying the starting point in 3D space.
  • rotation_axis - A RotationAxis 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?
examples/basic_usage.rs (line 17)
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");
}
source

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?
examples/basic_usage.rs (line 27)
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");
}
source

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?
examples/basic_usage.rs (line 31)
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 Clone for Spha256

source§

fn clone(&self) -> Spha256

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Spha256

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Spha256

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for Spha256

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,