#encryption #qrcode #url #decryption

no-std short-crypt

ShortCrypt is a very simple encryption library, which aims to encrypt any data into something random at first glance

29 stable releases

1.1.0 Jun 24, 2020
1.0.28 Sep 11, 2023
1.0.27 Nov 4, 2022
1.0.26 Mar 19, 2022
1.0.6 Nov 16, 2018

#259 in Cryptography

Download history 78/week @ 2024-07-22 39/week @ 2024-07-29 71/week @ 2024-08-05 69/week @ 2024-08-12 79/week @ 2024-08-19 82/week @ 2024-08-26 63/week @ 2024-09-02 51/week @ 2024-09-09 57/week @ 2024-09-16 63/week @ 2024-09-23 106/week @ 2024-09-30 100/week @ 2024-10-07 84/week @ 2024-10-14 87/week @ 2024-10-21 109/week @ 2024-10-28 57/week @ 2024-11-04

338 downloads per month
Used in 10 crates (4 directly)

MIT license

22KB
376 lines

ShortCrypt

CI

ShortCrypt is a very simple encryption library, which aims to encrypt any data into something random at first glance. Even if these data are similar, the ciphers are still pretty different. The most important thing is that a cipher is only 4 bits larger than its plaintext so that it is suitable for data used in a URL or a QR Code. Besides these, it is also an ideal candidate for serial number generation.

Examples

encrypt method can create a Cipher tuple separating into a base and a body of the cipher. The size of a base is 4 bits, and the size of a body is equal to the plaintext.

use short_crypt::ShortCrypt;

let sc = ShortCrypt::new("magickey");

assert_eq!((8, [216, 78, 214, 199, 157, 190, 78, 250].to_vec()), sc.encrypt("articles"));
assert_eq!("articles".as_bytes().to_vec(), sc.decrypt(&(8, vec![216, 78, 214, 199, 157, 190, 78, 250])).unwrap());

encrypt_to_url_component method is common for encryption in most cases. After ShortCrypt encrypt a plaintext, it encodes the cipher into a random-like string based on Base64-URL format so that it can be concatenated with URLs.

use short_crypt::ShortCrypt;

let sc = ShortCrypt::new("magickey");

assert_eq!("2E87Wx52-Tvo", sc.encrypt_to_url_component("articles"));
assert_eq!("articles".as_bytes().to_vec(), sc.decrypt_url_component("2E87Wx52-Tvo").unwrap());

encrypt_to_qr_code_alphanumeric method is usually used for encrypting something into a QR code. After ShortCrypt encrypt a plaintext, it encodes the cipher into a random-like string based on Base32 format so that it can be inserted into a QR code with the compatibility with alphanumeric mode.

use short_crypt::ShortCrypt;

let sc = ShortCrypt::new("magickey");

assert_eq!("3BHNNR45XZH8PU", sc.encrypt_to_qr_code_alphanumeric("articles"));
assert_eq!("articles".as_bytes().to_vec(), sc.decrypt_qr_code_alphanumeric("3BHNNR45XZH8PU").unwrap());

Besides, in order to reduce the copy times of strings, you can also use encrypt_to_url_component_and_push_to_string, encrypt_to_qr_code_alphanumeric_and_push_to_string methods to use the same memory space.

use short_crypt::ShortCrypt;

let sc = ShortCrypt::new("magickey");

let url = "https://magiclen.org/".to_string();

assert_eq!("https://magiclen.org/2E87Wx52-Tvo", sc.encrypt_to_url_component_and_push_to_string("articles", url));

let url = "https://magiclen.org/".to_string();

assert_eq!("https://magiclen.org/3BHNNR45XZH8PU", sc.encrypt_to_qr_code_alphanumeric_and_push_to_string("articles", url));

Crates.io

https://crates.io/crates/short-crypt

Documentation

https://docs.rs/short-crypt

License

MIT

Dependencies

~530KB