6
// Paul E. Jones <paulej@arid.us>
7
// All Rights Reserved.
9
// This software is licensed as "freeware." Permission to distribute
10
// this software in source and binary forms is hereby granted without
11
// a fee. THIS SOFTWARE IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED
12
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14
// THE AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DAMAGES RESULTING
15
// FROM THE USE OF THIS SOFTWARE, EITHER DIRECTLY OR INDIRECTLY, INCLUDING,
16
// BUT NOT LIMITED TO, LOSS OF DATA OR DATA BEING RENDERED INACCURATE.
18
//////////////////////////////////////////////////////////////////////////////
19
// ###Id: sha1.h,v 1.6 2004/03/27 18:02:26 paulej Exp ###
20
// Modified March 7, 2007 - Andy Tompkins - change to a header only library
21
//////////////////////////////////////////////////////////////////////////////
24
// This class implements the Secure Hashing Standard as defined
25
// in FIPS PUB 180-1 published April 17, 1995.
27
// Many of the variable names in this class, especially the single
28
// character names, were used because those were the names used
29
// in the publication.
31
// The Secure Hashing Standard, which uses the Secure Hashing
32
// Algorithm (SHA), produces a 160-bit message digest for a
33
// given data stream. In theory, it is highly improbable that
34
// two messages will produce the same message digest. Therefore,
35
// this algorithm can serve as a means of providing a "fingerprint"
38
// Portability Issues:
39
// SHA-1 is defined in terms of 32-bit "words". This code was
40
// written with the expectation that the processor has at least
41
// a 32-bit machine word size. If the machine word size is larger,
42
// the code should still function properly. One caveat to that
43
// is that the input functions taking characters and character arrays
44
// assume that only 8 bits of information are stored in each character.
47
// SHA-1 is designed to work with messages less than 2^64 bits long.
48
// Although SHA-1 allows a message digest to be generated for
49
// messages of any number of bits less than 2^64, this implementation
50
// only works with messages with a length that is a multiple of 8
53
#ifndef LIBLAS_SHA1_HPP_INCLUDED
54
#define LIBLAS_SHA1_HPP_INCLUDED
56
namespace liblas { namespace detail {
65
* This is the constructor for the sha1 class.
85
* This is the destructor for the sha1 class
98
// The destructor does nothing
105
* This function will initialize the sha1 class member variables
106
* in preparation for computing a new message digest.
121
Message_Block_Index = 0;
137
* This function will return the 160-bit message digest into the
141
* message_digest_array: [out]
142
* This is an array of five unsigned integers which will be filled
143
* with the message digest that has been computed.
146
* True if successful, false if it failed.
151
bool Result(unsigned *message_digest_array)
166
for(i = 0; i < 5; i++)
168
message_digest_array[i] = H[i];
178
* This function accepts an array of octets as the next portion of
182
* message_array: [in]
183
* An array of characters representing the next portion of the
192
void Input( const unsigned char *message_array,
200
if (Computed || Corrupted)
206
while(length-- && !Corrupted)
208
Message_Block[Message_Block_Index++] = (*message_array & 0xFF);
211
Length_Low &= 0xFFFFFFFF; // Force it to 32 bits
215
Length_High &= 0xFFFFFFFF; // Force it to 32 bits
216
if (Length_High == 0)
218
Corrupted = true; // Message is too long
222
if (Message_Block_Index == 64)
224
ProcessMessageBlock();
235
* This function accepts an array of octets as the next portion of
239
* message_array: [in]
240
* An array of characters representing the next portion of the
243
* The length of the message_array
251
void Input( const char *message_array,
254
Input((unsigned char *) message_array, length);
261
* This function accepts a single octets as the next message element.
264
* message_element: [in]
265
* The next octet in the message.
273
void Input(unsigned char message_element)
275
Input(&message_element, 1);
282
* This function accepts a single octet as the next message element.
285
* message_element: [in]
286
* The next octet in the message.
294
void Input(char message_element)
296
Input((unsigned char *) &message_element, 1);
303
* This operator makes it convenient to provide character strings to
304
* the SHA1 object for processing.
307
* message_array: [in]
308
* The character array to take as input.
311
* A reference to the SHA1 object.
314
* Each character is assumed to hold 8 bits of information.
317
SHA1& operator<<(const char *message_array)
319
const char *p = message_array;
334
* This operator makes it convenient to provide character strings to
335
* the SHA1 object for processing.
338
* message_array: [in]
339
* The character array to take as input.
342
* A reference to the SHA1 object.
345
* Each character is assumed to hold 8 bits of information.
348
SHA1& operator<<(const unsigned char *message_array)
350
const unsigned char *p = message_array;
365
* This function provides the next octet in the message.
368
* message_element: [in]
369
* The next octet in the message
372
* A reference to the SHA1 object.
375
* The character is assumed to hold 8 bits of information.
378
SHA1& operator<<(const char message_element)
380
Input((unsigned char *) &message_element, 1);
389
* This function provides the next octet in the message.
392
* message_element: [in]
393
* The next octet in the message
396
* A reference to the SHA1 object.
399
* The character is assumed to hold 8 bits of information.
402
SHA1& operator<<(const unsigned char message_element)
404
Input(&message_element, 1);
411
* ProcessMessageBlock
414
* This function will process the next 512 bits of the message
415
* stored in the Message_Block array.
424
* Many of the variable names in this function, especially the single
425
* character names, were used because those were the names used
426
* in the publication.
429
void ProcessMessageBlock()
431
const unsigned K[] = { // Constants defined for SHA-1
437
int t; // Loop counter
438
unsigned temp; // Temporary word value
439
unsigned W[80]; // Word sequence
440
unsigned A, B, C, D, E; // Word buffers
443
* Initialize the first 16 words in the array W
445
for(t = 0; t < 16; t++)
447
W[t] = ((unsigned) Message_Block[t * 4]) << 24;
448
W[t] |= ((unsigned) Message_Block[t * 4 + 1]) << 16;
449
W[t] |= ((unsigned) Message_Block[t * 4 + 2]) << 8;
450
W[t] |= ((unsigned) Message_Block[t * 4 + 3]);
453
for(t = 16; t < 80; t++)
455
W[t] = CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
464
for(t = 0; t < 20; t++)
466
temp = CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
470
C = CircularShift(30,B);
475
for(t = 20; t < 40; t++)
477
temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
481
C = CircularShift(30,B);
486
for(t = 40; t < 60; t++)
488
temp = CircularShift(5,A) +
489
((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
493
C = CircularShift(30,B);
498
for(t = 60; t < 80; t++)
500
temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
504
C = CircularShift(30,B);
509
H[0] = (H[0] + A) & 0xFFFFFFFF;
510
H[1] = (H[1] + B) & 0xFFFFFFFF;
511
H[2] = (H[2] + C) & 0xFFFFFFFF;
512
H[3] = (H[3] + D) & 0xFFFFFFFF;
513
H[4] = (H[4] + E) & 0xFFFFFFFF;
515
Message_Block_Index = 0;
522
* According to the standard, the message must be padded to an even
523
* 512 bits. The first padding bit must be a '1'. The last 64 bits
524
* represent the length of the original message. All bits in between
525
* should be 0. This function will pad the message according to those
526
* rules by filling the message_block array accordingly. It will also
527
* call ProcessMessageBlock() appropriately. When it returns, it
528
* can be assumed that the message digest has been computed.
542
* Check to see if the current message block is too small to hold
543
* the initial padding bits and length. If so, we will pad the
544
* block, process it, and then continue padding into a second block.
546
if (Message_Block_Index > 55)
548
Message_Block[Message_Block_Index++] = 0x80;
549
while(Message_Block_Index < 64)
551
Message_Block[Message_Block_Index++] = 0;
554
ProcessMessageBlock();
556
while(Message_Block_Index < 56)
558
Message_Block[Message_Block_Index++] = 0;
563
Message_Block[Message_Block_Index++] = 0x80;
564
while(Message_Block_Index < 56)
566
Message_Block[Message_Block_Index++] = 0;
572
* Store the message length as the last 8 octets
574
Message_Block[56] = static_cast<unsigned char>((Length_High >> 24) & 0xFF);
575
Message_Block[57] = static_cast<unsigned char>((Length_High >> 16) & 0xFF);
576
Message_Block[58] = static_cast<unsigned char>((Length_High >> 8) & 0xFF);
577
Message_Block[59] = static_cast<unsigned char>((Length_High) & 0xFF);
578
Message_Block[60] = static_cast<unsigned char>((Length_Low >> 24) & 0xFF);
579
Message_Block[61] = static_cast<unsigned char>((Length_Low >> 16) & 0xFF);
580
Message_Block[62] = static_cast<unsigned char>((Length_Low >> 8) & 0xFF);
581
Message_Block[63] = static_cast<unsigned char>((Length_Low) & 0xFF);
583
ProcessMessageBlock();
591
* This member function will perform a circular shifting operation.
595
* The number of bits to shift (1-31)
597
* The value to shift (assumes a 32-bit integer)
605
unsigned CircularShift(int bits, unsigned word)
607
return ((word << bits) & 0xFFFFFFFF) | ((word & 0xFFFFFFFF) >> (32-bits));
611
unsigned H[5]; // Message digest buffers
613
unsigned Length_Low; // Message length in bits
614
unsigned Length_High; // Message length in bits
616
unsigned char Message_Block[64]; // 512-bit message blocks
617
int Message_Block_Index; // Index into message block array
619
bool Computed; // Is the digest computed?
620
bool Corrupted; // Is the message digest corruped?
623
}} //namespace liblas::detail
625
#endif // LIBLAS_SHA1_HPP_INCLUDED