Couchbase C Client
2.10.7
Asynchronous C Client for Couchbase
|
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"
{
fprintf(stderr,
"%s. Received code 0x%X (%s)\n", msg, err,
lcb_strerror(instance, err));
exit(EXIT_FAILURE);
}
{
fprintf(stderr,
"CAS: 0x%" PRIx64
"\n", rb->
cas);
} else {
}
}
static void store_encrypted(
lcb_t instance,
const char *key,
const char *val)
{
printf("KEY: %s\n", key);
printf("PLAIN: %s\n", val);
field.
alg =
"AES-256-HMAC-SHA256";
die(instance, "Couldn't encrypt field 'message'", err);
}
if (ecmd.
out[ecmd.
nout - 1] ==
'\n') {
}
printf(
"CIPHER: %s\n", ecmd.
out);
die(instance, "Couldn't schedule storage operation", err);
}
}
int main(int argc, char *argv[])
{
{
if (argc < 2) {
fprintf(stderr, "Usage: %s couchbase://host/bucket [ password [ username ] ]\n", argv[0]);
exit(EXIT_FAILURE);
}
if (argc > 2) {
}
if (argc > 3) {
}
die(NULL, "Couldn't create couchbase handle", err);
}
die(instance, "Couldn't schedule connection", err);
}
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;
}
size_t nout
size of the output JSON document
Definition: crypto.h:142
lcb_error_t lcb_wait(lcb_t instance)
Wait for the execution of all batched requests.
const char * lcb_strerror(lcb_t instance, lcb_error_t error)
Get a textual descrtiption for the given error code.
const char * passwd
Password for bucket.
Definition: couchbase.h:293
lcb_error_t lcb_get_bootstrap_status(lcb_t instance)
Gets the initial bootstrap status.
const char * doc
pointer to the input JSON document
Definition: crypto.h:139
const char * connstr
Connection string.
Definition: couchbase.h:282
lcb_error_t lcb_connect(lcb_t instance)
Schedule the initial connection This function will schedule the initial connection for the handle.
const char * username
Username to use for authentication.
Definition: couchbase.h:288
@ LCB_SET
Unconditionally store the item in the cluster.
Definition: couchbase.h:1055
#define LCB_DATATYPE_JSON
Definition: couchbase.h:3355
lcb_error_t
Error codes returned by the library.
Definition: error.h:476
lcb_error_t lcb_store3(lcb_t instance, const void *cookie, const lcb_CMDSTORE *cmd)
Schedule a single storage request.
@ LCB_CALLBACK_STORE
lcb_store3()
Definition: couchbase.h:698
Command to encrypt JSON fields.
Definition: crypto.h:136
lcb_CAS cas
CAS for response (if applicable)
Definition: couchbase.h:626
lcb_error_t rc
Status code.
Definition: couchbase.h:626
struct lcb_create_st3 v3
Use this field.
Definition: couchbase.h:338
void lcbcrypto_register(lcb_t instance, const char *name, lcbcrypto_PROVIDER *provider)
Register crypto-provider for specified alias.
const char * lcb_strcbtype(int cbtype)
Returns the type of the callback as a string.
lcbcrypto_FIELDSPEC * fields
list of field specs
Definition: crypto.h:143
Wrapper structure for lcb_create()
Definition: couchbase.h:328
size_t nfields
number of field specs
Definition: crypto.h:144
uint16_t version
version of the structure, currently valid value is 0
Definition: crypto.h:137
#define LCB_CMD_SET_KEY(cmd, keybuf, keylen)
Set the key for the command.
Definition: couchbase.h:556
struct lcb_st * lcb_t
Definition: couchbase.h:41
lcb_error_t lcb_create(lcb_t *instance, const struct lcb_create_st *options)
Create an instance of lcb.
lcb_error_t lcbcrypto_encrypt_fields(lcb_t instance, lcbcrypto_CMDENCRYPT *cmd)
Encrypt all specified fields in the JSON encoded object.
@ LCB_SUCCESS
Success.
Definition: error.h:478
Base response structure for callbacks.
Definition: couchbase.h:625
char * out
pointer to output JSON document.
Definition: crypto.h:141
void lcb_destroy(lcb_t instance)
Destroy (and release all allocated resources) an instance of lcb.
lcb_storage_t operation
Controls how the operation is perfomed.
Definition: couchbase.h:1113
const char * alg
crypto provider alias (NUL-terminated)
Definition: crypto.h:125
const char * prefix
prefix to encrypted field.
Definition: crypto.h:138
#define LCB_CMD_SET_VALUE(scmd, valbuf, vallen)
Set the value buffer for the command. This may be used when the new value is a single contiguous buff...
Definition: couchbase.h:1139
int version
Indicates which field in the lcb_CRST_u union should be used.
Definition: couchbase.h:330
lcb_RESPCALLBACK lcb_install_callback3(lcb_t instance, int cbtype, lcb_RESPCALLBACK cb)
Command for storing an item to the server.
Definition: couchbase.h:1090
size_t ndoc
size of the input JSON document
Definition: crypto.h:140
lcb_datatype_t datatype
Do not set this value for now.
Definition: couchbase.h:1107
Structure for JSON field specification for encrypt/decrypt API.
Definition: crypto.h:123
const char * name
field name (NUL-terminated)
Definition: crypto.h:124