laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
//! Authenticated encryption with the application's keys.
//!
//! When you need to store a secret and read it back later, such as a user's token for another
//! service, encrypt it. [`encrypt_string`] and [`decrypt_string`] encrypt strings with the
//! application's `APP_KEY`, and [`encrypt`] and [`decrypt`] do the same for any serializable
//! value, as JSON. Decrypting tries the current key, then each key in `APP_PREVIOUS_KEYS`, so you
//! can rotate the key without losing data.
//!
//! ```no_run
//! # fn example(token: &str) -> laravel::Result<()> {
//! let encrypted = laravel::crypt::encrypt_string(token)?;
//!
//! assert_eq!(laravel::crypt::decrypt_string(&encrypted)?, token);
//! # Ok(()) }
//! ```
//!
//! An [`Encrypter`] does the work with explicit keys. It encrypts with AES, either in CBC mode
//! with an HMAC-SHA256 MAC or in GCM mode, into a base64-encoded JSON payload.
//!
//! ```
//! use laravel::crypt::{Cipher, Encrypter};
//!
//! let encrypter = Encrypter::new([7; 32], Cipher::Aes256Cbc)?;
//! let payload = encrypter.encrypt_string("secret")?;
//!
//! assert_eq!(encrypter.decrypt_string(&payload)?, "secret");
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! See [Encryption](https://github.com/laravel/rust-framework/blob/0.x/docs/encryption.md).

pub(crate) use laravel_database::crypt::*;
#[cfg(feature = "database")]
#[doc(hidden)]
pub use laravel_database::crypt::{__KeepCiphertext, __decrypt_json, __encrypt_json, __same_json};
#[doc(inline)]
pub use laravel_database::crypt::{
    Cipher, DecryptError, EncryptError, Encrypter, KeyError, decrypt, decrypt_string, encrypt, encrypt_string,
};
#[cfg(feature = "database")]
#[doc(inline)]
pub use laravel_database::crypt::{Encryptable, Encrypted};

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn application_encryption_accepts_owned_and_borrowed_values() {
        let app = crate::Application::configure()
            .replace_env(crate::config::Env::default())
            .with_config(|config, _| config.app.key = Some(Encrypter::generate_key(Cipher::Aes256Cbc)))
            .create();

        app.scope(async || {
            let values = vec!["Taylor".to_owned(), "Abigail".to_owned()];
            let borrowed = encrypt(&values).unwrap();
            let owned = encrypt(values).unwrap();
            let expected = vec!["Taylor".to_owned(), "Abigail".to_owned()];
            assert_eq!(decrypt::<Vec<String>>(&borrowed).unwrap(), expected);
            assert_eq!(decrypt::<Vec<String>>(&owned).unwrap(), expected);
        })
        .await;
    }
}