-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encrypt/decrypt data with secret key
- Loading branch information
Showing
7 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,8 @@ members = [ | |
"testing", | ||
"tls", | ||
"upgrade", | ||
|
||
"pastebin", | ||
"todo", | ||
"chat", | ||
"private-data", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "private-data" | ||
version = "0.0.0" | ||
workspace = "../" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
rocket = { path = "../../core/lib", features = ["secrets"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[default] | ||
secret_key = "itlYmFR2vYKrOmFhupMIn/hyB6lYCCTXz4yaQX89XVg=" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#[macro_use] | ||
extern crate rocket; | ||
|
||
use rocket::{Config, State}; | ||
use rocket::fairing::AdHoc; | ||
|
||
#[cfg(test)] mod tests; | ||
|
||
#[get("/encrypt/<msg>")] | ||
fn encrypt_endpoint(msg: &str, config: &State<Config>) -> String{ | ||
let secret_key = config.secret_key.clone(); | ||
let encrypted = secret_key.encrypt(msg).unwrap(); | ||
|
||
info!("received message for encrypt: '{}'", msg); | ||
info!("encrypted msg: '{}'", encrypted); | ||
|
||
encrypted | ||
} | ||
|
||
#[get("/decrypt/<msg>")] | ||
fn decrypt_endpoint(msg: &str, config: &State<Config>) -> String { | ||
let secret_key = config.secret_key.clone(); | ||
let decrypted = secret_key.decrypt(msg).unwrap(); | ||
|
||
info!("received message for decrypt: '{}'", msg); | ||
info!("decrypted msg: '{}'", decrypted); | ||
|
||
decrypted | ||
} | ||
|
||
#[launch] | ||
fn rocket() -> _ { | ||
rocket::build() | ||
.mount("/", routes![encrypt_endpoint, decrypt_endpoint]) | ||
.attach(AdHoc::config::<Config>()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use rocket::local::blocking::Client; | ||
|
||
#[test] | ||
fn encrypt_decrypt() { | ||
let client = Client::tracked(super::rocket()).unwrap(); | ||
let msg = "some-secret-message"; | ||
|
||
let encrypted = client.get(format!("/encrypt/{}", msg)).dispatch().into_string().unwrap(); | ||
let decrypted = client.get(format!("/decrypt/{}", encrypted)).dispatch().into_string().unwrap(); | ||
|
||
assert_eq!(msg, decrypted); | ||
} |