Encrypting data with AES using Nettle in C:
```c
#include
#include
void aes_encrypt_example() {
struct aes_ctx ctx;
uint8_t key[16] = "mysecretkey12345"; // 128-bit key
uint8_t plaintext[16] = "exampleplaintext";
uint8_t ciphertext[16];
// Initialize AES context with the key
aes_set_encrypt_key(&ctx, sizeof(key), key);
// Encrypt the plaintext
aes_encrypt(&ctx, sizeof(plaintext), ciphertext, plaintext);
// Print the ciphertext
for (int i = 0; i < sizeof(ciphertext); i++) {
printf("%02x", ciphertext[i]);
}
printf("\n");
}
int main() {
aes_encrypt_example();
return 0;
}
```
Hashing data with SHA-256 using Nettle in Python (using a ctypes wrapper):
```python
import ctypes
import ctypes.util
# Load the Nettle library
nettle = ctypes.CDLL(ctypes.util.find_library('nettle'))
# Define the SHA-256 context structure
class Sha256Ctx(ctypes.Structure):
_fields_ = [("state", ctypes.c_uint32 * 8),
("count", ctypes.c_uint64),
("block", ctypes.c_uint8 * 64),
("index", ctypes.c_size_t)]
# Initialize the SHA-256 context
ctx = Sha256Ctx()
nettle.nettle_sha256_init(ctypes.byref(ctx))
# Update the context with the input data
data = b"hello world"
nettle.nettle_sha256_update(ctypes.byref(ctx), ctypes.c_size_t(len(data)), data)
# Finalize the hash and retrieve the output
digest = (ctypes.c_uint8 * 32)()
nettle.nettle_sha256_digest(ctypes.byref(ctx), ctypes.c_size_t(len(digest)), digest)
# Print the SHA-256 hash
print("SHA-256:", "".join("{:02x}".format(b) for b in digest))
```