bad_lock 0.2.0

lock/unlock files with password by aes-128-cbc
Documentation
//! core: provides the ability to use AES encryption and decryption

// since 'aes::cipher' and 'cbc::cipher' are both re-exported,
// they are actually the same thing, so it doesn't matter where they are imported from.
use aes::cipher::{
    BlockEncryptMut,
    BlockDecryptMut,
    KeyIvInit,
    block_padding::{Pkcs7},
};
use crate::get_md5;

type Aes128CbcEnc = cbc::Encryptor<aes::Aes128Enc>;
type Aes128CbcDec = cbc::Decryptor<aes::Aes128Dec>;

/// AES128, CBC mode, PKCS7 padding
/// - key & iv: md5(code)
pub struct BadLockCore;

impl BadLockCore {
    /// encrypt source with code
    ///
    /// - len(result) = len(source) + 16 - len(source) % 16
    pub fn encrypt<P>(source: &[u8], password: P) -> Vec<u8>
    where
        P: AsRef<[u8]>,
    {
        // use md5 hash to ensure the length is 16
        let code = get_md5!(password);

        Aes128CbcEnc::new(&code.into(), &code.into())
            .encrypt_padded_vec_mut::<Pkcs7>(source)
    }

    /// decrypt source with code
    pub fn decrypt<P>(source: &[u8], password: P) -> Option<Vec<u8>>
    where
        P: AsRef<[u8]>,
    {
        // use md5 hash to ensure the length is 16
        let code = get_md5!(password);

        Aes128CbcDec::new(&code.into(), &code.into()).decrypt_padded_vec_mut::<Pkcs7>(source).ok()
    }
}

#[cfg(test)]
mod unit_test {
    #[test]
    fn encrypt() {
        let source = "hello world".as_bytes();
        let code = "1234567890123456";
        let encrypted = super::BadLockCore::encrypt(source, code);

        println!("{:02x?}", encrypted);
        // [0e, e6, a2, 36, 0d, 77, d8, 46, 18, 15, a3, bf, 07, 12, 74, b2]
    }

    #[test]
    fn decrypt() {
        let source = "hello world".as_bytes();
        let code = "1234567890123456";
        let encrypted = super::BadLockCore::encrypt(source, code);
        let decrypted = super::BadLockCore::decrypt(&encrypted, code);

        println!("{:02x?}", decrypted);
        // [68, 65, 6c, 6c, 6f, 20, 77, 6f, 72, 6c, 64]
    }
}