author | Michael Krelin <hacker@klever.net> | 2009-04-05 13:32:09 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2009-04-05 13:32:09 (UTC) |
commit | 878315238f71307b5b62ed314096f4a7c465bf3e (patch) (unidiff) | |
tree | dad1579d95b1f7189dc6be5cbd66c36cf340cb94 | |
parent | 01eedb36de69f92fc896c525047df78b34f87324 (diff) | |
download | iii-878315238f71307b5b62ed314096f4a7c465bf3e.zip iii-878315238f71307b5b62ed314096f4a7c465bf3e.tar.gz iii-878315238f71307b5b62ed314096f4a7c465bf3e.tar.bz2 |
integrity digest calculation
implemented integrity digest calculation for uploaded files,
thanks to cdavies of eye-fi forums.
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | src/eyetil.cc | 29 | ||||
-rw-r--r-- | src/eyetil.h | 3 |
2 files changed, 31 insertions, 1 deletions
diff --git a/src/eyetil.cc b/src/eyetil.cc index 2fbd687..fe816a6 100644 --- a/src/eyetil.cc +++ b/src/eyetil.cc | |||
@@ -6,2 +6,4 @@ | |||
6 | #include <stdexcept> | 6 | #include <stdexcept> |
7 | #include <algorithm> | ||
8 | #include <numeric> | ||
7 | #include <openssl/md5.h> | 9 | #include <openssl/md5.h> |
@@ -10,3 +12,2 @@ | |||
10 | binary_t& binary_t::from_hex(const std::string& h) { | 12 | binary_t& binary_t::from_hex(const std::string& h) { |
11 | /* TODO: algorithmize */ | ||
12 | std::string::size_type hs = h.length(); | 13 | std::string::size_type hs = h.length(); |
@@ -119 +120,27 @@ bool tarchive_t::read_data_into_fd(int fd) { | |||
119 | } | 120 | } |
121 | |||
122 | #pragma pack(1) | ||
123 | struct block512_t { | ||
124 | enum { words = 512 / sizeof(uint16_t) }; | ||
125 | uint16_t data[words]; | ||
126 | |||
127 | static uint16_t tcpcksum(block512_t& data) { | ||
128 | uint32_t sum = std::accumulate(data.data,data.data+words,0); | ||
129 | while(uint32_t hw = sum>>16) sum = (sum&0xffff)+hw; | ||
130 | return ~sum; | ||
131 | } | ||
132 | |||
133 | }; | ||
134 | #pragma pack() | ||
135 | |||
136 | binary_t integrity_digest(const void *ptr,size_t size,const std::string& ukey) { | ||
137 | binary_t key; key.from_hex(ukey); | ||
138 | std::vector<uint16_t> blksums; blksums.reserve(size/sizeof(block512_t)); | ||
139 | block512_t *db = (block512_t*)ptr, | ||
140 | *de = db + size/sizeof(block512_t); | ||
141 | std::transform( db, de, std::back_inserter(blksums), block512_t::tcpcksum ); | ||
142 | binary_t subject; | ||
143 | subject.from_data((void*)&(blksums.front()),blksums.size()*sizeof(uint16_t)); | ||
144 | std::copy( key.begin(), key.end(), std::back_inserter(subject) ); | ||
145 | return subject.md5(); | ||
146 | } | ||
diff --git a/src/eyetil.h b/src/eyetil.h index 195d24f..378f703 100644 --- a/src/eyetil.h +++ b/src/eyetil.h | |||
@@ -47,2 +47,5 @@ class tarchive_t { | |||
47 | 47 | ||
48 | binary_t integrity_digest(const void *ptr,size_t size, | ||
49 | const std::string& ukey); | ||
50 | |||
48 | #endif /* __EYETIL_H */ | 51 | #endif /* __EYETIL_H */ |