| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | // |
| 5 | |
| 6 | #ifndef SHA1_H_ |
| 7 | #define SHA1_H_ |
| 8 | |
| 9 | // Hasher class, performs no allocation and therefore does not throw or return |
| 10 | // errors. Usage is as follows: |
| 11 | // Create an instance (this initializes the hash). |
| 12 | // Add one or more blocks of input data using AddData(). |
| 13 | // Retrieve the hash using GetHash(). This can be done as many times as desired |
| 14 | // until the object is destructed. Once a hash is asked for, further AddData |
| 15 | // calls will be ignored. There is no way to reset object state (simply |
| 16 | // destroy the object and create another instead). |
| 17 | |
| 18 | #define SHA1_HASH_SIZE 20 // Number of bytes output by SHA-1 |
| 19 | |
| 20 | typedef struct { |
| 21 | DWORD magic_sha1; // Magic value for A_SHA_CTX |
| 22 | DWORD awaiting_data[16]; |
| 23 | // Data awaiting full 512-bit block. |
| 24 | // Length (nbit_total[0] % 512) bits. |
| 25 | // Unused part of buffer (at end) is zero |
| 26 | DWORD partial_hash[5]; |
| 27 | // Hash through last full block |
| 28 | DWORD nbit_total[2]; |
| 29 | // Total length of message so far |
| 30 | // (bits, mod 2^64) |
| 31 | } SHA1_CTX; |
| 32 | |
| 33 | class SHA1Hash |
| 34 | { |
| 35 | private: |
| 36 | SHA1_CTX m_Context; |
| 37 | BYTE m_Value[SHA1_HASH_SIZE]; |
| 38 | BOOL m_fFinalized; |
| 39 | |
| 40 | void SHA1Init(SHA1_CTX*); |
| 41 | void SHA1Update(SHA1_CTX*, const BYTE*, const DWORD); |
| 42 | void SHA1Final(SHA1_CTX*, BYTE* digest); |
| 43 | |
| 44 | public: |
| 45 | SHA1Hash(); |
| 46 | void AddData(BYTE *pbData, DWORD cbData); |
| 47 | BYTE *GetHash(); |
| 48 | }; |
| 49 | |
| 50 | #endif // SHA1_H_ |
| 51 | |