| 1 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // LibSha1 |
| 3 | // |
| 4 | // Implementation of SHA1 hash function. |
| 5 | // Original author: Steve Reid <[email protected]> |
| 6 | // Contributions by: James H. Brown <[email protected]>, Saul Kravitz <[email protected]>, |
| 7 | // and Ralph Giles <[email protected]> |
| 8 | // Modified by WaterJuice retaining Public Domain license. |
| 9 | // |
| 10 | // This is free and unencumbered software released into the public domain - June 2013 waterjuice.org |
| 11 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 12 | |
| 13 | #ifndef _sha1_h_ |
| 14 | #define _sha1_h_ |
| 15 | |
| 16 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 17 | // IMPORTS |
| 18 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 24 | // TYPES |
| 25 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 26 | |
| 27 | // Sha1Context - This must be initialised using Sha1Initialised. Do not modify the contents of this structure directly. |
| 28 | typedef struct |
| 29 | { |
| 30 | uint32_t State[5]; |
| 31 | uint32_t Count[2]; |
| 32 | uint8_t Buffer[64]; |
| 33 | } Sha1Context; |
| 34 | |
| 35 | #define SHA1_HASH_SIZE ( 160 / 8 ) |
| 36 | |
| 37 | typedef struct |
| 38 | { |
| 39 | uint8_t bytes [SHA1_HASH_SIZE]; |
| 40 | } SHA1_HASH; |
| 41 | |
| 42 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 43 | // PUBLIC FUNCTIONS |
| 44 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 45 | |
| 46 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 47 | // Sha1Initialise |
| 48 | // |
| 49 | // Initialises an SHA1 Context. Use this to initialise/reset a context. |
| 50 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 51 | void |
| 52 | Sha1Initialise |
| 53 | ( |
| 54 | Sha1Context* Context |
| 55 | ); |
| 56 | |
| 57 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 58 | // Sha1Update |
| 59 | // |
| 60 | // Adds data to the SHA1 context. This will process the data and update the internal state of the context. Keep on |
| 61 | // calling this function until all the data has been added. Then call Sha1Finalise to calculate the hash. |
| 62 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 63 | void |
| 64 | Sha1Update |
| 65 | ( |
| 66 | Sha1Context* Context, |
| 67 | void* Buffer, |
| 68 | uint32_t BufferSize |
| 69 | ); |
| 70 | |
| 71 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 72 | // Sha1Finalise |
| 73 | // |
| 74 | // Performs the final calculation of the hash and returns the digest (20 byte buffer containing 160bit hash). After |
| 75 | // calling this, Sha1Initialised must be used to reuse the context. |
| 76 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 77 | void |
| 78 | Sha1Finalise |
| 79 | ( |
| 80 | Sha1Context* Context, |
| 81 | SHA1_HASH* Digest |
| 82 | ); |
| 83 | |
| 84 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 85 | #endif //_sha1_h_ |