1*2abb3134SXin Li // Copyright 2014 Google Inc. All rights reserved.
2*2abb3134SXin Li //
3*2abb3134SXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*2abb3134SXin Li // you may not use this file except in compliance with the License.
5*2abb3134SXin Li // You may obtain a copy of the License at
6*2abb3134SXin Li //
7*2abb3134SXin Li // http://www.apache.org/licenses/LICENSE-2.0
8*2abb3134SXin Li //
9*2abb3134SXin Li // Unless required by applicable law or agreed to in writing, software
10*2abb3134SXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*2abb3134SXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*2abb3134SXin Li // See the License for the specific language governing permissions and
13*2abb3134SXin Li // limitations under the License.
14*2abb3134SXin Li
15*2abb3134SXin Li // Sample code for encoder.cc.
16*2abb3134SXin Li //
17*2abb3134SXin Li // This is the code in README.md. It's here to make sure it actually builds
18*2abb3134SXin Li // and runs.
19*2abb3134SXin Li
20*2abb3134SXin Li #include <cassert> // assert
21*2abb3134SXin Li
22*2abb3134SXin Li #include "encoder.h"
23*2abb3134SXin Li #include "openssl_hash_impl.h"
24*2abb3134SXin Li #include "unix_kernel_rand_impl.h"
25*2abb3134SXin Li
main(int argc,char ** argv)26*2abb3134SXin Li int main(int argc, char** argv) {
27*2abb3134SXin Li // Suppress unused variable warnings
28*2abb3134SXin Li (void) argc;
29*2abb3134SXin Li (void) argv;
30*2abb3134SXin Li
31*2abb3134SXin Li FILE* fp = fopen("/dev/urandom", "r");
32*2abb3134SXin Li rappor::UnixKernelRand irr_rand(fp);
33*2abb3134SXin Li
34*2abb3134SXin Li rappor::Deps deps(rappor::Md5, "client-secret", rappor::HmacSha256,
35*2abb3134SXin Li irr_rand);
36*2abb3134SXin Li rappor::Params params(32, // num_bits (k)
37*2abb3134SXin Li 2, // num_hashes (h)
38*2abb3134SXin Li 128, // num_cohorts (m)
39*2abb3134SXin Li 0.25, // probability f for PRR
40*2abb3134SXin Li 0.75, // probability p for IRR
41*2abb3134SXin Li 0.5); // probability q for IRR
42*2abb3134SXin Li
43*2abb3134SXin Li const char* encoder_id = "metric-name";
44*2abb3134SXin Li rappor::Encoder encoder(encoder_id, params, deps);
45*2abb3134SXin Li
46*2abb3134SXin Li // Now use it to encode values. The 'out' value can be sent over the
47*2abb3134SXin Li // network.
48*2abb3134SXin Li rappor::Bits out;
49*2abb3134SXin Li assert(encoder.EncodeString("foo", &out)); // returns false on error
50*2abb3134SXin Li printf("'foo' encoded with RAPPOR: %0x, cohort %d\n", out, encoder.cohort());
51*2abb3134SXin Li
52*2abb3134SXin Li // Raw bits
53*2abb3134SXin Li assert(encoder.EncodeBits(0x123, &out)); // returns false on error
54*2abb3134SXin Li printf("0x123 encoded with RAPPOR: %0x, cohort %d\n", out, encoder.cohort());
55*2abb3134SXin Li }
56*2abb3134SXin Li
57