Shows how to use field-encryption API to encrypt JSON values.
Shows how to use field-encryption API to encrypt JSON values.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#define PRIx64 "I64x"
#else
#include <inttypes.h>
#endif
#include "openssl_symmetric_provider.h"
{
exit(EXIT_FAILURE);
}
static void op_callback(
lcb_INSTANCE *instance,
int cbtype,
const lcb_RESPBASE *rb)
{
if (rb->rc == LCB_SUCCESS) {
fprintf(stderr, "CAS: 0x%" PRIx64 "\n", rb->cas);
} else {
}
}
static void store_encrypted(
lcb_INSTANCE *instance,
const char *key,
const char *val)
{
lcb_CMDSTORE cmd = {};
printf("KEY: %s\n", key);
printf("PLAIN: %s\n", val);
field.
alg =
"AES-256-HMAC-SHA256";
if (err != LCB_SUCCESS) {
die(instance, "Couldn't encrypt field 'message'", err);
}
if (ecmd.
out[ecmd.
nout - 1] ==
'\n') {
}
printf(
"CIPHER: %s\n", ecmd.
out);
LCB_CMD_SET_VALUE(&cmd, ecmd.
out, ecmd.
nout);
err = lcb_store3(instance, NULL, &cmd);
if (err != LCB_SUCCESS) {
die(instance, "Couldn't schedule storage operation", err);
}
}
int main(int argc, char *argv[])
{
{
struct lcb_create_st create_options = {};
create_options.version = 3;
if (argc < 2) {
fprintf(stderr, "Usage: %s couchbase://host/bucket [ password [ username ] ]\n", argv[0]);
exit(EXIT_FAILURE);
}
create_options.v.v3.connstr = argv[1];
if (argc > 2) {
create_options.v.v3.passwd = argv[2];
}
if (argc > 3) {
create_options.v.v3.username = argv[3];
}
if (err != LCB_SUCCESS) {
die(NULL, "Couldn't create couchbase handle", err);
}
if (err != LCB_SUCCESS) {
die(instance, "Couldn't schedule connection", err);
}
if (err != LCB_SUCCESS) {
die(instance, "Couldn't bootstrap from cluster", err);
}
}
store_encrypted(instance, "secret-1", "{\"message\":\"The old grey goose jumped over the wrickety gate.\"}");
printf("\n");
store_encrypted(instance, "secret-2", "{\"message\":10}");
printf("\n");
store_encrypted(instance, "secret-3", "{\"message\":\"10\"}");
printf("\n");
store_encrypted(
instance, "secret-4",
"{\"message\":[\"The\",\"Old\",\"Grey\",\"Goose\",\"Jumped\",\"over\",\"the\",\"wrickety\",\"gate\"]}");
printf("\n");
store_encrypted(instance, "secret-5",
"{\"message\":{\"myValue\":\"The old grey goose jumped over the wrickety gate.\",\"myInt\":10}}");
return 0;
}
Main header file for Couchbase.
#define LCB_DATATYPE_JSON
Definition couchbase.h:2116
void lcb_destroy(lcb_INSTANCE *instance)
Destroy (and release all allocated resources) an instance of lcb.
lcbcrypto_FIELDSPEC * fields
list of field specs
Definition crypto.h:125
char * out
pointer to output JSON document.
Definition crypto.h:123
size_t ndoc
size of the input JSON document
Definition crypto.h:122
const char * doc
pointer to the input JSON document
Definition crypto.h:121
const char * prefix
prefix to encrypted field.
Definition crypto.h:120
const char * name
field name (NUL-terminated)
Definition crypto.h:106
uint16_t version
version of the structure, currently valid value is 0
Definition crypto.h:119
size_t nout
size of the output JSON document
Definition crypto.h:124
const char * alg
crypto provider alias (NUL-terminated)
Definition crypto.h:107
size_t nfields
number of field specs
Definition crypto.h:126
lcb_STATUS lcbcrypto_encrypt_fields(lcb_INSTANCE *instance, lcbcrypto_CMDENCRYPT *cmd)
Encrypt all specified fields in the JSON encoded object.
void lcbcrypto_register(lcb_INSTANCE *instance, const char *name, lcbcrypto_PROVIDER *provider)
Register crypto-provider for specified alias.
Command to encrypt JSON fields.
Definition crypto.h:118
Structure for JSON field specification for encrypt/decrypt API.
Definition crypto.h:105
LCB_INTERNAL_API const char * lcb_strerror_short(lcb_STATUS error)
Get a shorter textual description of an error message.
lcb_STATUS
Error codes returned by the library.
Definition error.h:213
lcb_STATUS lcb_get_bootstrap_status(lcb_INSTANCE *instance)
Gets the initial bootstrap status.
lcb_STATUS lcb_create(lcb_INSTANCE **instance, const lcb_CREATEOPTS *options)
Create an instance of lcb.
struct lcb_st lcb_INSTANCE
Library handle representing a connection to a cluster and its data buckets.
Definition couchbase.h:35
lcb_STATUS lcb_connect(lcb_INSTANCE *instance)
Schedule the initial connection This function will schedule the initial connection for the handle.
const char * lcb_strcbtype(int cbtype)
Returns the type of the callback as a string.
@ LCB_CALLBACK_STORE
lcb_store()
Definition couchbase.h:472
@ LCB_STORE_UPSERT
The default storage mode.
Definition couchbase.h:894
lcb_STATUS lcb_wait(lcb_INSTANCE *instance, lcb_WAITFLAGS flags)
Wait for completion of scheduled operations.
@ LCB_WAIT_DEFAULT
Behave like the old lcb_wait()
Definition couchbase.h:1861
#define LCB_CMD_SET_KEY(cmd, keybuf, keylen)
Set the key for the command.
Definition utils.h:52