https://github.com/blagojeblagojevic/sha256
SHA256 as a header library
https://github.com/blagojeblagojevic/sha256
c cryptography header-only sha2 sha256 sha256-hash
Last synced: 12 months ago
JSON representation
SHA256 as a header library
- Host: GitHub
- URL: https://github.com/blagojeblagojevic/sha256
- Owner: BlagojeBlagojevic
- License: mit
- Created: 2024-08-13T09:18:57.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-13T13:57:32.000Z (almost 2 years ago)
- Last Synced: 2025-04-08T09:48:24.679Z (about 1 year ago)
- Topics: c, cryptography, header-only, sha2, sha256, sha256-hash
- Language: C
- Homepage:
- Size: 13.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
---
# SHA-256 Hash Implementation
This project provides a C implementation of the SHA-256 hashing algorithm. SHA-256 is part of the SHA-2 family of cryptographic hash functions and is widely used for secure data hashing and digital signatures.
## Features
- **SHA-256 Algorithm**: Implements the SHA-256 hash function as defined by the [SHA-2 standard](https://en.wikipedia.org/wiki/SHA-2).
- **Inline Functions**: Provides efficient hash computation using inline functions.
- **Error Handling**: Includes basic error handling for memory allocation issues.
## File Overview
### `hash.h`
- **Header File**: Contains definitions, macro functions, and function prototypes for the SHA-256 hash implementation.
- **Macros**: Defines helper macros for bitwise operations, rotations, and bit manipulation.
- **Types**: Defines the `HASH_CTX` structure for maintaining the state of the hash computation.
- **SHA-256 Functions**:
- `HASH_Init()`: Initializes the hash context.
- `HASH_Transform()`: Processes a 512-bit block of data.
- `HASH_Update()`: Updates the hash context with new data.
- `HASH_Final()`: Finalizes the hash computation and produces the final hash value.
- `HASH()`: Convenience function to hash a string and return the hash as a hexadecimal string.
## Usage
To use the SHA-256 hashing functions in your C project:
1. **Include the Header File**:
```c
#define HASH_IMPLEMENTATION
#include "hash.h"
```
2. **Hash a String**:
```c
#include
#include
#include
#include "hash.h"
int main() {
char *input = "Hello, world!";
char *hashStr = HASH(input);
printf("Hash: %s\n", hashStr);
free(hashStr);
return EXIT_SUCCESS;
}
```
## Compilation
To compile the code:
```bash
gcc -o hash_program hash.c -O2
```
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## Acknowledgments
- [Wikipedia - SHA-2](https://en.wikipedia.org/wiki/SHA-2): For the detailed explanation of SHA-256.
---