Skip to content

Commit

Permalink
Add types.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Warchant committed Jun 24, 2017
1 parent ec60f06 commit dbfa8b9
Show file tree
Hide file tree
Showing 21 changed files with 2,532 additions and 697 deletions.
1 change: 0 additions & 1 deletion irohad/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ add_subdirectory(ametsuchi)
add_subdirectory(api)
add_subdirectory(common)
add_subdirectory(consensus)
add_subdirectory(logger)
add_subdirectory(main)
add_subdirectory(ordering)
add_subdirectory(peer_service)
Expand Down
3 changes: 2 additions & 1 deletion libs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_subdirectory(crypto)
add_subdirectory(datetime)
add_subdirectory(timer)
add_subdirectory(dao)
add_subdirectory(dao)
add_subdirectory(logger)
83 changes: 0 additions & 83 deletions libs/common.hpp

This file was deleted.

33 changes: 31 additions & 2 deletions libs/crypto/crypto.cpp → libs/common/byteutils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,41 @@
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by bogdan on 24.06.17.
//

#ifndef IROHA_BYTEUTILS_H
#define IROHA_BYTEUTILS_H

#include <crypto/base64.hpp>
#include <string>
#include "types.hpp"

namespace iroha {

/**
* Converts given string to the blob of given size.
* @tparam size
* @param s
* @return
*/
template <size_t size>
blob_t<size> to_blob(std::string s) {
if (s.size() != size) throw std::runtime_error("to_blob size mismatch");

blob_t<size> b;
std::copy(s.begin(), s.end(), b.begin());

return b;
}
}

#include "crypto.hpp"
#endif // IROHA_BYTEUTILS_H
105 changes: 105 additions & 0 deletions libs/common/types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_COMMON_HPP
#define IROHA_COMMON_HPP

#include <dirent.h>
#include <array>
#include <cstdio>

/**
* This file defines common types used in iroha.
*
* std::string is convenient to use but it is not safe, because we can not
* guarantee at compile-time fixed length of the string.
*
* For std::array it is possible, so we prefer it over std::string.
*/
namespace iroha {

/**
* Base type which represents blob of fixed size.
*/
template <size_t size_>
class blob_t : public std::array<uint8_t, size_> {
/**
* Dark magic of C++, do not touch pls :)
* author: @warchant
*/
public:
/**
* In compile-time returns size of current blob.
*/
constexpr static size_t size() { return size_; }

/**
* Converts current blob to std::string
*/
std::string to_string() const noexcept {
return std::string{this->begin(), this->end()};
}

/**
* Converts current blob to base64, represented as std::string
*/
std::string to_base64() const noexcept {
return base64_encode(this->data(), size_);
}

/**
* Converts current blob to hex string.
*/
std::string to_hexstring() const noexcept {
std::string res(size_ * 2, 0);
uint8_t front, back;
auto ptr = this->data();
for (uint32_t i = 0, k = 0; i < size_; i++) {
front = (uint8_t)(ptr[i] & 0xF0) >> 4;
back = (uint8_t)(ptr[i] & 0xF);
res[k++] = code[front];
res[k++] = code[back];
}
return res;
}

private:
static const std::string code = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
};

template <size_t size>
using hash_t = blob_t<size>;

// fixed-size hashes
using hash224_t = hash_t<224 / 8>;
using hash256_t = hash_t<256 / 8>;
using hash384_t = hash_t<384 / 8>;
using hash512_t = hash_t<512 / 8>;

namespace ed25519 {
using sig_t = blob_t<64>; // ed25519 sig is 64 bytes length
using pubkey_t = blob_t<32>;
using privkey_t = blob_t<64>;
}

// timestamps
using ts64_t = uint64_t;
using ts32_t = uint32_t;

} // namespace iroha
#endif // IROHA_COMMON_HPP
33 changes: 19 additions & 14 deletions libs/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
add_library(hash STATIC
hash.cpp
hash.hpp
)

target_link_libraries(hash PUBLIC
keccak
)




add_library(crypto STATIC
common.hpp
base64.hpp
hash.hpp
keypair.hpp
crypto.cpp
crypto.hpp
)

target_link_libraries(crypto PUBLIC
keccak
ed25519
optional
any
)
crypto.hpp
ed25519_impl.cpp
)
target_link_libraries(crypto PUBLiC
ed25519
hash
)
30 changes: 0 additions & 30 deletions libs/crypto/base_crypto_provider.hpp

This file was deleted.

Loading

0 comments on commit dbfa8b9

Please sign in to comment.