Skip to content

Commit

Permalink
Merge pull request #17 from troian/digest_to_str
Browse files Browse the repository at this point in the history
feat: digest to_string
  • Loading branch information
troian authored Oct 17, 2019
2 parents 42d4c34 + f3da064 commit c68b967
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation d
set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")

find_package(OpenSSL REQUIRED)
if (NOT WIN32)
if (NOT WIN32 AND NOT JsonCPP_FOUND)
find_package(PkgConfig REQUIRED)
pkg_check_modules(JsonCPP REQUIRED jsoncpp)
endif (NOT WIN32)
endif ()

include_directories(SYSTEM ${OPENSSL_INCLUDE_DIR})
include_directories(SYSTEM ${JsonCPP_INCLUDE_DIRS})
Expand Down
2 changes: 2 additions & 0 deletions include/export/josepp/digest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class digest final {

uint8_t *data();

std::string to_string() const;

public:
static const EVP_MD *md(digest::type t) {
switch (t) {
Expand Down
1 change: 0 additions & 1 deletion include/export/josepp/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#pragma once

#include <json/json.h>
#include <json/value.h>

#include <string>

Expand Down
28 changes: 18 additions & 10 deletions src/digest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <sstream>
#include <iomanip>

#include <josepp/digest.hpp>

#include <openssl/sha.h>

namespace jose {

digest::digest(digest::type type, const uint8_t *in_data, size_t in_size) :
_size(SHA256_DIGEST_LENGTH)
, _data(nullptr)
{
digest::digest(digest::type type, const uint8_t *in_data, size_t in_size)
: _size(SHA256_DIGEST_LENGTH)
, _data(nullptr) {
try {
_data = std::shared_ptr<uint8_t>(new uint8_t[SHA512_DIGEST_LENGTH], std::default_delete<uint8_t[]>());
} catch (...) {
Expand Down Expand Up @@ -90,19 +92,25 @@ digest::digest(digest::type type, const uint8_t *in_data, size_t in_size) :
}
}

digest::~digest()
{
digest::~digest() {
std::memset(_data.get(), 0, _size);
}

size_t digest::size() const
{
size_t digest::size() const {
return _size;
}

uint8_t *digest::data()
{
uint8_t *digest::data() {
return _data.get();
}

std::string digest::to_string() const {
std::stringstream s;
for (size_t i = 0; i < size() / 2; ++i) {
s << std::hex << std::setfill('0') << std::setw(2) << (_data.get()[i * 2] << 8 | _data.get()[(i * 2) + 1]);
}

return s.str();
}

} // namespace jose

0 comments on commit c68b967

Please sign in to comment.