add ability to encode/decode keys to/from string

This commit is contained in:
horhik 2020-09-11 01:01:13 +03:00
commit 754edd4725
9 changed files with 270 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

129
Cargo.lock generated Normal file
View File

@ -0,0 +1,129 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "fallible-iterator"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fallible-streaming-iterator"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]]
name = "gcc"
version = "0.3.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
[[package]]
name = "hole"
version = "0.1.0"
dependencies = [
"ntru",
"rusqlite",
"toml",
]
[[package]]
name = "libc"
version = "0.2.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235"
[[package]]
name = "libsqlite3-sys"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a245984b1b06c291f46e27ebda9f369a94a1ab8461d0e845e23f9ced01f5db"
dependencies = [
"pkg-config",
"vcpkg",
]
[[package]]
name = "linked-hash-map"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
[[package]]
name = "lru-cache"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c"
dependencies = [
"linked-hash-map",
]
[[package]]
name = "memchr"
version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "ntru"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1529ecefb499c128db3cfdcff1f30106ed379b00443cd674e7a9afc49962a987"
dependencies = [
"gcc",
"libc",
]
[[package]]
name = "pkg-config"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33"
[[package]]
name = "rusqlite"
version = "0.24.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c78c3275d9d6eb684d2db4b2388546b32fdae0586c20a82f3905d21ea78b9ef"
dependencies = [
"bitflags",
"fallible-iterator",
"fallible-streaming-iterator",
"libsqlite3-sys",
"lru-cache",
"memchr",
"smallvec",
]
[[package]]
name = "serde"
version = "1.0.115"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5"
[[package]]
name = "smallvec"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252"
[[package]]
name = "toml"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a"
dependencies = [
"serde",
]
[[package]]
name = "vcpkg"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c"

12
Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "hole"
version = "0.1.0"
authors = ["horhik <horhik@tuta.io>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ntru = "0.5.6"
rusqlite = "0.24.0"
toml = "0.5.6"

0
myfile.db Normal file
View File

9
src/cli/cli_base.rs Normal file
View File

@ -0,0 +1,9 @@
use std::io;
pub fn get_stdin () -> String {
let mut input = String::new();
io::stdin().read_line(&mut input);
return input
}

3
src/cli/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod cli_base;

View File

@ -0,0 +1,83 @@
use ntru::rand::RNG_DEFAULT;
use ntru::encparams::{DEFAULT_PARAMS_256_BITS};
use ntru::types::{KeyPair, PrivateKey, PublicKey};
use std::str;
extern crate ntru;
pub fn key_to_string(key: &Box<[u8]>) -> String {
let utf = unsafe {str::from_utf8_unchecked(&key)};
let string = String::from(utf);
return String::from(string);
}
pub fn public_key_from_sring (pk: String) -> PublicKey {
let bytes = pk.into_bytes();
let imported = PublicKey::import(&bytes);
return imported
}
pub fn private_key_from_sring (pk: String) -> PrivateKey {
let bytes = pk.into_bytes();
let imported = PrivateKey::import(&bytes);
return imported
}
pub fn generate () -> (String, String, KeyPair) {
let rand_ctx = ntru::rand::init(&RNG_DEFAULT).unwrap();
let kp = ntru::generate_key_pair(&DEFAULT_PARAMS_256_BITS, &rand_ctx).unwrap();
// extracting public and private key from kp
let pub_key = KeyPair::get_public(&kp);
let private_key = KeyPair::get_private(&kp);
// getting pub and priv key params for exporting
let pub_key_params = KeyPair::get_params(&kp).unwrap();
let private_key_params = PrivateKey::get_params(&private_key).unwrap();
// exporting
let pub_key_exported: Box<[u8]> = PublicKey::export(&pub_key, &pub_key_params);
let private_key_exported: Box<[u8]> = PrivateKey::export(&private_key, &private_key_params);
// converting to string
let pub_key_string = key_to_string(&pub_key_exported);
let private_key_string = key_to_string(&private_key_exported);
/*
* It was a test
*
let xp = &PublicKey::import(&pub_key_exported);
let boo = xp == pub_key;
let value = from_utf8(&pub_key_exported).unwrap();
let strVal = String::from(value);
print!(" without converting \n {}", xp == pub_key );
assert_eq!(xp, pub_key);
let newbytes = strVal.into_bytes();
let newkey = PublicKey::import(&newbytes);
print!(" with converting \n {}", &newkey == pub_key);
assert_eq!(&newkey , pub_key);
*/
return (pub_key_string, private_key_string, kp )
}
pub fn main () {
let (public, private, key_pair) = generate();
let initial_pub = KeyPair::get_public(&key_pair);
let initial_priv = KeyPair::get_private(&key_pair);
let final_pub = public_key_from_sring(public);
let final_priv = private_key_from_sring(private);
let bo0 = initial_pub == &final_pub;
let bo1 = initial_priv == &final_priv;
print!("Pub : {}, Priv : {}", bo0, bo1);
assert_eq!(initial_pub, &final_pub);
assert_eq!(initial_priv, &final_priv);
}

1
src/encrypting/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod encryption;

32
src/main.rs Normal file
View File

@ -0,0 +1,32 @@
mod cli;
mod encrypting;
fn main() {
encrypting::encryption::main()
/*
let (one, two) = keys;
let value =String::from_utf8_lossy(&*one);
let strVal = String::from(value);
let newbytes = strVal.into_bytes();
print!("{:?}", newbytes);
let newkey = PrivateKey::import(newbytes);
let conn = Connection::open("myfile.db").unwrap();
conn.execute("CREATE TABLE person (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL
)", NO_PARAMS).unwrap();
let name: String = "Steve Example".to_string();
let email: String = "steve@example.org".to_string();
conn.execute("INSERT INTO person (name, email) VALUES (?1, ?2)",
&[&name, &email]).unwrap();
*/
//let mut std = cli::cli_base::get_stdin();
//print!("{}",std)
}