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
11 // 10 Jan 2012 - can now handle the full size of messages (2^64 - 1 bits)
13 // This is a byte oriented implementation
15 #ifndef BOOST_UUID_SHA1_H
16 #define BOOST_UUID_SHA1_H
18 #include <boost/static_assert.hpp>
19 #include <boost/throw_exception.hpp>
20 #include <boost/uuid/uuid.hpp> // for version
25 #ifdef BOOST_NO_STDC_NAMESPACE
35 BOOST_STATIC_ASSERT(sizeof(unsigned char)*8 == 8);
36 BOOST_STATIC_ASSERT(sizeof(unsigned int)*8 == 32);
38 inline unsigned int left_rotate(unsigned int x, std::size_t n)
40 return (x<<n) ^ (x>> (32-n));
46 typedef unsigned int(digest_type)[5];
52 void process_byte(unsigned char byte);
53 void process_block(void const* bytes_begin, void const* bytes_end);
54 void process_bytes(void const* buffer, std::size_t byte_count);
56 void get_digest(digest_type& digest);
57 unsigned char get_version() const;
61 void process_byte_impl(unsigned char byte);
66 unsigned char block_[64];
68 std::size_t block_byte_index_;
69 std::size_t bit_count_low;
70 std::size_t bit_count_high;
78 inline void sha1::reset()
86 block_byte_index_ = 0;
91 inline void sha1::process_byte(unsigned char byte)
93 process_byte_impl(byte);
95 // size_t max value = 0xFFFFFFFF
96 //if (bit_count_low + 8 >= 0x100000000) { // would overflow
97 //if (bit_count_low >= 0x100000000-8) {
98 if (bit_count_low < 0xFFFFFFF8) {
103 if (bit_count_high <= 0xFFFFFFFE) {
106 BOOST_THROW_EXCEPTION(std::runtime_error("sha1 too many bytes"));
111 inline void sha1::process_byte_impl(unsigned char byte)
113 block_[block_byte_index_++] = byte;
115 if (block_byte_index_ == 64) {
116 block_byte_index_ = 0;
121 inline void sha1::process_block(void const* bytes_begin, void const* bytes_end)
123 unsigned char const* begin = static_cast<unsigned char const*>(bytes_begin);
124 unsigned char const* end = static_cast<unsigned char const*>(bytes_end);
125 for(; begin != end; ++begin) {
126 process_byte(*begin);
130 inline void sha1::process_bytes(void const* buffer, std::size_t byte_count)
132 unsigned char const* b = static_cast<unsigned char const*>(buffer);
133 process_block(b, b+byte_count);
136 inline void sha1::process_block()
139 for (std::size_t i=0; i<16; ++i) {
140 w[i] = (block_[i*4 + 0] << 24);
141 w[i] |= (block_[i*4 + 1] << 16);
142 w[i] |= (block_[i*4 + 2] << 8);
143 w[i] |= (block_[i*4 + 3]);
145 for (std::size_t i=16; i<80; ++i) {
146 w[i] = left_rotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1);
149 unsigned int a = h_[0];
150 unsigned int b = h_[1];
151 unsigned int c = h_[2];
152 unsigned int d = h_[3];
153 unsigned int e = h_[4];
155 for (std::size_t i=0; i<80; ++i) {
160 f = (b & c) | (~b & d);
166 f = (b & c) | (b & d) | (c & d);
173 unsigned temp = left_rotate(a, 5) + f + e + k + w[i];
176 c = left_rotate(b, 30);
188 inline unsigned char sha1::get_version() const
190 // RFC 4122 Section 4.1.3
191 return uuid::version_name_based_sha1;
194 inline void sha1::get_digest(digest_type& digest)
196 // append the bit '1' to the message
197 process_byte_impl(0x80);
199 // append k bits '0', where k is the minimum number >= 0
200 // such that the resulting message length is congruent to 56 (mod 64)
201 // check if there is enough space for padding and bit_count
202 if (block_byte_index_ > 56) {
204 while (block_byte_index_ != 0) {
205 process_byte_impl(0);
209 while (block_byte_index_ < 56) {
210 process_byte_impl(0);
213 while (block_byte_index_ < 56) {
214 process_byte_impl(0);
218 // append length of message (before pre-processing)
219 // as a 64-bit big-endian integer
220 process_byte_impl( static_cast<unsigned char>((bit_count_high>>24) & 0xFF) );
221 process_byte_impl( static_cast<unsigned char>((bit_count_high>>16) & 0xFF) );
222 process_byte_impl( static_cast<unsigned char>((bit_count_high>>8 ) & 0xFF) );
223 process_byte_impl( static_cast<unsigned char>((bit_count_high) & 0xFF) );
224 process_byte_impl( static_cast<unsigned char>((bit_count_low>>24) & 0xFF) );
225 process_byte_impl( static_cast<unsigned char>((bit_count_low>>16) & 0xFF) );
226 process_byte_impl( static_cast<unsigned char>((bit_count_low>>8 ) & 0xFF) );
227 process_byte_impl( static_cast<unsigned char>((bit_count_low) & 0xFF) );
237 }}} // namespace boost::uuids::detail