Skip to content

Commit

Permalink
Added md5 and crc32 hashing utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Herdinger committed Feb 23, 2016
1 parent 54c9561 commit 1985dcf
Show file tree
Hide file tree
Showing 8 changed files with 1,023 additions and 0 deletions.
5 changes: 5 additions & 0 deletions es-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ set(CORE_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/src/ThemeData.h
${CMAKE_CURRENT_SOURCE_DIR}/src/Util.h
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.h
${CMAKE_CURRENT_SOURCE_DIR}/src/hash.h
${CMAKE_CURRENT_SOURCE_DIR}/src/md5.h
${CMAKE_CURRENT_SOURCE_DIR}/src/crc32.h

# Animations
${CMAKE_CURRENT_SOURCE_DIR}/src/animations/Animation.h
Expand Down Expand Up @@ -76,6 +79,8 @@ set(CORE_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/ThemeData.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Util.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/md5.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/crc32.cpp

# Animations
${CMAKE_CURRENT_SOURCE_DIR}/src/animations/AnimationController.cpp
Expand Down
30 changes: 30 additions & 0 deletions es-core/src/Util.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "Util.h"
#include "resources/ResourceManager.h"
#include "platform.h"
#include "md5.h"
#include "crc32.h"
#include <fstream>

namespace fs = boost::filesystem;

Expand Down Expand Up @@ -64,6 +67,33 @@ Eigen::Vector2f roundVector(const Eigen::Vector2f& vec)
return ret;
}

void hashFile(const boost::filesystem::path& path, Hash& algorithm)
{
std::ifstream fileStream(path.generic_string(), std::ios::binary);
if(fileStream.is_open()) {
while(!fileStream.eof()) {
char buffer[4096];
fileStream.read(buffer, 4096);
int bytesRead = fileStream.gcount();
algorithm.add(buffer, bytesRead);
}
}
}

std::string getMd5(const boost::filesystem::path& path)
{
MD5 algorithm;
hashFile(path, algorithm);
return algorithm.getHash();
}

std::string getCrc(const boost::filesystem::path& path)
{
CRC32 algorithm;
hashFile(path, algorithm);
return algorithm.getHash();
}

// embedded resources, e.g. ":/font.ttf", need to be properly handled too
std::string getCanonicalPath(const std::string& path)
{
Expand Down
3 changes: 3 additions & 0 deletions es-core/src/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Eigen::Vector2f roundVector(const Eigen::Vector2f& vec);

float round(float num);

std::string getMd5(const boost::filesystem::path& path);
std::string getCrc(const boost::filesystem::path& path);

std::string getCanonicalPath(const std::string& str);

// example: removeCommonPath("/home/pi/roms/nes/foo/bar.nes", "/home/pi/roms/nes/") returns "foo/bar.nes"
Expand Down
430 changes: 430 additions & 0 deletions es-core/src/crc32.cpp

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions es-core/src/crc32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// //////////////////////////////////////////////////////////
// crc32.h
// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
// see http://create.stephan-brumme.com/disclaimer.html
//

#pragma once

#include "hash.h"
#include <string>

// define fixed size integer types
#ifdef _MSC_VER
// Windows
typedef unsigned __int8 uint8_t;
typedef unsigned __int32 uint32_t;
#else
// GCC
#include <stdint.h>
#endif


/// compute CRC32 hash, based on Intel's Slicing-by-8 algorithm
/** Usage:
CRC32 crc32;
std::string myHash = crc32("Hello World"); // std::string
std::string myHash2 = crc32("How are you", 11); // arbitrary data, 11 bytes
// or in a streaming fashion:
CRC32 crc32;
while (more data available)
crc32.add(pointer to fresh data, number of new bytes);
std::string myHash3 = crc32.getHash();
Note:
You can find code for the faster Slicing-by-16 algorithm on my website, too:
http://create.stephan-brumme.com/crc32/
Its unrolled version is about twice as fast but its look-up table doubled in size as well.
*/
class CRC32 : public Hash
{
public:
/// hash is 4 bytes long
enum { HashBytes = 4 };

/// same as reset()
CRC32();

/// compute CRC32 of a memory block
std::string operator()(const void* data, size_t numBytes);
/// compute CRC32 of a string, excluding final zero
std::string operator()(const std::string& text);

/// add arbitrary number of bytes
void add(const void* data, size_t numBytes);

/// return latest hash as 8 hex characters
std::string getHash();
/// return latest hash as bytes
void getHash(unsigned char buffer[HashBytes]);

/// restart
void reset();

private:
/// hash
uint32_t m_hash;
};
28 changes: 28 additions & 0 deletions es-core/src/hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// //////////////////////////////////////////////////////////
// hash.h
// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
// see http://create.stephan-brumme.com/disclaimer.html
//

#pragma once

#include <string>

/// abstract base class
class Hash
{
public:
/// compute hash of a memory block
virtual std::string operator()(const void* data, size_t numBytes) = 0;
/// compute hash of a string, excluding final zero
virtual std::string operator()(const std::string& text) = 0;

/// add arbitrary number of bytes
virtual void add(const void* data, size_t numBytes) = 0;

/// return latest hash as hex characters
virtual std::string getHash() = 0;

/// restart
virtual void reset() = 0;
};
Loading

0 comments on commit 1985dcf

Please sign in to comment.