pub struct Verifier { /* private fields */ }
Expand description

Verifier struct; similiar to ActiveSupport::MessageVerifier.

Implementations§

source§

impl Verifier

source

pub fn new(secret: &str) -> Verifier

Create a new Verifier object.

Examples found in repository?
examples/generate_encrypt.rs (line 10)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    let key_base = "helloworld";
    let salt = "test salt";
    let sign_salt = "test signed salt";

    let verifier = Verifier::new(key_base);

    let dkp = DerivedKeyParams::default();
    let encryptor = AesHmacEncryptor::new(key_base, salt, sign_salt, dkp).unwrap();

    let message = "{\"key\":\"value\"}";

    println!("{}", verifier.generate(message).expect("Verifier failed"));
    println!(
        "{}",
        encryptor
            .encrypt_and_sign(message)
            .expect("Encryptor failed")
    );
}
More examples
Hide additional examples
examples/verify_decrypt.rs (line 13)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
    let key_base = "helloworld";
    let salt = "test salt";
    let sign_salt = "test signed salt";

    let verifier = Verifier::new(key_base);

    let dkp = DerivedKeyParams::default();
    let encryptor = AesHmacEncryptor::new(key_base, salt, sign_salt, dkp).unwrap();

    let mut input: Vec<String> = vec![];
    let mut buffer = String::new();

    for _ in 0..2 {
        match io::stdin().read_line(&mut buffer) {
            Ok(_) => {
                input.push(buffer.clone());

                buffer.clear();
            }

            Err(_) => panic!("Read failed"),
        }
    }

    let (msg1, msg2) = match (input.first(), input.last()) {
        (Some(m1), Some(m2)) => (m1, m2),

        _ => panic!("Missing input"),
    };

    match verifier.verify(msg1) {
        Ok(verified_result) => match encryptor.decrypt_and_verify(msg2) {
            Ok(ref decrypted_result) => {
                println!(
                    "Verified Message: {}",
                    str::from_utf8(&verified_result).expect("Verifier failed")
                );
                println!(
                    "Decrypted Message: {}",
                    str::from_utf8(decrypted_result).expect("Encryptor failed")
                );
            }

            Err(e) => panic!("Encryptor Error: {:?}", e),
        },

        Err(e) => panic!("Verifier Error: {:?}", e),
    }
}
source

pub fn verify(&self, message: &str) -> Result<Vec<u8>>

Verify a signed message generated by a compatible verifier.

Examples found in repository?
examples/verify_decrypt.rs (line 39)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
    let key_base = "helloworld";
    let salt = "test salt";
    let sign_salt = "test signed salt";

    let verifier = Verifier::new(key_base);

    let dkp = DerivedKeyParams::default();
    let encryptor = AesHmacEncryptor::new(key_base, salt, sign_salt, dkp).unwrap();

    let mut input: Vec<String> = vec![];
    let mut buffer = String::new();

    for _ in 0..2 {
        match io::stdin().read_line(&mut buffer) {
            Ok(_) => {
                input.push(buffer.clone());

                buffer.clear();
            }

            Err(_) => panic!("Read failed"),
        }
    }

    let (msg1, msg2) = match (input.first(), input.last()) {
        (Some(m1), Some(m2)) => (m1, m2),

        _ => panic!("Missing input"),
    };

    match verifier.verify(msg1) {
        Ok(verified_result) => match encryptor.decrypt_and_verify(msg2) {
            Ok(ref decrypted_result) => {
                println!(
                    "Verified Message: {}",
                    str::from_utf8(&verified_result).expect("Verifier failed")
                );
                println!(
                    "Decrypted Message: {}",
                    str::from_utf8(decrypted_result).expect("Encryptor failed")
                );
            }

            Err(e) => panic!("Encryptor Error: {:?}", e),
        },

        Err(e) => panic!("Verifier Error: {:?}", e),
    }
}
source

pub fn is_valid_message( &self, encoded_data: &str, signature: &str ) -> Result<bool>

Check if the given signature is valid for some encoded data.

source

pub fn generate(&self, message: &str) -> Result<String>

Generate a signed message from the input message. This message can be consumed and verified by a compatible verifier.

Examples found in repository?
examples/generate_encrypt.rs (line 17)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    let key_base = "helloworld";
    let salt = "test salt";
    let sign_salt = "test signed salt";

    let verifier = Verifier::new(key_base);

    let dkp = DerivedKeyParams::default();
    let encryptor = AesHmacEncryptor::new(key_base, salt, sign_salt, dkp).unwrap();

    let message = "{\"key\":\"value\"}";

    println!("{}", verifier.generate(message).expect("Verifier failed"));
    println!(
        "{}",
        encryptor
            .encrypt_and_sign(message)
            .expect("Encryptor failed")
    );
}

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> 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

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.
§

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

§

fn vzip(self) -> V