Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ The Makefile is tailored for g++, but should work with other compilers.

It doesn't matter where you install the git-crypt binary - choose wherever
is most convenient for you.

BUILDING ON WINDOWS

* Install mingw
* Download OpenSSL for Windows
* Put libeay32.lib on mingw lib folder
* Put openssl header´s folder on mingw include folder

$ make

23 changes: 20 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,32 @@ PREFIX := /usr/local

OBJFILES = git-crypt.o commands.o crypto.o util.o

INSTALL = install -m 755 git-crypt $(PREFIX)/bin/

ifeq ($(OS),Windows_NT)
LDFLAGS = -llibeay32 -lwsock32
# CXXFLAGS += -static-libgcc
CXXFLAGS += -static-libgcc -static-libstdc++
OBJFILES = git-crypt.o commands.o crypto.o util_win32.o
INSTALL = cp git-crypt.exe $(PREFIX)/bin/
endif

all: git-crypt

git-crypt: $(OBJFILES)
$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

clean:
rm -f *.o git-crypt
rm -f *.o git-crypt git-crypt.exe \
rm -fr test

install:
install -m 755 git-crypt $(PREFIX)/bin/
$(INSTALL)

test:
./test.sh

strip:
strip git-crypt.exe

.PHONY: all clean install
.PHONY: all clean install test strip
14 changes: 11 additions & 3 deletions commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* as that of the covered work.
*/


#include "commands.hpp"
#include "crypto.hpp"
#include "util.hpp"
Expand All @@ -45,6 +46,13 @@
#include <openssl/rand.h>
#include <openssl/err.h>

#ifdef __WIN32__
#define system(a) win32_system(a)
#else
typedef std::fstream temp_fstream;
#endif


