mirror of
https://github.com/anvie/litcrypt.rs
synced 2026-06-08 13:12:05 +00:00
Merge pull request #11 from HazelTheWitch/master
Add lc_env to encrypt environment variables at compile time
This commit is contained in:
Generated
+1
-1
@@ -42,7 +42,7 @@ checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
|
||||
|
||||
[[package]]
|
||||
name = "litcrypt"
|
||||
version = "0.3.0"
|
||||
version = "0.4.0"
|
||||
dependencies = [
|
||||
"expectest",
|
||||
"proc-macro2",
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "litcrypt"
|
||||
authors = ['Robin Syihab (r@ansvia.com)']
|
||||
version = "0.3.0"
|
||||
version = "0.4.0"
|
||||
description = "Let's encrypt your string statically during compile time"
|
||||
license = "Apache-2.0"
|
||||
repository = "https://github.com/anvie/litcrypt.rs"
|
||||
|
||||
@@ -184,6 +184,28 @@ pub fn lc(tokens: TokenStream) -> TokenStream {
|
||||
}
|
||||
}
|
||||
something = String::from(&something[1..something.len() - 1]);
|
||||
|
||||
encrypt_string(something)
|
||||
}
|
||||
|
||||
/// Encrypts an environment variable at compile time with the key set before, via calling [`use_litcrypt!`].
|
||||
#[proc_macro]
|
||||
pub fn lc_env(tokens: TokenStream) -> TokenStream {
|
||||
let mut var_name = String::from("");
|
||||
|
||||
for tok in tokens {
|
||||
var_name = match tok {
|
||||
TokenTree::Literal(lit) => lit.to_string(),
|
||||
_ => "<unknown>".to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
var_name = String::from(&var_name[1..var_name.len() - 1]);
|
||||
|
||||
encrypt_string(env::var(var_name).unwrap_or(String::from("unknown")))
|
||||
}
|
||||
|
||||
fn encrypt_string(something: String) -> TokenStream {
|
||||
let magic_spell = get_magic_spell();
|
||||
let encrypt_key = xor::xor(&magic_spell, b"l33t");
|
||||
let encrypted = xor::xor(&something.as_bytes(), &encrypt_key);
|
||||
|
||||
@@ -12,3 +12,8 @@ pub fn test_literal1() {
|
||||
pub fn test_literal2() {
|
||||
assert_eq!(lc!("Very secret word"), "Very secret word");
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_env() {
|
||||
assert_eq!(lc_env!("SECRET_ENV"), "Shhhhhh");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user