File: HexTest.cpp

package info (click to toggle)
barada-pam 0.5-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 516 kB
  • ctags: 101
  • sloc: sh: 3,774; cpp: 533; makefile: 69
file content (47 lines) | stat: -rw-r--r-- 1,049 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "Util.h"
#include <stdio.h>
#include <string>
#include <sstream>
#include <fstream>
#include <string.h>
#include <iostream>

#include <openssl/rand.h>
#include <boost/assert.hpp>

int main(int argc, char **argv) {
  unsigned char buf[16];
  unsigned char res[16];
  
  if (!(RAND_bytes(buf, sizeof(buf)))) {
    fprintf(stderr, "Error: Not enough entropy available to generate key.  Try again later.\n");
    return 0;
  }

  printf("Generated Random Data:\n");
  for (unsigned int i=0;i<sizeof(buf);i++) {
    printf("%X", buf[i]);
  }
  printf("\n");

  std::string hexString = Util::charToHexString(buf, sizeof(buf));
  std::cout << "Hex String:\n" << hexString << "\n";

  Util::hexStringToChar(res, sizeof(res), hexString);

  assert(hexString.length() == (sizeof(buf) * 2));
  
  printf("Got Back Data:\n");
  for (unsigned int i=0;i<sizeof(res);i++) {
    printf("%X", res[i]);
  }
  printf("\n");

  printf("Comparing...\n");

  for (unsigned int i=0;i<sizeof(buf);i++) {
    assert(buf[i] == res[i]);
  }
  
  printf("Done.\n");
}