Skip to content
Permalink
Browse files

Extract hashing function into its own file. It's currently Mac-specif…

…ic code (though it looks very similar to OpenSSL/LibreSSL MD5 hash code, so should probably be easy to get to compile elsewhere).
  • Loading branch information...
uliwitness committed Nov 23, 2016
1 parent 32b3ade commit 3043964890ddf5672b25857a8f34e80e0d57b680
Showing with 59 additions and 22 deletions.
  1. +8 −0 bugmatic.xcodeproj/project.pbxproj
  2. +4 −22 bugmatic/bugmatic.cpp
  3. +29 −0 bugmatic/md5hash.cpp
  4. +18 −0 bugmatic/md5hash.hpp
@@ -12,6 +12,8 @@
55614A781D3A431C0026B9A8 /* json11.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 55614A761D3A431C0026B9A8 /* json11.cpp */; };
55C366A51DE63EB0001DAC25 /* configfile.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 55C366A31DE63EB0001DAC25 /* configfile.cpp */; };
55C366A61DE64913001DAC25 /* configfile.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 55C366A31DE63EB0001DAC25 /* configfile.cpp */; };
55C366A91DE64F95001DAC25 /* md5hash.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 55C366A71DE64F95001DAC25 /* md5hash.cpp */; };
55C366AA1DE650C0001DAC25 /* md5hash.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 55C366A71DE64F95001DAC25 /* md5hash.cpp */; };
55CEAE911DE0469A0099E768 /* fake_filesystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 55CEAE8F1DE0469A0099E768 /* fake_filesystem.cpp */; };
55CEAE991DE0792C0099E768 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 55CEAE981DE0792C0099E768 /* main.cpp */; };
55CEAEA01DE079870099E768 /* bugmatic.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 55CEAE9E1DE079860099E768 /* bugmatic.cpp */; };
@@ -63,6 +65,8 @@
55614A771D3A431C0026B9A8 /* json11.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = json11.hpp; path = json11/json11.hpp; sourceTree = SOURCE_ROOT; };
55C366A31DE63EB0001DAC25 /* configfile.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = configfile.cpp; sourceTree = "<group>"; };
55C366A41DE63EB0001DAC25 /* configfile.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = configfile.hpp; sourceTree = "<group>"; };
55C366A71DE64F95001DAC25 /* md5hash.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = md5hash.cpp; sourceTree = "<group>"; };
55C366A81DE64F95001DAC25 /* md5hash.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = md5hash.hpp; sourceTree = "<group>"; };
55CEAE8F1DE0469A0099E768 /* fake_filesystem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fake_filesystem.cpp; sourceTree = "<group>"; };
55CEAE901DE0469A0099E768 /* fake_filesystem.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = fake_filesystem.hpp; sourceTree = "<group>"; };
55CEAE961DE0792C0099E768 /* bugmatic_tests */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = bugmatic_tests; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -120,6 +124,8 @@
55CEAE8F1DE0469A0099E768 /* fake_filesystem.cpp */,
55C366A41DE63EB0001DAC25 /* configfile.hpp */,
55C366A31DE63EB0001DAC25 /* configfile.cpp */,
55C366A81DE64F95001DAC25 /* md5hash.hpp */,
55C366A71DE64F95001DAC25 /* md5hash.cpp */,
55614A771D3A431C0026B9A8 /* json11.hpp */,
55614A761D3A431C0026B9A8 /* json11.cpp */,
);
@@ -241,6 +247,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
55C366A91DE64F95001DAC25 /* md5hash.cpp in Sources */,
55614A781D3A431C0026B9A8 /* json11.cpp in Sources */,
554408B11D2A9336001EA2BC /* main.cpp in Sources */,
554408B91D2AB1E3001EA2BC /* url_request.cpp in Sources */,
@@ -254,6 +261,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
55C366AA1DE650C0001DAC25 /* md5hash.cpp in Sources */,
55CEAE991DE0792C0099E768 /* main.cpp in Sources */,
55CEAEA31DE08E3B0099E768 /* url_request.cpp in Sources */,
55CEAEA51DE08E420099E768 /* json11.cpp in Sources */,
@@ -20,19 +20,16 @@
#include <cstdio>
#include <cmath>
#include <unistd.h>
#include <CommonCrypto/CommonCrypto.h>
#include "json11.hpp"
#include "url_request.hpp"
#include "fake_filesystem.hpp" // until <filesystem> becomes available.
#include "configfile.hpp"
#include "md5hash.hpp"


