Skip to content

Commit

Permalink
Merge pull request #8 from atsushi130/develop
Browse files Browse the repository at this point in the history
v0.1.2
  • Loading branch information
atsushi authored May 24, 2017
2 parents 3c18ea1 + 6c61514 commit c366b9d
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "cryptor"
version = "0.1.1"
version = "0.1.2"
authors = ["atsushi130 <[email protected]>"]
license = "MIT/Apache-2.0"
description = "Cryptor is encryption machine corresponding to the diversity of algorithms."
readme = "README.md"
repository = "https://github.com/atsushi130/Cryptor"
keywords = ["enigma", "cipher", "encrypt", "decrypt"]
keywords = ["enigma", "cipher", "encrypt", "decrypt", "base64"]

[dependencies]
lazy_static = "0.2.8"
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
[![MIT / Apache2.0 dual licensed](https://img.shields.io/badge/dual%20license-MIT%20/%20Apache%202.0-blue.svg)](./LICENSE.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.1/cryptor/)

Cryptor is encryption machine corresponding to the diversity of algorithms.

## Dependencies
Insert to Cargo.toml of your project.
```toml
[dependencies]
cryptor = "0.1.0"
cryptor = "0.1.2"
```
or
```console
// Newest version
cargo add cryptor

// Version specification
cargo add [email protected].0
cargo add [email protected].2

// If not exist on crates.io
mkdir lib
Expand All @@ -27,8 +28,9 @@ or
cargo add cryptor --path=lib/cryptor/
```

## Default Crypto algorithm
- [Enigma](https://github.com/atsushi130/Cryptor/tree/master/src/cryptor/algorithm/enigma)
## Default crypto algorithm
- [![Enigma](https://img.shields.io/badge/Cryptor-Enigma-6fb536.svg)](https://github.com/atsushi130/Cryptor/tree/master/src/cryptor/algorithm/enigma)
- [![Base64](https://img.shields.io/badge/Cryptor-Base64-6fb536.svg)](https://github.com/atsushi130/Cryptor/tree/master/src/cryptor/algorithm/base64)

## Usage

Expand Down Expand Up @@ -70,7 +72,7 @@ let mut encrypter = Encrypter {
};
```

Return type of encrypt method is `EncryptValue<YourAlgorithm>`;
Return type of encrypt method is `EncryptValue<YourAlgorithm>`.
```rust
let encrypted: EncryptValue<YourAlgorithm> = encrypter.encrypt(&character);
println!("encrypted character is {}", encrypted.text);
Expand Down
28 changes: 28 additions & 0 deletions src/cryptor/algorithm/base64/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Base64
[![Base64](https://img.shields.io/badge/Cryptor-Base64-6fb536.svg)](https://github.com/atsushi130/Cryptor/tree/master/src/cryptor/algorithm/base64)

## Usage
**Import modules**
```rust
extern crate cryptor;
use cryptor::cryptor::{ Base64 };
use cryptor::cryptor::{ Cryptor, CryptoValue };
```

**Setup Cryptor**
```rust
let mut cryptor = Cryptor { algorithm: Base64 };
```

**Encryption**
```rust
let encrypted: CryptoValue<Base64> = cryptor.encrypt(&"A quick brown fox jumps over the lazy dog.");
println!("encrypted: {}", encrypted.text);
```

**Decryption**
```rust
let decrypted: CryptoValue<Base64>> = cryptor.encrypt(&encrypted);
println!("decrypted: {}", decrypted); // decrypted: A quick brown fox jumps over the lazy dog.
```

31 changes: 31 additions & 0 deletions src/cryptor/algorithm/base64/base64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

// Copyright (c) 2017 Atsushi Miyake
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or http://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.

/// data_encoding module
use base64::{ encode, decode };

/// algorithm module
use super::super::{Algorithm, CryptoValue};

pub struct Base64;

impl Algorithm for Base64 {

type V = Base64;

fn encrypt(&mut self, string: &str) -> CryptoValue<Self::V> {
let bytes = string.as_bytes();
let encoded = encode(&bytes);
CryptoValue::new(&encoded)
}

fn decrypt(&mut self, string: &str) -> CryptoValue<Self::V> {
let bytes = decode(string).unwrap();
let decoded = String::from_utf8(bytes).unwrap();
CryptoValue::new(&decoded)
}
}
9 changes: 9 additions & 0 deletions src/cryptor/algorithm/base64/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

// Copyright (c) 2017 Atsushi Miyake
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or http://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.

mod base64;
pub use self::base64::Base64;
3 changes: 2 additions & 1 deletion src/cryptor/algorithm/enigma/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Enigma
[![Enigma](https://img.shields.io/badge/Cryptor-Enigma-green.svg)](https://github.com/atsushi130/Cryptor/tree/master/src/cryptor/algorithm/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.1/cryptor/cryptor/struct.Enigma.html)

## Usage
**Import modules**
Expand Down
20 changes: 13 additions & 7 deletions src/cryptor/algorithm/enigma/enigma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// 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.

/// data_encoding module
use base64::{ encode, decode };
/// cryptor module
use super::super::super::Cryptor;

/// algorithm module
use super::super::{Algorithm, CryptoValue};
use super::super::{ Algorithm, CryptoValue, Base64 };

/// enigma module
use super::{ Router, Reflector, RouterManager, Plugboard };
Expand Down Expand Up @@ -73,12 +73,18 @@ impl Enigma {
}

fn encode_base64(&self, string: &str) -> String {
let bytes = string.as_bytes();
encode(&bytes)
let mut cryptor = self.build_base64_cryptor();
cryptor.encrypt(string).text
}

fn decode_base64(&self, string: &str) -> String {
let bytes = decode(string).unwrap();
String::from_utf8(bytes).unwrap()
let mut cryptor = self.build_base64_cryptor();
cryptor.decrypt(string).text
}

fn build_base64_cryptor(&self) -> Cryptor<Base64> {
Cryptor {
algorithm: Base64
}
}
}
3 changes: 3 additions & 0 deletions src/cryptor/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
mod algorithm;
mod crypto_value;
mod enigma;
mod base64;

pub use self::algorithm::Algorithm;
pub use self::crypto_value::CryptoValue;
Expand All @@ -26,3 +27,5 @@ pub use self::enigma::SUBSTITUTION_TABLE2;
pub use self::enigma::SUBSTITUTION_TABLE3;
pub use self::enigma::REFLECTOR;
pub use self::enigma::PLUGBOARD;

pub use self::base64::Base64;
2 changes: 2 additions & 0 deletions src/cryptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ pub use self::algorithm::SUBSTITUTION_TABLE2;
pub use self::algorithm::SUBSTITUTION_TABLE3;
pub use self::algorithm::REFLECTOR;
pub use self::algorithm::PLUGBOARD;

pub use self::algorithm::Base64;
19 changes: 19 additions & 0 deletions tests/cryptor_tests/algorithm/base64/base64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

mod base64_tests {

use cryptor::cryptor::{ Base64, Algorithm };

#[test]
fn encryptable() {
let mut base64 = Base64;
let result = base64.encrypt("A");
assert_eq!("QQ==", result.text);
}

#[test]
fn decryptable() {
let mut base64 = Base64;
let result = base64.decrypt("QQ==");
assert_eq!("A", result.text);
}
}
2 changes: 2 additions & 0 deletions tests/cryptor_tests/algorithm/base64/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

pub mod base64;
3 changes: 2 additions & 1 deletion tests/cryptor_tests/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
// 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.

pub mod enigma;
pub mod enigma;
pub mod base64;

0 comments on commit c366b9d

Please sign in to comment.