ChaCha32 C implementation – 32‑round stream encryption, compatible with Arduino & Go versions.
To build the example program, run the following command in your terminal:
gcc test.c chacha32.c -o test.exeBelow is a minimal example from test.c:
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "chacha32.h"
int main() {
// Fixed key (32 bytes) and nonce (12 bytes)
uint8_t key[32] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
};
uint8_t nonce[12] = {
0x00, 0x00, 0x00, 0x09,
0x00, 0x00, 0x00, 0x4A,
0x00, 0x00, 0x00, 0x00
};
uint8_t message[] = "Hello ChaCha32!"; // Plaintext message
uint8_t encrypted[sizeof(message)];
uint8_t decrypted[sizeof(message)];
// Encryption
chacha32_encrypt(key, nonce, message, encrypted, sizeof(message));
// Print the encrypted message (HEX format)
printf("Encrypted: ");
for (size_t i = 0; i < sizeof(message); i++) {
printf("%02X ", encrypted[i]);
}
printf("\n");
// Decryption
chacha32_decrypt(key, nonce, encrypted, decrypted, sizeof(message));
// Print the decrypted message as text
printf("Decrypted: %s\n", decrypted);
return 0;
}- ChaCha32Arduino – Arduino compatible version
- chacha32go – Go compatible