#define USER_AGENT "bugmatic/0.1"


#define hex_char(n) setw(2) << setfill('0') << hex << int(n)


using Json = json11::Json;
using namespace std;
using namespace fake;
@@ -55,23 +52,6 @@ string file_contents( ifstream& stream )
}


string hash_string( const std::string& inData )
{
stringstream issueHash;
unsigned char hash[CC_MD5_DIGEST_LENGTH];
CC_MD5( inData.data(), (CC_LONG)inData.size(), hash );
issueHash << hex_char(hash[0]) << hex_char(hash[0]) << hex_char(hash[1]) << hex_char(hash[2]) << hex_char(hash[3]) << hex_char(hash[4]) << hex_char(hash[5]) << hex_char(hash[6]) << hex_char(hash[7]) << hex_char(hash[8]) << hex_char(hash[9]) << hex_char(hash[10]) << hex_char(hash[11]) << hex_char(hash[12]) << hex_char(hash[13]) << hex_char(hash[14]) << hex_char(hash[15]) << dec;
return issueHash.str();
}


string hash_md5_file( const std::string& inFileName )
{
ifstream issueFileIn(inFileName);
string issueJsonStr = file_contents(issueFileIn);
return hash_string( issueJsonStr );
}

void paged_cached_download( string url, string fname, string userName, string password, bool ignoreCache, std::function<void(string)> fileContentsCallback )
{
ifstream downloadedjson;
@@ -677,7 +657,9 @@ void working_copy::pull( const remote& inRemote )
// Already have a file of this name?
if( filesystem::exists(filesystem::path(issuefilename.str())) )
{
string fileHash = hash_md5_file( issuefilename.str() );
ifstream issueFileIn(issuefilename.str());
string issueJsonStr = file_contents(issueFileIn);
string fileHash = hash_string( issueJsonStr );
string pristineHash = hashesFile.value_for_key(to_string(bugNumber));
if( pristineHash != fileHash )
{
@@ -0,0 +1,29 @@
//
// md5hash.cpp
// bugmatic
//
// Created by Uli Kusterer on 23/11/2016.
// Copyright © 2016 Uli Kusterer. All rights reserved.
//

#include "md5hash.hpp"
#include <sstream>
#include <fstream>
#include <iomanip>
#include <CommonCrypto/CommonCrypto.h>


using namespace std;


#define hex_char(n) setw(2) << setfill('0') << hex << int(n)


string hash_string( const std::string& inData )
{
stringstream issueHash;
unsigned char hash[CC_MD5_DIGEST_LENGTH];
CC_MD5( inData.data(), (CC_LONG)inData.size(), hash );
issueHash << hex_char(hash[0]) << hex_char(hash[0]) << hex_char(hash[1]) << hex_char(hash[2]) << hex_char(hash[3]) << hex_char(hash[4]) << hex_char(hash[5]) << hex_char(hash[6]) << hex_char(hash[7]) << hex_char(hash[8]) << hex_char(hash[9]) << hex_char(hash[10]) << hex_char(hash[11]) << hex_char(hash[12]) << hex_char(hash[13]) << hex_char(hash[14]) << hex_char(hash[15]) << dec;
return issueHash.str();
}
@@ -0,0 +1,18 @@
//
// md5hash.hpp
// bugmatic
//
// Created by Uli Kusterer on 23/11/2016.
// Copyright © 2016 Uli Kusterer. All rights reserved.
//

#ifndef md5hash_hpp
#define md5hash_hpp

#include <string>


std::string hash_string( const std::string& inData );


#endif /* md5hash_hpp */

0 comments on commit 3043964

Please sign in to comment.
You can’t perform that action at this time.