39 static_assert(
sizeof(
unsigned char)*8 == 8,
"Please reboot the C universe.");
40 static_assert(
sizeof(
unsigned int)*8 == 32,
"sizeof(unsigned int) == 4");
42 #define SHA_DIGEST_LENGTH 20
54 void process_block(
void const* bytes_begin,
void const* bytes_end);
55 void process_bytes(
void const* buffer, std::size_t byte_count);
61 static unsigned int left_rotate(
unsigned int x, std::size_t n)
63 return (x<<n) ^ (x>> (32-n));
69 unsigned char block_[64];
71 std::size_t block_byte_index_;
72 std::size_t byte_count_;
88 block_byte_index_ = 0;
94 block_[block_byte_index_++] = byte;
96 if (block_byte_index_ == 64) {
97 block_byte_index_ = 0;
104 unsigned char const* begin =
static_cast<unsigned char const*
>(bytes_begin);
105 unsigned char const* end =
static_cast<unsigned char const*
>(bytes_end);
106 for(; begin != end; ++begin) {
113 unsigned char const*
b =
static_cast<unsigned char const*
>(buffer);
117 inline void sha1::process_block()
120 for (std::size_t i=0; i<16; ++i) {
121 w[i] = (block_[i*4 + 0] << 24);
122 w[i] |= (block_[i*4 + 1] << 16);
123 w[i] |= (block_[i*4 + 2] << 8);
124 w[i] |= (block_[i*4 + 3]);
126 for (std::size_t i=16; i<80; ++i) {
127 w[i] = left_rotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1);
130 unsigned int a = h_[0];
131 unsigned int b = h_[1];
132 unsigned int c = h_[2];
133 unsigned int d = h_[3];
134 unsigned int e = h_[4];
136 for (std::size_t i=0; i<80; ++i) {
141 f = (
b & c) | (~
b & d);
147 f = (
b & c) | (
b & d) | (c & d);
154 unsigned temp = left_rotate(
a, 5) + f + e + k + w[i];
157 c = left_rotate(
b, 30);
171 std::size_t bit_count = byte_count_*8;
179 if (block_byte_index_ > 56) {
181 while (block_byte_index_ != 0) {
186 while (block_byte_index_ < 56) {
190 while (block_byte_index_ < 56) {
201 process_byte(
static_cast<unsigned char>((bit_count>>24) & 0xFF));
202 process_byte(
static_cast<unsigned char>((bit_count>>16) & 0xFF));
203 process_byte(
static_cast<unsigned char>((bit_count>>8 ) & 0xFF));
204 process_byte(
static_cast<unsigned char>((bit_count) & 0xFF));
unsigned int(& digest_type)[5]
void process_byte(unsigned char byte)
void process_bytes(void const *buffer, std::size_t byte_count)
void process_block(void const *bytes_begin, void const *bytes_end)
void get_digest(digest_type digest)