Skip to content

Commit

Permalink
Merge pull request #6 from muflihun/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
abumq authored Sep 26, 2017
2 parents 1ac6a7b + cfcfd76 commit 76a335c
Show file tree
Hide file tree
Showing 19 changed files with 2,213 additions and 439 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## [1.1.0] - 26-09-2017
### Updates
- Moved `Base16::toRawString` to `MineCommon::byteArrayToRawString`
- Added `MineCommon::rawStringToByteArray`
- Renamed `BigIntegerHelper` to `MathHelper`

## [1.0.0] - 05-09-2017
### Added
- Initial release
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ project(Mine)
option (test_main_header "Test main header (mine.h)" OFF)
option (test_wstring_conversions "Test std::wstring (wchar_t*) conversions for encodings" ON)

set (MINE_VERSION "1.0.0")
set (MINE_SOVERSION "1.0.0")

set (MINE_VERSION "1.1.0") ## Also update build.php
set (MINE_SOVERSION "1.1.0")

add_definitions (-DMINE_VERSION="${MINE_VERSION}")

Expand Down Expand Up @@ -150,6 +151,7 @@ else()
add_executable(mine-unit-tests
test/main.cc
src/mine-common.cc
src/big-integer.cc
src/rsa.cc
src/aes.cc
src/base16.cc
Expand Down
42 changes: 39 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

![banner]

Mine is fast, single-header minimal cryptography implementation for small-medium projects that cannot afford to link to external libraries.
Mine is fast, memory-efficient, single-header minimal cryptography implementation for small-medium projects that cannot afford to link to external libraries.

[![Build Status](https://img.shields.io/travis/muflihun/mine/master.svg)](https://travis-ci.org/muflihun/mine)
[![Build Status](https://img.shields.io/travis/muflihun/mine/develop.svg)](https://travis-ci.org/muflihun/mine)
[![Version](https://img.shields.io/github/release/muflihun/mine.svg)](https://github.com/muflihun/mine/releases/latest)
[![Documentation](https://img.shields.io/badge/docs-doxygen-blue.svg)](https://muflihun.github.io/mine)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/muflihun/mine/blob/master/LICENCE)
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/MuflihunDotCom/25)

# Overview
It all started with [ripe](https://github.com/muflihun/ripe) that depends on third-party library (initially OpenSSL then Crypto++) linked statically. However after deploying [residue](https://github.com/muflihun/residue) with ripe to older distributions of linux, we realized that portability is an issue for ripe as _minimal_ library. So we started to implement standards forming _Mine_.
It all started with [ripe](https://github.com/muflihun/ripe) that depends on third-party library (initially OpenSSL then Crypto++) linked statically. However after deploying [residue](https://github.com/muflihun/residue) with ripe to older distributions of linux, we learnt that portability is an issue for ripe as _minimal_ library (because of it's dependencies). So we started to implement standards forming _Mine_.

We are very careful with our implementations and have [unit tests](/test/) in place.
We are very careful with our implementations and have more than 50 [test cases](/test/) in-place.

# Installation
Simply copy `mine.h` and `mine.cc` from [`package/`](/package/) directory to your project or your local machine.
Expand All @@ -38,6 +39,41 @@ This is what we are aiming for _minimal_ crypto library.
* RSA needs big number implementation, for unit tests we use [Integer from Crypto++](https://www.cryptopp.com/wiki/Integer)
* RSA currently does not support signing & verification or reading keys from PEM files

# Quick Reference

### Base16

* `mine::Base16::encode(str);`
* `mine::Base16::encode(str.begin(), str.end());`
* `mine::Base16::decode(encoding);`

### Base64

* `mine::Base64::encode(str);`
* `mine::Base64::encode(str.begin(), str.end());`
* `mine::Base64::decode(encoding);`
* `mine::Base64::decode(encoding.begin(), encoding.end());`
* `mine::Base64::expectedLength(n);`

### AES

```c++
std::string random256BitKey = mine::AES::generateRandomKey(256);

mine::AES aesManager;
aesManager.encrypt(b16Input, hexKey, mine::MineCommon::Encoding::Base16, mine::MineCommon::Encoding::Base64); // takes base16, encrypts and returns base64

aesManager.setKey(random256BitKey); // now use this key
aesManager.encr(b16Input, mine::MineCommon::Encoding::Base16, mine::MineCommon::Encoding::Base64); // don't need key with requests
aesManager.decr(b64Input, mine::MineCommon::Encoding::Base64, mine::MineCommon::Encoding::Raw); // Returns raw string
```

### ZLib

* `mine::ZLib::compressString(str);`
* `mine::ZLib::decompressString(str);`
* `mine::ZLib::decompressFile(outputFile, inputFile);`

# Contribution
You can contribute to the project by testing on various platforms (e.g, Windows, Android etc)

Expand Down
4 changes: 3 additions & 1 deletion build.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// modules for ease of development
///

$lib_version = "1.0.0";
$lib_version = "1.1.0";

$header_template = <<<EOT
//
Expand Down Expand Up @@ -86,6 +86,7 @@ function resolveTemplate($template, $includes, $lines, $lib_version, $filename)
"src/base16.h",
"src/base64.h",
"src/aes.h",
// "src/big-integer.h",
"src/rsa.h",
"src/zlib.h",
);
Expand Down Expand Up @@ -128,6 +129,7 @@ function resolveTemplate($template, $includes, $lines, $lib_version, $filename)
"src/base16.cc",
"src/base64.cc",
"src/aes.cc",
// "src/big-integer.cc",
"src/rsa.cc",
"src/zlib.cc",
);
Expand Down
39 changes: 24 additions & 15 deletions package/mine.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// Bismillah ar-Rahmaan ar-Raheem
//
// Mine (1.0.0)
// Mine (1.1.0)
// Single header minimal cryptography library
//
// Copyright (c) 2017 Muflihun Labs
Expand All @@ -14,8 +14,8 @@
// https://muflihun.com
//
#include <algorithm>
#include <random>
#include <iterator>
#include <random>
#include <sstream>
#include <stdexcept>
#include <iostream>
Expand All @@ -30,7 +30,7 @@

using namespace mine;
#ifndef MINE_VERSION
#define MINE_VERSION "1.0.0"
#define MINE_VERSION "1.1.0"
#endif


Expand Down Expand Up @@ -89,6 +89,22 @@ ByteArray MineCommon::generateRandomBytes(const std::size_t len) noexcept
return result;
}

std::string MineCommon::byteArrayToRawString(const ByteArray& input) noexcept
{
std::ostringstream ss;
std::copy(input.begin(), input.end(), std::ostream_iterator<char>(ss));
return ss.str();
}

ByteArray MineCommon::rawStringToByteArray(const std::string& str) noexcept
{
ByteArray byteArr;
for (char c : str) {
byteArr.push_back(static_cast<byte>(c));
}
return byteArr;
}

std::string MineCommon::version() noexcept
{
return MINE_VERSION;
Expand Down Expand Up @@ -120,13 +136,6 @@ ByteArray Base16::fromString(const std::string& hex)
return byteArr;
}

std::string Base16::toRawString(const ByteArray& input)
{
std::ostringstream ss;
std::copy(input.begin(), input.end(), std::ostream_iterator<char>(ss));
return ss.str();
}

void Base16::decode(char a, char b, std::ostringstream& ss)
{
try {
Expand Down Expand Up @@ -761,7 +770,7 @@ ByteArray AES::resolveInputMode(const std::string& input, MineCommon::Encoding i
std::string AES::resolveOutputMode(const ByteArray& input, MineCommon::Encoding outputMode)
{
if (outputMode == MineCommon::Encoding::Raw) {
return Base16::toRawString(input);
return MineCommon::byteArrayToRawString(input);
} else if (outputMode == MineCommon::Encoding::Base16) {
return Base16::encode(input.begin(), input.end());
}
Expand Down Expand Up @@ -1039,31 +1048,31 @@ std::string AES::encr(const std::string& input, MineCommon::Encoding inputEncodi
if (m_key.empty()) {
throw std::runtime_error("Key not set");
}
return encrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding, pkcs5Padding);
return encrypt(input, Base16::encode(MineCommon::byteArrayToRawString(m_key)), inputEncoding, outputEncoding, pkcs5Padding);
}

std::string AES::encr(const std::string& input, std::string& iv, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding, bool pkcs5Padding)
{
if (m_key.empty()) {
throw std::runtime_error("Key not set");
}
return encrypt(input, Base16::encode(Base16::toRawString(m_key)), iv, inputEncoding, outputEncoding, pkcs5Padding);
return encrypt(input, Base16::encode(MineCommon::byteArrayToRawString(m_key)), iv, inputEncoding, outputEncoding, pkcs5Padding);
}

std::string AES::decr(const std::string& input, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding)
{
if (m_key.empty()) {
throw std::runtime_error("Key not set");
}
return decrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding);
return decrypt(input, Base16::encode(MineCommon::byteArrayToRawString(m_key)), inputEncoding, outputEncoding);
}

std::string AES::decr(const std::string& input, const std::string& iv, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding)
{
if (m_key.empty()) {
throw std::runtime_error("Key not set");
}
return decrypt(input, Base16::encode(Base16::toRawString(m_key)), iv, inputEncoding, outputEncoding);
return decrypt(input, Base16::encode(MineCommon::byteArrayToRawString(m_key)), iv, inputEncoding, outputEncoding);
}

ByteArray AES::encr(const ByteArray& input, bool pkcs5Padding)
Expand Down
Loading

0 comments on commit 76a335c

Please sign in to comment.