GenerateCampaignCode is a .NET 8.0 project designed to generate and validate campaign codes using different hashing algorithms (SHA1 and HMACSHA256). The project includes API endpoints to generate and validate codes, as well as extensive testing to ensure reliability and performance.
- Click this link -> https://generatecode.azurewebsites.net/swagger (maybe open slowly you should wait a little first open)
The CodeGenerator
class provides methods to generate and validate secure campaign codes using SHA1 and HMACSHA256 hashing algorithms. It includes caching functionality to optimize performance and reduce redundant computations. This service is designed for applications that require generating unique, secure codes for various campaigns or promotions.
-
Configuration and Dependencies:
CampaignCodeSettings
: Configuration settings for the campaign code, including characters set and code length.IMemoryCache
: Caching service to store and retrieve generated codes.ILogger<CodeGenerator>
: Logging service to record informational messages and errors.
-
Main Methods:
GenerateCodeSHA1(string id, string salt)
: Generates a campaign code using the SHA1 hashing algorithm, with optional caching to improve performance.ValidateCodeSHA1(string id, string salt, string code)
: Validates a given code by comparing it with a newly generated code using SHA1.GenerateCodeWithHMACSHA256(string id, string salt)
: Generates a campaign code using the HMACSHA256 hashing algorithm.ValidateCodeHMACSHA256(string id, string salt, string code)
: Validates a given code by comparing it with a newly generated code using HMACSHA256.
-
Caching:
- Caches generated SHA1 codes to reduce redundant computations and improve performance. Cached codes have a sliding expiration of one day.
-
Logging:
- Logs the generation and retrieval of codes, as well as any errors encountered during the process.
- Getting Started
- Usage
- Configuration
- Running the Tests
- Contributing
- Azure Web App Setup and GitHub Secrets
- License
- .NET 8.0 SDK
- Visual Studio or any other preferred IDE
-
Clone the repository:
git clone https://github.com/Andronovo-bit/GenerateCampaignCode.git cd GenerateCampaignCode
-
Build the solution:
dotnet build
-
Navigate to the
src/Api
directory:cd src/Api
-
Run the application:
dotnet run
The API will be available at https://localhost:5109/swagger
by default.
-
Generate Code (SHA1)
POST /api/code/generate/sha1 { "id": "string", "salt": "string" }
-
Validate Code (SHA1)
POST /api/code/validate/sha1 { "id": "string", "salt": "string", "code": "string" }
-
Generate Code (HMACSHA256)
POST /api/code/generate/hmacsha256 { "id": "string", "salt": "string" }
-
Validate Code (HMACSHA256)
POST /api/code/validate/hmacsha256 { "id": "string", "salt": "string", "code": "string" }
The application settings are stored in appsettings.json
. You can configure the campaign code settings in this file:
{
...
"CampaignCodeSettings": {
"Characters": "ACDEFGHKLMNPRTXYZ234579",
"Length": 8,
"PrivateKey": "YOUR_PRIVATE_KEY"
}
...
}
The solution includes extensive unit tests to ensure code reliability. To run the tests, use the following command:
-
Navigate to the
GenerateCampaignCode.Tests
directory:cd ./GenerateCampaignCode.Tests
-
Test the application:
dotnet test
The tests cover code generation, validation, and uniqueness checks.
- Sign in to the Azure Portal.
- From the Azure portal menu, select "Create a resource".
- In the "Search the Marketplace" field, type 'Web App' and press enter.
- Select "Web App" from the results, then click "Create".
- Fill in the details for your web app, such as Subscription, Resource Group, Name, Publish (Code), and Runtime Stack (.NET Core).
- Click "Review + create" and then "Create" after verifying your details.
- Navigate to your GitHub repository.
- Go to "Settings" > "Secrets" > "Actions".
- Click on "New repository secret".
- Add the following secrets required for Azure deployment:
AZURE_PUBLISH_PROFILE
: The publish profile XML content. (You can download this from your Azure Web App's "Deployment Center".)
- Use these secrets in your
publish.yml
GitHub Action workflow to deploy your application.
This project is licensed under the MIT License - see the LICENSE file for details.
- About The Project: Provides a brief overview of what the project is about and the technologies used.
- Getting Started: Includes instructions on prerequisites, installation, and running the API.
- API Endpoints: Details the available endpoints for generating and validating codes.
- Configuration: Describes how to configure the application settings.
- Running the Tests: Instructions on how to run the unit tests.
- License: Information about the project's license.
This README file serves as a comprehensive guide for users and contributors, helping them understand the project, set it up, and contribute effectively.