// Encrypt contents of stdin and write to stdout
void clean (const char* keyfile)
{
Expand All @@ -56,7 +64,7 @@ void clean (const char* keyfile)
hmac_sha1_state hmac(keys.hmac, HMAC_KEY_LEN); // Calculate the file's SHA1 HMAC as we go
uint64_t file_size = 0; // Keep track of the length, make sure it doesn't get too big
std::string file_contents; // First 8MB or so of the file go here
std::fstream temp_file; // The rest of the file spills into a temporary file on disk
temp_fstream temp_file; // The rest of the file spills into a temporary file on disk
temp_file.exceptions(std::fstream::badbit);

char buffer[1024];
Expand Down Expand Up @@ -163,7 +171,7 @@ void diff (const char* keyfile, const char* filename)
load_keys(keyfile, &keys);

// Open the file
std::ifstream in(filename);
std::ifstream in(filename, std::ios::binary);
if (!in) {
perror(filename);
std::exit(1);
Expand Down Expand Up @@ -234,7 +242,7 @@ void init (const char* argv0, const char* keyfile)
// git config filter.git-crypt.smudge "git-crypt smudge /path/to/key"
std::string command("git config filter.git-crypt.smudge ");
command += escape_shell_arg(escape_shell_arg(git_crypt_path) + " smudge " + escape_shell_arg(keyfile_path));

if (system(command.c_str()) != 0) {
std::clog << "git config failed\n";
std::exit(1);
Expand Down
9 changes: 7 additions & 2 deletions crypto.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright 2012 Andrew Ayer
*
* This file is part of git-crypt.
Expand Down Expand Up @@ -38,17 +38,22 @@
#include <iostream>
#include <cstring>
#include <cstdlib>
#ifdef __WIN32__
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif

void load_keys (const char* filepath, keys_t* keys)
{
std::ifstream file(filepath);
std::ifstream file(filepath, std::ios::binary);
if (!file) {
perror(filepath);
std::exit(1);
}
char buffer[AES_KEY_BITS/8 + HMAC_KEY_LEN];
file.read(buffer, sizeof(buffer));

if (file.gcount() != sizeof(buffer)) {
std::clog << filepath << ": Premature end of key file\n";
std::exit(1);
Expand Down
4 changes: 4 additions & 0 deletions git-crypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ try {
std::cin.exceptions(std::ios_base::badbit);
std::cout.exceptions(std::ios_base::badbit);

#ifdef __WIN32__
set_cin_cout_binary_mode();
#endif

if (argc < 3) {
print_usage(argv[0]);
return 2;
Expand Down
161 changes: 161 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
use File::Path;
use File::Copy;
use Term::ANSIColor;
use Digest::MD5 qw(md5);

print "\n"."========= GIT-CRYPT TEST ========="."\n";

my $cdir=Cwd::cwd();
my $dir=$cdir."/test";

### Set test folder
if ( -d $dir ) {
rmtree($dir) or die "Cannot remove '$dir' : $!";
}
mkpath($dir) or die "Cannot create '$dir' : $!";
chdir($dir);

### Generate git-crypt key
my $key="test.key";

print "\n";
`git-crypt keygen $key`; !($?) or die;

### Create git repository

print "\n"."========= CREATE GIT ORIGIN REPO ========="."\n";

my $repo="repo";
mkpath($repo) or die "Cannot create '$repo' : $!";
chdir($repo);

print "\n";
my $out=`git init `; !($?) or die;
print $out;

## put some files
my @files_hpp=glob "$cdir/*.hpp";
my @files_cpp=glob "$cdir/*.cpp";

foreach my $f (@files_hpp) { copy "$f", "." or die; }
print "add: *.hpp files"."\n";
foreach my $f (@files_cpp) { copy "$f", "." or die; }
print "add: *.cpp files"."\n";

my $all_hpp = join ' ', @files_hpp;
`tar -zcf "hpp.tgz" $all_hpp 2>&1`; !($?) or die;
print "add: hpp.tgz file"."\n";
my $all_cpp = join ' ', @files_cpp;
`tar -zcf "cpp.tgz" $all_cpp 2>&1`; !($?) or die;
print "add: cpp.tgz file"."\n";

open (FILE, '>>.gitattributes');
print FILE "*.hpp filter=git-crypt diff=git-crypt"."\n";
print FILE "*.cpp filter=git-crypt diff=git-crypt"."\n";
print FILE "*.tgz filter=git-crypt diff=git-crypt"."\n";
close (FILE);
print "set .gitattributes filter for: *.hpp *.cpp *.tgz"."\n";

print "Initialized git-crypt repository with key: $dir/$key"."\n";
`git-crypt init $dir/$key`; !($?) or die;

print "git add & commit files"."\n";
`git add --all`; !($?) or die;
`git commit -m "test git-crypt"`; !($?) or die;

chdir($dir);


print "\n"."======= CLONE GIT ENCRYPTED REPO ======="."\n";

my $clonerepo="clonerepo";

`git clone --quiet file://'$dir/$repo' $clonerepo`; !($?) or die;
print "\n"."Clone Encrypted Git repository in "."$dir/$clonerepo/"."\n";



### TEST

### Test#1
## isencrypted clone repo ?

chdir("$dir/$clonerepo");

print "\n"."======= TEST 1: isencrypted clone repo? ======="."\n";
print "\n";

my @all_files=glob("*.hpp *.cpp *.tgz");
foreach my $file (@all_files) {
print "\"$file\" encrypted?: ";
if ( isencrypted($file) ){
print colored("OK", "green")."\n";
} else{
print colored("FAIL", "red")."\n";
}
}

print "\n";


### Test#2
## decrypt clone repo

print "\n"."======= TEST 2: decrypt clone repo ======="."\n";
print "\n";

print "Initialized git-crypt repository with key: $dir/$key"."\n";
`git-crypt init $dir/$key`; !($?) or die;

print "\n";
foreach my $file (@all_files) {
print "\"$file\" decrypted?: ";
if ( isdecrypted($file) ){
print colored("OK", "green")."\n";
} else{
print colored("FAIL", "red")."\n";
}
}

print "\n";


sub isencrypted {
my $crypthead="\x00GITCRYPT\x00";
my $file = "$dir/$clonerepo/".shift;
open (FILE, $file) or die "Can't open '$file' : $!";
binmode(FILE) or die "Can't binmode '$file' : $!";
my $filehead;
read (FILE, $filehead, 10);
close (FILE);

if ( "$crypthead" eq "$filehead" ) {
return 1;
}
return 0;
}


sub isdecrypted {
my $file = shift;
if ( getmd5("$dir/$repo/$file") eq getmd5("$dir/$clonerepo/$file") ) {
return 1;
}
return 0;
}

sub getmd5 {
my $file = shift;
open (FILE, $file) or die "Can't open '$file' : $!";
binmode(FILE) or die "Can't binmode '$file' : $!";
my $data = <FILE>;
close(FILE);
return md5($data);
}


exit 0;
1 change: 0 additions & 1 deletion util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,3 @@ std::string escape_shell_arg (const std::string& str)
new_str.push_back('"');
return new_str;
}

20 changes: 19 additions & 1 deletion util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,29 @@
#include <string>
#include <ios>
#include <iosfwd>
#include <fstream>

int exec_command (const char* command, std::ostream& output);

int exec_command (const char* command, std::ostream& output);
std::string resolve_path (const char* path);
void open_tempfile (std::fstream&, std::ios_base::openmode);
std::string escape_shell_arg (const std::string&);


#ifdef __WIN32__
int win32_system (const char* command);
void set_cin_cout_binary_mode (void);

class temp_fstream : public std::fstream {
public:
temp_fstream();
void open (const char *fname, std::ios_base::openmode mode);
virtual ~temp_fstream();
private:
char *fileName;
};
#endif


#endif

Loading