Skip to content

Commit

Permalink
better sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Oct 9, 2023
1 parent 040385f commit cedc9fe
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions core/samples/pubnub_crypto_module_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ static void print_encrypted_message(const char* display, pubnub_bymebl_t encrypt
int main()
{
char const *msg = "Hello world";
char const *cipher_key = "enigma";
uint8_t const *cipher_key = (uint8_t*)"enigma";

printf("message to be encrypted: %s\n\n", msg);

pubnub_bymebl_t block;
block.ptr = (uint8_t*)msg;
block.size = strlen(msg);

struct pubnub_crypto_provider_t *legacy_crypto_module = pubnub_crypto_legacy_module_init((uint8_t*) cipher_key);
struct pubnub_crypto_provider_t *legacy_crypto_module = pubnub_crypto_legacy_module_init(cipher_key);
pubnub_bymebl_t legacy_encrypt_result = legacy_crypto_module->encrypt(legacy_crypto_module, block);

if (NULL == legacy_encrypt_result.ptr) {
Expand All @@ -91,7 +91,7 @@ int main()

print_encrypted_message("encrypt with legacy AES-CBC result", legacy_encrypt_result);

struct pubnub_crypto_provider_t *crypto_module = pubnub_crypto_aes_cbc_module_init((uint8_t*)cipher_key);
struct pubnub_crypto_provider_t *crypto_module = pubnub_crypto_aes_cbc_module_init(cipher_key);
pubnub_bymebl_t encrypt_result = crypto_module->encrypt(crypto_module, block);

if (NULL == encrypt_result.ptr) {
Expand Down Expand Up @@ -133,20 +133,19 @@ int main()
}
pubnub_init(pbp, "demo", "demo");

pubnub_set_transaction_timeout(pbp, PUBNUB_DEFAULT_NON_SUBSCRIBE_TIMEOUT);

/* Leave this commented out to use the default - which is
blocking I/O on most platforms. Uncomment to use non-
blocking I/O.
*/
pubnub_set_non_blocking_io(pbp);

generate_user_id(pbp);

pubnub_set_crypto_module(pbp, pubnub_crypto_aes_cbc_module_init((uint8_t*)cipher_key));
pubnub_set_crypto_module(pbp, pubnub_crypto_aes_cbc_module_init(cipher_key));

enum pubnub_res result;

printf("Subscribing to PubNub infrastructure...\n");

result = pubnub_subscribe(pbp, "hello_world", NULL);
if (PNR_STARTED == result) {
result = pubnub_await(pbp);
Expand All @@ -160,6 +159,8 @@ int main()
return -1;
}

printf("Publishing a message...\n");

result = pubnub_publish(pbp, "hello_world", "\"Hello world from Pubnub C core API!\"");
if (PNR_STARTED == result) {
result = pubnub_await(pbp);
Expand All @@ -173,6 +174,8 @@ int main()
return -1;
}

printf("Fetching messages...\n");

result = pubnub_subscribe(pbp, "hello_world", NULL);
if (PNR_STARTED == result) {
result = pubnub_await(pbp);
Expand All @@ -186,8 +189,9 @@ int main()
return -1;
}

printf("Here are the messages:\n");
for (const char* msg = pubnub_get(pbp); msg != NULL; msg = pubnub_get(pbp)) {
printf("Received message: %s\n", msg);
printf("> %s\n", msg);
}

puts("Pubnub crypto module demo over.");
Expand Down

0 comments on commit cedc9fe

Please sign in to comment.