35 std::istringstream is(s);
42 std::string rest_of_buffer;
43 read(is, rest_of_buffer, BLOCK_BYTES - (
int)buffer.size());
44 buffer += rest_of_buffer;
49 buffer_to_block(buffer, block);
51 read(is, buffer, BLOCK_BYTES);
66 std::ostringstream result;
67 for (
unsigned int i = 0; i < DIGEST_INTS; i++)
69 result << std::hex << std::setfill(
'0') << std::setw(8);
70 result << (digest[i] & 0xffffffff);
85 for (
unsigned int i = 0; i < DIGEST_INTS; i++)
87 uint32_t digesttemp = htonl(digest[i]);
88 result.
append((
const char*)&digesttemp,
sizeof(digest[0]));
97void Sha1::createDigest()
100 uint64_t total_bits = (transforms*BLOCK_BYTES + buffer.size()) * 8;
103 buffer += (char)0x80;
104 unsigned int orig_size = (
unsigned int)buffer.size();
105 while (buffer.size() < BLOCK_BYTES)
107 buffer += (char)0x00;
111 buffer_to_block(buffer, block);
113 if (orig_size > BLOCK_BYTES - 8)
116 for (
unsigned int i = 0; i < BLOCK_INTS - 2; i++)
123 block[BLOCK_INTS - 1] = (
uint32_t)total_bits;
124 block[BLOCK_INTS - 2] = (total_bits >> 32);
130 std::ifstream stream(filename.c_str(), std::ios::binary);
133 return checksum.
final();
140 digest[0] = 0x67452301;
141 digest[1] = 0xefcdab89;
142 digest[2] = 0x98badcfe;
143 digest[3] = 0x10325476;
144 digest[4] = 0xc3d2e1f0;
156void Sha1::transform(
uint32_t block[BLOCK_BYTES])
166#define rol(value, bits) (((value) << (bits)) | (((value) & 0xffffffff) >> (32 - (bits))))
167#define blk(i) (block[i&15] = rol(block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i&15],1))
170#define R0(v,w,x,y,z,i) z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v,5); w=rol(w,30);
171#define R1(v,w,x,y,z,i) z += ((w&(x^y))^y) + blk(i) + 0x5a827999 + rol(v,5); w=rol(w,30);
172#define R2(v,w,x,y,z,i) z += (w^x^y) + blk(i) + 0x6ed9eba1 + rol(v,5); w=rol(w,30);
173#define R3(v,w,x,y,z,i) z += (((w|x)&y)|(w&x)) + blk(i) + 0x8f1bbcdc + rol(v,5); w=rol(w,30);
174#define R4(v,w,x,y,z,i) z += (w^x^y) + blk(i) + 0xca62c1d6 + rol(v,5); w=rol(w,30);
270void Sha1::buffer_to_block(
const std::string &buffer,
uint32_t block[BLOCK_BYTES])
273 for (
unsigned int i = 0; i < BLOCK_INTS; i++)
275 block[i] = (buffer[4*i+3] & 0xff)
276 | (buffer[4*i+2] & 0xff)<<8
277 | (buffer[4*i+1] & 0xff)<<16
278 | (buffer[4*i+0] & 0xff)<<24;
283void Sha1::read(std::istream &is, std::string &s,
int maxBuffer)
285 char* sbuf =
new char[maxBuffer];
286 is.read(sbuf, maxBuffer);
287 s.assign(sbuf, (
unsigned int)is.gcount());
#define R1(v, w, x, y, z, i)
#define R2(v, w, x, y, z, i)
#define R0(v, w, x, y, z, i)
#define R3(v, w, x, y, z, i)
#define R4(v, w, x, y, z, i)
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
static const PreallocateType Preallocate
used only to disambiguate constructors
Data & append(const char *str, size_type len)
Appends the specified number of bytes to the end of this Data.
void update(const std::string &s)
static std::string from_file(const std::string &filename)
dcm! – add UnusedChecking(_enum) below;
unsigned long long uint64_t