/
Crypt.cc
102 lines (81 loc) · 2.27 KB
/
Crypt.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "Crypt.hh"
#include "StdioFile.hh"
#include "Exception.hh"
#include "MemMap.hh"
#include <iomanip>
#include <sstream>
// dependent libraries
#include <gcrypt.h>
#include <boost/throw_exception.hpp>
namespace gr { namespace crypt {
// map 4MB of data at a time
const u64_t read_size = 1024 * 4096 ;
struct MD5::Impl
{
gcry_md_hd_t hd ;
} ;
MD5::MD5() : m_impl( new Impl )
{
::gcry_error_t err = ::gcry_md_open( &m_impl->hd, GCRY_MD_MD5, 0 ) ;
if ( err != GPG_ERR_NO_ERROR )
{
BOOST_THROW_EXCEPTION( Exception() << expt::ErrMsg( ::gcry_strerror(err) ) ) ;
}
}
MD5::~MD5()
{
::gcry_md_close( m_impl->hd ) ;
}
void MD5::Write( const void *data, std::size_t size )
{
::gcry_md_write( m_impl->hd, data, size ) ;
}
std::string MD5::Get() const
{
unsigned char *md5 = ::gcry_md_read( m_impl->hd, GCRY_MD_MD5 ) ;
unsigned int len = ::gcry_md_get_algo_dlen(GCRY_MD_MD5) ;
// format the MD5 string
std::ostringstream ss ;
for ( unsigned int i = 0 ; i < len ; i++ )
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(md5[i]) ;
return ss.str() ;
}
std::string MD5::Get( const fs::path& file )
{
try
{
StdioFile sfile( file ) ;
return Get( sfile ) ;
}
catch ( StdioFile::Error& )
{
return "" ;
}
}
std::string MD5::Get( StdioFile& file )
{
MD5 crypt ;
u64_t size = file.Size() ;
for ( u64_t i = 0 ; i < size ; i += read_size )
{
MemMap map( file, i, static_cast<std::size_t>(std::min(read_size, size-i)) ) ;
crypt.Write( map.Addr(), map.Length() ) ;
}
return crypt.Get() ;
}
} } // end of namespaces