1 // boost/uuid/sha1.hpp header file ----------------------------------------------//
3 // Copyright 2007 Andy Tompkins.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
9 // 29 May 2007 - Initial Revision
10 // 25 Feb 2008 - moved to namespace boost::uuids::detail
12 // This is a byte oriented implementation
13 // Note: this implementation does not handle message longer than
16 #ifndef BOOST_UUID_SHA1_H
17 #define BOOST_UUID_SHA1_H
19 #include <boost/static_assert.hpp>
22 #ifdef BOOST_NO_STDC_NAMESPACE
32 BOOST_STATIC_ASSERT(sizeof(unsigned char)*8 == 8);
33 BOOST_STATIC_ASSERT(sizeof(unsigned int)*8 == 32);
35 inline unsigned int left_rotate(unsigned int x, std::size_t n)
37 return (x<<n) ^ (x>> (32-n));
43 typedef unsigned int(&digest_type)[5];
49 void process_byte(unsigned char byte);
50 void process_block(void const* bytes_begin, void const* bytes_end);
51 void process_bytes(void const* buffer, std::size_t byte_count);
53 void get_digest(digest_type digest);
61 unsigned char block_[64];
63 std::size_t block_byte_index_;
64 std::size_t byte_count_;
72 inline void sha1::reset()
80 block_byte_index_ = 0;
84 inline void sha1::process_byte(unsigned char byte)
86 block_[block_byte_index_++] = byte;
88 if (block_byte_index_ == 64) {
89 block_byte_index_ = 0;
94 inline void sha1::process_block(void const* bytes_begin, void const* bytes_end)
96 unsigned char const* begin = static_cast<unsigned char const*>(bytes_begin);
97 unsigned char const* end = static_cast<unsigned char const*>(bytes_end);
98 for(; begin != end; ++begin) {
103 inline void sha1::process_bytes(void const* buffer, std::size_t byte_count)
105 unsigned char const* b = static_cast<unsigned char const*>(buffer);
106 process_block(b, b+byte_count);
109 inline void sha1::process_block()
112 for (std::size_t i=0; i<16; ++i) {
113 w[i] = (block_[i*4 + 0] << 24);
114 w[i] |= (block_[i*4 + 1] << 16);
115 w[i] |= (block_[i*4 + 2] << 8);
116 w[i] |= (block_[i*4 + 3]);
118 for (std::size_t i=16; i<80; ++i) {
119 w[i] = left_rotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1);
122 unsigned int a = h_[0];
123 unsigned int b = h_[1];
124 unsigned int c = h_[2];
125 unsigned int d = h_[3];
126 unsigned int e = h_[4];
128 for (std::size_t i=0; i<80; ++i) {
133 f = (b & c) | (~b & d);
139 f = (b & c) | (b & d) | (c & d);
146 unsigned temp = left_rotate(a, 5) + f + e + k + w[i];
149 c = left_rotate(b, 30);
161 inline void sha1::get_digest(digest_type digest)
163 std::size_t bit_count = byte_count_*8;
165 // append the bit '1' to the message
168 // append k bits '0', where k is the minimum number >= 0
169 // such that the resulting message length is congruent to 56 (mod 64)
170 // check if there is enough space for padding and bit_count
171 if (block_byte_index_ > 56) {
173 while (block_byte_index_ != 0) {
178 while (block_byte_index_ < 56) {
182 while (block_byte_index_ < 56) {
187 // append length of message (before pre-processing)
188 // as a 64-bit big-endian integer
193 process_byte( static_cast<unsigned char>((bit_count>>24) & 0xFF));
194 process_byte( static_cast<unsigned char>((bit_count>>16) & 0xFF));
195 process_byte( static_cast<unsigned char>((bit_count>>8 ) & 0xFF));
196 process_byte( static_cast<unsigned char>((bit_count) & 0xFF));
206 }}} // namespace boost::uuids::detail