From e55dcd0c278c628725ece12af4620358802bcda0 Mon Sep 17 00:00:00 2001 From: atsushi130 Date: Thu, 1 Jun 2017 08:27:58 +0900 Subject: [PATCH 1/3] update package version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dea6436..6696a41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ [root] name = "cryptor" -version = "0.1.3" +version = "0.1.4" dependencies = [ "base64 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 5cbd15f..e8dba6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptor" -version = "0.1.3" +version = "0.1.4" authors = ["atsushi130 "] license = "MIT/Apache-2.0" description = "Cryptor is encryption machine corresponding to the diversity of algorithms." From c0274ab5ecb4fb4515df3ebfcd40b1cae2793229 Mon Sep 17 00:00:00 2001 From: atsushi130 Date: Thu, 1 Jun 2017 08:28:12 +0900 Subject: [PATCH 2/3] update each documentation --- README.md | 39 ++++++++++++++++++-------- src/cryptor/algorithm/base64/README.md | 14 +++++---- src/cryptor/algorithm/enigma/README.md | 21 +++++++++----- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 509a8d6..b1d624a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![MIT / Apache2.0 dual licensed](https://img.shields.io/badge/dual%20license-MIT%20/%20Apache%202.0-blue.svg)](./LICENSE-MIT.md) [![Travis Build Status](https://api.travis-ci.org/atsushi130/Cryptor.svg?branch=master)](https://travis-ci.org/atsushi130/Cryptor) [![crates.io](https://img.shields.io/crates/v/cryptor.svg)](https://crates.io/crates/cryptor) -[![Document](https://img.shields.io/badge/Cryptor-Document-3B5998.svg)](https://docs.rs/cryptor/0.1.3/cryptor/) +[![Document](https://img.shields.io/badge/Cryptor-Document-3B5998.svg)](https://docs.rs/cryptor/0.1.4/cryptor/) Cryptor is encryption machine corresponding to the diversity of algorithms. @@ -10,7 +10,7 @@ Cryptor is encryption machine corresponding to the diversity of algorithms. Insert to Cargo.toml of your project. ```toml [dependencies] -cryptor = "0.1.3" +cryptor = "0.1.4" ``` or ```console @@ -18,7 +18,7 @@ or ❯ cargo add cryptor // Version specification -❯ cargo add cryptor@0.1.3 +❯ cargo add cryptor@0.1.4 // If not exist on crates.io ❯ mkdir lib @@ -77,21 +77,38 @@ println!("decrypted string is {}", decrypted.text); ``` ## Change logs -**v0.1.3** -Defined associated function to builds new Cryptor. +**v0.1.4** +Add exception of Cryptor and Enigma. + +**CryptoError** +```rust +pub enum CryptoError { + InvalidString(String), + InvalidByte(usize, u8, DecodeError), + InvalidLength(DecodeError), + UTF8Error(FromUtf8Error) +} +``` + +**EnigmaError** ```rust -impl Cryptor { - pub fn new(algorithm: T) -> Self { - Cryptor { - algorithm - } - } +pub enum EnigmaError { + InvalidLength } ``` **changed usage** ```rust let mut cryptor = Cryptor::new(your_algorithm); +match cryptor.encrypt(string) { + Ok(ref crypted) => println!("crypted: {}", crypted.text), + Err(ref error) => println!("{}", error) +} + +match cryptor.decrypt(string) { + Ok(ref crypted) => println!("crypted: {}", crypted.text), + Err(ref error) => println!("{}", error) +} ``` ## LICENSE diff --git a/src/cryptor/algorithm/base64/README.md b/src/cryptor/algorithm/base64/README.md index c1276d4..ac3a1d4 100644 --- a/src/cryptor/algorithm/base64/README.md +++ b/src/cryptor/algorithm/base64/README.md @@ -1,6 +1,6 @@ # Base64 [![Base64](https://img.shields.io/badge/Cryptor-Base64-6fb536.svg)](https://github.com/atsushi130/Cryptor/tree/master/src/cryptor/algorithm/base64) -[![Document](https://img.shields.io/badge/Base64-Document-3B5998.svg)](https://docs.rs/cryptor/0.1.3/cryptor/cryptor/struct.Base64.html) +[![Document](https://img.shields.io/badge/Base64-Document-3B5998.svg)](https://docs.rs/cryptor/0.1.4/cryptor/cryptor/struct.Base64.html) ## Usage **Import modules** @@ -17,13 +17,17 @@ let mut cryptor = Cryptor::new(Base64); **Encryption** ```rust -let encrypted: CryptoValue = cryptor.encrypt(&"A quick brown fox jumps over the lazy dog."); -println!("encrypted: {}", encrypted.text); +match cryptor.encrypt("A quick brown fox jumps over the lazy dog.") { + Ok(ref crypted) => println!("crypted: {}", crypted.text), + Err(ref error) => println!("{}", error) +} ``` **Decryption** ```rust -let decrypted: CryptoValue> = cryptor.encrypt(&encrypted); -println!("decrypted: {}", decrypted); // decrypted: A quick brown fox jumps over the lazy dog. +match cryptor.decrypt(&string) { + Ok(ref crypted) => println!("crypted: {}", crypted.text), // crypted: A quick brown fox jumps over the lazy dog. + Err(ref error) => println!("{}", error) +} ``` diff --git a/src/cryptor/algorithm/enigma/README.md b/src/cryptor/algorithm/enigma/README.md index b1b778f..11e3599 100644 --- a/src/cryptor/algorithm/enigma/README.md +++ b/src/cryptor/algorithm/enigma/README.md @@ -1,6 +1,6 @@ # Enigma [![Enigma](https://img.shields.io/badge/Cryptor-Enigma-6fb536.svg)](https://github.com/atsushi130/Cryptor/tree/master/src/cryptor/algorithm/enigma) -[![Document](https://img.shields.io/badge/Enigma-Document-3B5998.svg)](https://docs.rs/cryptor/0.1.3/cryptor/cryptor/struct.Enigma.html) +[![Document](https://img.shields.io/badge/Enigma-Document-3B5998.svg)](https://docs.rs/cryptor/0.1.4/cryptor/cryptor/struct.Enigma.html) ## Usage **Import modules** @@ -32,18 +32,25 @@ let mut cryptor = Cryptor::new(enigma); **Encryption** ```rust -let encrypted: CryptoValue = cryptor.encrypt(&"A quick brown fox jumps over the lazy dog."); -println!("encrypted: {}", encrypted.text); +match cryptor.encrypt(&"A quick brown fox jumps over the lazy dog.") { + Ok(ref crypted) => println!("crypted: {}", crypted.text), + Err(ref error) => println!("{}", error) +} ``` **Decryption** ```rust -let decrypted: CryptoValue = cryptor.encrypt(&encrypted); -println!("decrypted: {}", decrypted); // decrypted: A quick brown fox jumps over the lazy dog. +match cryptor.decrypt(&string) { + Ok(ref crypted) => println!("crypted: {}", crypted.text), // crypted: A quick brown fox jumps over the lazy dog. + Err(ref error) => println!("{}", error) +} ``` **Set router's position** To set the position, specify a character string. The length of the specified string must be equal to the number of routers. -``` -cryptor.algorithm.set_positons("ABC"); +```rust +match cryptor.algorithm.set_positons("ABC") { + Ok(_) => println!("set positions."), + Err(ref error) => println!("{}", error) +} ``` From 65b5e453f9b9a2c3a4beaf8e7055bae74a21d8f3 Mon Sep 17 00:00:00 2001 From: atsushi130 Date: Thu, 1 Jun 2017 08:40:34 +0900 Subject: [PATCH 3/3] fix README --- README.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b1d624a..7f57ca0 100644 --- a/README.md +++ b/README.md @@ -37,15 +37,15 @@ or Import modules ```rust extern crate cryptor; -use cryptor::cryptor::{ Cryptor, CryptoValue, Algorithm }; +use cryptor::cryptor::{ Cryptor, CryptoValue, CryptoError, Algorithm }; ``` Implement structure with this Algorithm trait. ```rust pub trait Algorithm { type V: Algorithm; - fn encrypt(&mut self, character: &char) -> CryptoValue; - fn decrypt(&mut self, character: &char) -> CryptoValue; + fn encrypt(&mut self, character: &char) -> Result, CryptoError>; + fn decrypt(&mut self, character: &char) -> Result, CryptoError>; } ``` @@ -54,13 +54,17 @@ Cryptor have member with Algorithm trait. Dependency injection your implemented let mut cryptor = Cryptor::new(YourAlgorithm); ``` -Return type of encrypt and decrypt method is `CryptoValue`. +Return type of encrypt and decrypt method is `Result, CryptoError>`. ```rust -let encrypted: CryptoValue = cryptor.encrypt(&string); -println!("encrypted string is {}", encrypted.text); +match cryptor.encrypt(&string) { + Ok(ref crypted) => println!("crypted: {}", crypted.text), + Err(ref error) => println!("{}", error) +} -let decrypted: CryptoValue = cryptor.decrypt(&string); -println!("decrypted string is {}", decrypted.text); +match cryptor.decrypt(&string) { + Ok(ref crypted) => println!("crypted: {}", crypted.text), + Err(ref error) => println!("{}", error) +} ``` ## Run