62 void SHA1::update(std::istream& is) {
63 std::string rest_of_buffer;
64 read(is, rest_of_buffer,
static_cast<std::size_t>(
static_cast<
int>(BLOCK_BYTES) -
static_cast<
int>(buffer.size())));
65 buffer += rest_of_buffer;
68 uint32_t block[BLOCK_INTS];
69 buffer_to_block(buffer, block);
71 read(is, buffer, BLOCK_BYTES);
79 std::string SHA1::final() {
81 const uint64_t total_bits = (transforms * BLOCK_BYTES + buffer.size()) * 8;
84 buffer +=
static_cast<std::string::value_type>(0x80);
85 const std::size_t orig_size = buffer.size();
86 while (buffer.size() < BLOCK_BYTES) {
87 buffer +=
static_cast<
char>(0x00);
90 uint32_t block[BLOCK_INTS];
91 buffer_to_block(buffer, block);
93 if (orig_size > BLOCK_BYTES - 8) {
95 for (
unsigned int i = 0; i < BLOCK_INTS - 2; i++) {
101 block[BLOCK_INTS - 1] =
static_cast<uint32_t>(total_bits & 0x00000000FFFFFFFF);
102 block[BLOCK_INTS - 2] =
static_cast<uint32_t>((total_bits >> 32) & 0x00000000FFFFFFFF);
106 std::ostringstream result;
107 for (
unsigned int i = 0; i < DIGEST_INTS; i++) {
108 result << std::hex << std::setfill(
'0') << std::setw(8);
109 result << (digest[i] & 0xffffffff);
236 void SHA1::buffer_to_block(
const std::string& buffer, uint32_t block[BLOCK_BYTES]) {
238 for (
unsigned int i = 0; i < BLOCK_INTS; i++) {
239 block[i] =
static_cast<uint32_t>((buffer[4 * i + 3] & 0xff) | (buffer[4 * i + 2] & 0xff) << 8 |
240 (buffer[4 * i + 1] & 0xff) << 16 | (buffer[4 * i + 0] & 0xff) << 24);
252 std::vector<
unsigned char> buf;
257 for (std::size_t i = 0, j = 0; i < string.size(); i += 2, j += 1) {
258 hex_byte[0] = string.at(i);
259 hex_byte[1] = string.at(i + 1);
260 char* end_ptr =
nullptr;
261 buf.push_back(
static_cast<
unsigned char>(strtoul(hex_byte, &end_ptr, 16)));