mirror of
https://github.com/mii443/encrypt.git
synced 2025-08-22 15:05:33 +00:00
add config
This commit is contained in:
8
Cargo.lock
generated
8
Cargo.lock
generated
@ -34,6 +34,12 @@ version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
|
||||
|
||||
[[package]]
|
||||
name = "bigdecimal"
|
||||
version = "0.3.0"
|
||||
@ -130,6 +136,7 @@ checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
|
||||
name = "encrypt"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"bigdecimal",
|
||||
"clap",
|
||||
"env_logger",
|
||||
@ -139,6 +146,7 @@ dependencies = [
|
||||
"rand_chacha 0.3.1",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"toml",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
|
@ -16,3 +16,5 @@ log = "0.4.17"
|
||||
env_logger = "0.9.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "*"
|
||||
base64 = "*"
|
||||
toml = "*"
|
||||
|
2
gpsl_conf.toml
Normal file
2
gpsl_conf.toml
Normal file
@ -0,0 +1,2 @@
|
||||
private_key = "NDM1ODIxMjU2NDA1MTc3NjExNTc0MTk2NjQ5MjUyMzMxMDA5MTMyMjMwNTA1MjA1MTA1OTMzMTA0NjEwODU3MzU3NDg3NDA1Njk5ODU="
|
||||
public_key = "eyJQb2ludCI6eyJ4Ijp7InZhbHVlIjoiMHg1NDhlZWQwYzNlYzc5OTJlMzhhODkyNGJjMDk4NTZmZDcwOTNjZDIyZjQ3N2ZjNjA0OGQ3ZWYxNjJhNDM3Y2UiLCJwIjoiMHhmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZWZmZmZmYzJmIn0sInkiOnsidmFsdWUiOiIweGU3OTVlNGZmMDdlOTYyYmUzNzFlYmJhYWVlYWI2ZDBkZmVlNjg5YmQ3MzlmYjk5MGI1NmE0NDQxZTJhZGFiNzIiLCJwIjoiMHhmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZWZmZmZmYzJmIn0sImEiOnsidmFsdWUiOiIweDAiLCJwIjoiMHhmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZWZmZmZmYzJmIn0sImIiOnsidmFsdWUiOiIweDciLCJwIjoiMHhmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZWZmZmZmYzJmIn19fQ=="
|
@ -37,6 +37,10 @@ pub enum EllipticCurvePoint {
|
||||
}
|
||||
|
||||
impl EllipticCurvePoint {
|
||||
pub fn from_str(s: &str) -> Result<Self, serde_json::Error> {
|
||||
serde_json::from_str(s)
|
||||
}
|
||||
|
||||
pub fn exp(&self, k: U512) -> Self {
|
||||
if k == U512::zero() {
|
||||
return Self::Infinity;
|
||||
|
@ -2,11 +2,13 @@ use crate::gpsl::node::*;
|
||||
use crate::gpsl::token::*;
|
||||
use crate::gpsl::tokenizer::*;
|
||||
use log::debug;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::gpsl_type::GPSLType;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct Parser {
|
||||
pub tokenizer: Tokenizer,
|
||||
pub local_vars: HashMap<String, usize>,
|
||||
|
@ -1,4 +1,6 @@
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub enum TokenKind {
|
||||
CONTROL,
|
||||
RETURN,
|
||||
@ -9,7 +11,7 @@ pub enum TokenKind {
|
||||
TEXT,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Token {
|
||||
pub kind: TokenKind,
|
||||
pub num: i64,
|
||||
|
@ -1,8 +1,10 @@
|
||||
use crate::gpsl::source::*;
|
||||
use crate::gpsl::token::*;
|
||||
use log::*;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct Tokenizer {
|
||||
pub tokens: Vec<Token>,
|
||||
pub cursor: usize,
|
||||
|
112
src/main.rs
112
src/main.rs
@ -3,6 +3,7 @@ mod elliptic_curve;
|
||||
mod gpsl;
|
||||
use common::finite_field::FiniteFieldElement;
|
||||
use elliptic_curve::elliptic_curve::EllipticCurve;
|
||||
use elliptic_curve::elliptic_curve::EllipticCurvePoint;
|
||||
use elliptic_curve::encryption::Encryption;
|
||||
use gpsl::external_function::ExternalFuncReturn;
|
||||
use gpsl::external_function::ExternalFuncStatus;
|
||||
@ -12,13 +13,99 @@ use gpsl::vm::gpsl::ServerFunctionCall;
|
||||
use gpsl::{external_function::STD_FUNC, source::*, tokenizer::*, vm::gpsl::*};
|
||||
use log::*;
|
||||
use primitive_types::U512;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::BufRead;
|
||||
use std::io::BufReader;
|
||||
use std::io::Read;
|
||||
use std::io::Write;
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
struct ConfigFile {
|
||||
pub private_key: Option<String>,
|
||||
pub public_key: Option<String>,
|
||||
}
|
||||
|
||||
impl ConfigFile {
|
||||
pub fn from_config(config: Config) -> Self {
|
||||
let private_key = {
|
||||
if let Some(private_key) = config.private_key {
|
||||
let s = private_key.to_string();
|
||||
let encode = base64::encode(&s);
|
||||
Some(encode)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let public_key = {
|
||||
if let Some(public_key) = config.public_key {
|
||||
let s = serde_json::to_string(&public_key).unwrap();
|
||||
let encode = base64::encode(&s);
|
||||
Some(encode)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
Self {
|
||||
private_key,
|
||||
public_key,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
struct Config {
|
||||
pub private_key: Option<U512>,
|
||||
pub public_key: Option<EllipticCurvePoint>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn read_file(file: &str) -> String {
|
||||
let mut file = fs::File::open(file).unwrap();
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).unwrap();
|
||||
contents
|
||||
}
|
||||
pub fn from_file(file: &str) -> Self {
|
||||
let file = Config::read_file(file);
|
||||
let config: ConfigFile = toml::from_str(&file).unwrap();
|
||||
|
||||
let private_key = {
|
||||
if let Some(private_key) = config.private_key {
|
||||
let decoded = base64::decode(&private_key).unwrap();
|
||||
let s = std::str::from_utf8(&decoded).unwrap();
|
||||
Some(U512::from_str(s).unwrap())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let public_key = {
|
||||
if let Some(public_key) = config.public_key {
|
||||
let decoded = base64::decode(&public_key).unwrap();
|
||||
let s = std::str::from_utf8(&decoded).unwrap();
|
||||
Some(EllipticCurvePoint::from_str(s).unwrap())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
Config {
|
||||
private_key,
|
||||
public_key,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
[6139062701328441600,
|
||||
[258929920560, 23709360],
|
||||
@ -233,6 +320,9 @@ fn client(args: Args) {
|
||||
};
|
||||
|
||||
let functions = parser.functions().unwrap();
|
||||
|
||||
println!("{}", serde_json::to_string(&functions).unwrap());
|
||||
|
||||
let mut server_functions: HashMap<String, HashMap<String, Box<Node>>> = HashMap::new();
|
||||
for function in functions.clone() {
|
||||
match *function.clone().1 {
|
||||
@ -294,7 +384,23 @@ fn client(args: Args) {
|
||||
}
|
||||
|
||||
let encryption = generate_encryption();
|
||||
let private_key = Encryption::get_private_key();
|
||||
|
||||
let config = if Path::new("gpsl_conf.toml").exists() {
|
||||
Config::from_file("gpsl_conf.toml")
|
||||
} else {
|
||||
let private_key = Encryption::get_private_key();
|
||||
let config = Config {
|
||||
private_key: Some(private_key),
|
||||
public_key: Some(encryption.get_public_key(private_key)),
|
||||
};
|
||||
|
||||
let mut file = File::create("gpsl_conf.toml").unwrap();
|
||||
let config_file = ConfigFile::from_config(config.clone());
|
||||
file.write_all(serde_json::to_string(&config_file).unwrap().as_bytes())
|
||||
.unwrap();
|
||||
|
||||
config
|
||||
};
|
||||
|
||||
let mut gpsl = GPSL::new(
|
||||
source,
|
||||
@ -302,8 +408,8 @@ fn client(args: Args) {
|
||||
Some(server_functions),
|
||||
Some(servers),
|
||||
encryption.clone(),
|
||||
Some(private_key),
|
||||
Some(encryption.get_public_key(private_key)),
|
||||
config.private_key,
|
||||
config.public_key,
|
||||
vec![STD_FUNC],
|
||||
);
|
||||
let res = gpsl.run("main".to_string(), HashMap::new());
|
||||
|
Reference in New Issue
Block a user