mirror of
https://github.com/anvie/litcrypt.rs
synced 2026-06-08 13:12:05 +00:00
first init.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
target/
|
||||
@@ -0,0 +1,3 @@
|
||||
[submodule "xor-rs"]
|
||||
path = xor-rs
|
||||
url = git@github.com:anvie/xor-rs.git
|
||||
Generated
+29
@@ -0,0 +1,29 @@
|
||||
[root]
|
||||
name = "litcrypt"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"aster 0.36.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"xor 0.1.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aster"
|
||||
version = "0.36.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-serialize"
|
||||
version = "0.3.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "xor"
|
||||
version = "0.1.4"
|
||||
dependencies = [
|
||||
"rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[metadata]
|
||||
"checksum aster 0.36.0 (registry+https://github.com/rust-lang/crates.io-index)" = "365684a2d8153bde2ca60826e54c8d3df76e06578ed868f8baaf91ae811af07b"
|
||||
"checksum rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "237546c689f20bb44980270c73c3b9edd0891c1be49cc1274406134a66d3957b"
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "litcrypt"
|
||||
version = "0.0.1"
|
||||
|
||||
[lib]
|
||||
name = "litcrypt"
|
||||
plugin = true
|
||||
path = "src/litcrypt.rs"
|
||||
|
||||
[dependencies]
|
||||
rustc-serialize = "0.3"
|
||||
aster = { version = "*", default_features = false }
|
||||
# litcrypt = { path = "../litcrypt", version = "*" }
|
||||
xor = { path = "./xor-rs" }
|
||||
@@ -0,0 +1,14 @@
|
||||
// Compile this example code and check the compiled output with:
|
||||
// $ strings ./target/debug/examples/simple | grep Voldemort
|
||||
// the encrypted one will not print anything (just blank).
|
||||
|
||||
#![feature(plugin, custom_attribute)]
|
||||
#![plugin(litcrypt)]
|
||||
|
||||
extern crate xor;
|
||||
|
||||
fn main(){
|
||||
// uncomment this for plain (non pre-compile-encrypted string)
|
||||
// println!("his name is: {}", "Voldemort");
|
||||
println!("his name is: {}", lc!("Voldemort"));
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![crate_type="dylib"]
|
||||
#![feature(plugin_registrar, rustc_private)]
|
||||
#![feature(slice_patterns)]
|
||||
|
||||
extern crate syntax;
|
||||
extern crate syntax_pos;
|
||||
extern crate rustc;
|
||||
extern crate rustc_plugin;
|
||||
extern crate rustc_serialize as serialize;
|
||||
extern crate aster;
|
||||
extern crate xor;
|
||||
|
||||
use std::env;
|
||||
|
||||
use syntax::parse::token;
|
||||
use syntax::tokenstream::TokenTree;
|
||||
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
|
||||
use syntax_pos::Span;
|
||||
use rustc_plugin::Registry;
|
||||
// use aster::invoke::Invoke;
|
||||
use aster::path::PathBuilder;
|
||||
|
||||
use serialize::base64::{STANDARD, ToBase64};
|
||||
|
||||
|
||||
fn expand_litcrypt(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
|
||||
-> Box<MacResult + 'static> {
|
||||
|
||||
if args.len() != 1 {
|
||||
cx.span_err(
|
||||
sp,
|
||||
&format!("argument should be a single identifier, but got {} arguments", args.len()));
|
||||
return DummyResult::any(sp);
|
||||
}
|
||||
|
||||
let text = match args[0] {
|
||||
TokenTree::Token(_, token::Literal(token::Lit::Str_(name), _)) => name.to_string(),
|
||||
_ => {
|
||||
cx.span_err(sp, "argument should be a single identifier");
|
||||
return DummyResult::any(sp);
|
||||
}
|
||||
};
|
||||
|
||||
let xor_encrypt_key = match env::var("XOR_ENCRYPT_KEY"){
|
||||
Ok(a) => a,
|
||||
Err(_) => {
|
||||
cx.span_err(sp, "you needs to specify encrypt key via XOR_ENCRYPT_KEY environment variable.");
|
||||
return DummyResult::any(sp);
|
||||
}
|
||||
};
|
||||
|
||||
let encrypted = xor::xor(text.as_bytes(), xor_encrypt_key.as_bytes());
|
||||
let _encrypted = encrypted.to_base64(STANDARD);
|
||||
|
||||
println!("[litcrypt] encrypted: `{}` -> `{}`", text, _encrypted);
|
||||
|
||||
let path = PathBuilder::new()
|
||||
.span(sp)
|
||||
.global()
|
||||
.ids(&["xor","decrypt"])
|
||||
.build();
|
||||
|
||||
let builder = aster::AstBuilder::new().span(sp);
|
||||
|
||||
let _lit = builder.expr().lit().str(_encrypted.as_str());
|
||||
let _key = builder.expr().lit().str(xor_encrypt_key.as_str());
|
||||
let _expr = builder.expr().call().build_path(path).with_arg(_lit).with_arg(_key).build();
|
||||
|
||||
MacEager::expr(_expr)
|
||||
}
|
||||
|
||||
#[plugin_registrar]
|
||||
pub fn plugin_registrar(reg: &mut Registry) {
|
||||
reg.register_macro("lc", expand_litcrypt);
|
||||
}
|
||||
Submodule
+1
Submodule xor-rs added at 814d5644e5
Reference in New Issue
Block a user