Skip to content

Commit

Permalink
Taken baseXX operations out from CipherSet.
Browse files Browse the repository at this point in the history
Created Hash class with hash operations and rewritten entire code to use
it instead hash_t.
  • Loading branch information
GamePad64 committed Apr 6, 2013
1 parent 51f0260 commit 3a262fe
Show file tree
Hide file tree
Showing 23 changed files with 307 additions and 153 deletions.
12 changes: 3 additions & 9 deletions src/common/crypto/CipherSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define CIPHERSET_H_

#include "CryptoTypes.h"
#include "Hash.h"
#include <string>

namespace p2pnet {
Expand All @@ -28,9 +29,9 @@ class CipherSet {

// Cryptographic functions
//! Computes hash from string
virtual hash_t computeHash(const std::string& data) = 0;
virtual Hash computeHash(const std::string& data) = 0;
//! Checks if hash is hash(data)
virtual bool checkHash(const std::string& data, const hash_t& hash) = 0;
virtual bool checkHash(const std::string& data, const Hash& hash) = 0;
//! Generates key pair in BER format
virtual key_pair_t generateKeyPair() = 0;
virtual bool checkKeyPair(key_pair_t key_pair) = 0;
Expand All @@ -41,13 +42,6 @@ class CipherSet {

//virtual key_private_t PEMToPrivKey(std::string key_pem) = 0;
//virtual key_public_t PEMToPubKey(std::string key_pem) = 0;

// Base64 operations
virtual std::string encodeToBase64(std::string data) = 0;
virtual std::string decodeFromBase64(std::string base64) = 0;
// Base58 operations
virtual std::string encodeToBase58(std::string data) = 0;
virtual std::string decodeFromBase58(std::string base64) = 0;
};

} /* namespace crypto */
Expand Down
23 changes: 7 additions & 16 deletions src/common/crypto/CipherSetV1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

#include "CipherSetV1.h"
#include "../../common/crypto/Hash.h"
#include <botan/keccak.h>
#include <botan/rsa.h>

Expand All @@ -22,13 +23,15 @@ namespace crypto {
CipherSetV1::CipherSetV1() {}
CipherSetV1::~CipherSetV1() {}

hash_t CipherSetV1::computeHash(const std::string& data) {
Hash CipherSetV1::computeHash(const std::string& data) {
Botan::Keccak_1600 hasher(KECCAK_LENGTH);
return hasher.process(data);
Hash hash;
hash.fromBinaryVector(hasher.process(data));
return hash;
}

bool CipherSetV1::checkHash(const std::string& data, const hash_t& hash) {
return (this->computeHash(data) == hash);
bool CipherSetV1::checkHash(const std::string& data, const Hash& hash) {
return (computeHash(data).toBinaryVector() == hash.toBinaryVector());
}

key_pair_t CipherSetV1::generateKeyPair() {
Expand Down Expand Up @@ -75,17 +78,5 @@ std::string CipherSetV1::pubKeyToPEM(key_public_t key_public) {
key_public_t CipherSetV1::PEMToPubKey(std::string key_pem) {
}*/

std::string CipherSetV1::encodeToBase64(std::string data) {
Botan::Pipe pipe(new Botan::Base64_Encoder);
pipe.process_msg(data);
return pipe.read_all_as_string(0);
}

std::string CipherSetV1::decodeFromBase64(std::string base64) {
Botan::Pipe pipe(new Botan::Base64_Decoder);
pipe.process_msg(base64);
return pipe.read_all_as_string(0);
}

} /* namespace crypto */
} /* namespace p2pnet */
10 changes: 2 additions & 8 deletions src/common/crypto/CipherSetV1.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class CipherSetV1: public p2pnet::crypto::CipherSet {
const short KECCAK_LENGTH = 512;
const short RSA_LENGTH = 4096;

virtual hash_t computeHash(const std::string& data);
virtual bool checkHash(const std::string& data, const hash_t& hash);
virtual Hash computeHash(const std::string& data);
virtual bool checkHash(const std::string& data, const Hash& hash);
virtual key_pair_t generateKeyPair();
virtual bool checkKeyPair(key_pair_t key_pair);

Expand All @@ -51,12 +51,6 @@ class CipherSetV1: public p2pnet::crypto::CipherSet {

// TODO DSA signature
// TODO Encryptions!

virtual std::string encodeToBase64(std::string data);
virtual std::string decodeFromBase64(std::string base64);

virtual std::string encodeToBase58(std::string data);
virtual std::string decodeFromBase58(std::string base58);
};

} /* namespace crypto */
Expand Down
62 changes: 0 additions & 62 deletions src/common/crypto/CryptoFunc.cpp

This file was deleted.

8 changes: 0 additions & 8 deletions src/common/crypto/CryptoTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ typedef struct {
key_private_t key_private;
} key_pair_t;

//! Type for storing results of hash-functions
typedef std::vector<Botan::byte, Botan::secure_allocator<Botan::byte> > hash_t;

//! Hamming distance of two hash_t
unsigned short hashDistance(hash_t hash1, hash_t hash2);
//! Returns Hex value of hash
std::string hashToHex(hash_t hash);
std::string hashToB58(hash_t hash);
} /* namespace crypto */
} /* namespace p2pnet */

Expand Down
100 changes: 100 additions & 0 deletions src/common/crypto/Hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "Hash.h"
#include "base58.h"
#include "base64.h"
#include <sstream>
#include <iomanip>

namespace p2pnet {
namespace crypto {

Hash::Hash() {}
Hash::~Hash() {}

void Hash::fromBinaryVector(hash_t hash_vector){
hash = hash_vector;
}

const Hash::hash_t Hash::toBinaryVector() const {
return hash;
}

void Hash::fromBinaryString(std::string hash_string) {
hash = hash_t(hash_string.begin(), hash_string.end());
}

const std::string Hash::toBinaryString() const {
std::string s = std::string(hash.begin(), hash.end());
return s;
}

void Hash::fromHex(std::string hex_string) {
//FIXME: Well, later.
// Use strtoul(), Luke! It is in <cstdlib>, you know :)
}

const std::string Hash::toHex() const {
std::ostringstream hash_ss;

hash_ss << std::hex << std::uppercase << std::setfill('0');
for( int twobytes : hash ) {
hash_ss << std::setw(2) << twobytes;
}

return hash_ss.str();
}

void Hash::fromBase58(std::string hash_string) {
fromBinaryString(decodeFromBase58(hash_string));
}

const std::string Hash::toBase58() const {
return encodeToBase58(toBinaryString());
}

void Hash::fromBase64(std::string hash_string) {
fromBinaryString(decodeFromBase58(hash_string));
}

const std::string Hash::toBase64() const {
return encodeToBase64(toBinaryString());
}

unsigned short Hash::computeDistance(Hash rhash) {
unsigned short distance = 0;

hash_t rhash_vector = rhash.toBinaryVector();

auto char1 = hash.begin();
auto char2 = rhash_vector.begin();

unsigned char comp_value;

while(char1 != hash.end() && char2 != rhash_vector.end()){
comp_value = (*char1) ^ (*char2);
while(comp_value){
++distance;
comp_value &= comp_value - 1;
}
++char1;
++char2;
}

return distance;
}

} /* namespace crypto */
} /* namespace p2pnet */
54 changes: 54 additions & 0 deletions src/common/crypto/Hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef HASH_H_
#define HASH_H_

#include <vector>
#include <string>
#include <botan/botan.h>

namespace p2pnet {
namespace crypto {

class Hash {
public:
Hash();
virtual ~Hash();

typedef std::vector<Botan::byte, Botan::secure_allocator<Botan::byte> > hash_t;

void fromBinaryVector(hash_t hash_vector);
const hash_t toBinaryVector() const;

void fromBinaryString(std::string hash_string);
const std::string toBinaryString() const;

void fromHex(std::string hex_string);
const std::string toHex() const;

void fromBase58(std::string hash_string);
const std::string toBase58() const;

void fromBase64(std::string hash_string);
const std::string toBase64() const;

unsigned short computeDistance(Hash rhash);
private:
hash_t hash;
};

} /* namespace crypto */
} /* namespace p2pnet */
#endif /* HASH_H_ */
13 changes: 4 additions & 9 deletions src/common/crypto/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef BASE58_H_
#define BASE58_H_

// Botan::BigInt is really good enough for that. Pretty good BigInt library.
#include <botan/bigint.h>
#include <string>
#include <deque>
#include "CipherSetV1.h"
#include "base58.h"

namespace p2pnet {
namespace crypto {

std::string base58_alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";

std::string CipherSetV1::encodeToBase58(std::string data) {
std::string encodeToBase58(std::string data) {
Botan::BigInt big_data = Botan::BigInt::decode(std::vector<unsigned char>(data.begin(), data.end()));

Botan::BigInt base_count = base58_alphabet.length();
Expand All @@ -45,7 +42,7 @@ std::string CipherSetV1::encodeToBase58(std::string data) {
return std::string(result.begin(), result.end());
}

std::string CipherSetV1::decodeFromBase58(std::string base58) {
std::string decodeFromBase58(std::string base58) {
int len = base58.length();

Botan::BigInt big_data = 0;
Expand All @@ -62,5 +59,3 @@ std::string CipherSetV1::decodeFromBase58(std::string base58) {

}
}

#endif /* BASE58_H_ */

0 comments on commit 3a262fe

Please sign in to comment.