CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Member Functions | Private Attributes
cond::Cipher Class Reference

#include <Cipher.h>

Public Member Functions

std::string b64decrypt (const std::string &input)
 
std::string b64encrypt (const std::string &input)
 
 Cipher (const std::string &key)
 
std::string decrypt (const unsigned char *input, size_t inputSize)
 
size_t encrypt (const std::string &input, unsigned char *&output)
 
 ~Cipher ()
 

Private Member Functions

size_t bf_process_alloc (const unsigned char *input, size_t input_size, unsigned char *&output, bool decrypt=false)
 

Private Attributes

BLOWFISH_CTXm_ctx
 

Detailed Description

Definition at line 11 of file Cipher.h.

Constructor & Destructor Documentation

cond::Cipher::Cipher ( const std::string &  key)
explicit

Definition at line 10 of file Cipher.cc.

References Blowfish_Init(), relval_steps::k, and m_ctx.

10  :
11  m_ctx(new BLOWFISH_CTX){
12  char* k = const_cast<char*>(key.c_str());
13  Blowfish_Init( m_ctx, reinterpret_cast<unsigned char*>(k), key.size());
14 }
BLOWFISH_CTX * m_ctx
Definition: Cipher.h:32
void Blowfish_Init(BLOWFISH_CTX *ctx, unsigned char *key, int keyLen)
Definition: blowfish.cc:355
cond::Cipher::~Cipher ( )

Definition at line 16 of file Cipher.cc.

16  {
17  delete m_ctx;
18 }
BLOWFISH_CTX * m_ctx
Definition: Cipher.h:32

Member Function Documentation

std::string cond::Cipher::b64decrypt ( const std::string &  input)

Definition at line 117 of file Cipher.cc.

References base64_decode_alloc, input, run_regression::ret, AlCaHLTBitMon_QueryRunRegistry::string, and cond::throwException().

Referenced by cond::CredentialStore::exportAll(), cond::CredentialStore::importForPrincipal(), cond::CredentialStore::listConnections(), cond::CredentialStore::selectForUser(), cond::CredentialStore::setPermission(), cond::CredentialStore::startSession(), cond::CredentialStore::updateConnection(), and cond::CredentialStore::updatePrincipal().

117  {
118  if( b64in.empty() ) return "";
119  char* input = 0;
120  size_t inputSize = 0;
121  if( !base64_decode_alloc( b64in.c_str(), b64in.size(), &input, &inputSize ) ){
122  throwException("Input provided is not a valid base64 string.","Cipher::b64decrypt");
123  }
124  std::string ret = decrypt( reinterpret_cast<const unsigned char*>(input), inputSize );
125  free (input);
126  return ret;
127 }
std::string decrypt(const unsigned char *input, size_t inputSize)
Definition: Cipher.cc:79
static std::string const input
Definition: EdmProvDump.cc:43
#define base64_decode_alloc(in, inlen, out, outlen)
Definition: base64.h:60
void throwException(std::string const &message, std::string const &methodName)
Definition: Exception.cc:17
std::string cond::Cipher::b64encrypt ( const std::string &  input)

Definition at line 105 of file Cipher.cc.

References base64_encode_alloc(), edm::false, dbtoconf::out, run_regression::ret, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by cond::CredentialStore::addUser(), cond::CredentialStore::installAdmin(), cond::CredentialStore::setPermission(), cond::CredentialStore::updateConnection(), and cond::CredentialStore::updatePrincipal().

105  {
106  if( input.empty() ) return "";
107  unsigned char* out = 0;
108  size_t outSize = bf_process_alloc( reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), out, false );
109  char* b64out = 0;
110  size_t b64size = base64_encode_alloc( reinterpret_cast<const char*>(out), outSize, &b64out );
111  std::string ret( b64out, b64size );
112  free (out);
113  free (b64out);
114  return ret;
115 }
static std::string const input
Definition: EdmProvDump.cc:43
size_t bf_process_alloc(const unsigned char *input, size_t input_size, unsigned char *&output, bool decrypt=false)
Definition: Cipher.cc:20
size_t base64_encode_alloc(const char *in, size_t inlen, char **out)
Definition: base64.cc:117
tuple out
Definition: dbtoconf.py:99
volatile std::atomic< bool > shutdown_flag false
size_t cond::Cipher::bf_process_alloc ( const unsigned char *  input,
size_t  input_size,
unsigned char *&  output,
bool  decrypt = false 
)
private

Definition at line 20 of file Cipher.cc.

References assert(), Blowfish_Decrypt(), Blowfish_Encrypt(), i, j, dttmaxenums::L, and dttmaxenums::R.

23  {
24  assert(input_size != 0);
25 
26  uInt32 L, R;
27  unsigned int j = sizeof(uInt32);
28 
29  unsigned int output_size=0;
30 
31  if( !input_size ) {
32  output = 0;
33  return 0;
34  }
35 
36  for ( unsigned int i=0; i < input_size; i+=(j*2)){
37  output_size = i+2*j;
38  }
39  output = (unsigned char*) malloc( output_size );
40  memset(output, 0, output_size);
41 
42  for (unsigned int i=0; i < input_size; i+=(j*2)) {
43  L = R = 0;
44  unsigned int nl = 0;
45  unsigned int nr = 0;
46  if( input_size > i+j ){
47  nl = j;
48  if( input_size > i+2*j ){
49  nr = j;
50  } else {
51  nr = input_size-i-j;
52  }
53  } else {
54  nl = input_size-i;
55  nr = 0;
56  }
57  if(nl) memcpy(&L, input+i, nl);
58  if(nr) memcpy(&R, input+i+j, nr);
59  if( !decrypt ){
60  Blowfish_Encrypt(m_ctx, &L, &R);
61  } else {
62  Blowfish_Decrypt(m_ctx, &L, &R);
63  }
64  memcpy(output+i, &L, j);
65  memcpy(output+i+j, &R, j);
66  }
67 
68  return output_size;
69 }
int i
Definition: DBlmapReader.cc:9
BLOWFISH_CTX * m_ctx
Definition: Cipher.h:32
void Blowfish_Encrypt(BLOWFISH_CTX *ctx, uInt32 *xl, uInt32 *xr)
Definition: blowfish.cc:299
void Blowfish_Decrypt(BLOWFISH_CTX *ctx, uInt32 *xl, uInt32 *xr)
Definition: blowfish.cc:326
unsigned int uInt32
Definition: blowfish.h:26
assert(m_qm.get())
std::string decrypt(const unsigned char *input, size_t inputSize)
Definition: Cipher.cc:79
static std::string const input
Definition: EdmProvDump.cc:43
int j
Definition: DBlmapReader.cc:9
std::string cond::Cipher::decrypt ( const unsigned char *  input,
size_t  inputSize 
)

Definition at line 79 of file Cipher.cc.

References i, dbtoconf::out, run_regression::ret, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by cond::DecodingKey::init().

79  {
80  if( !inputSize ) return "";
81  unsigned char* out = 0;
82  size_t outSize = bf_process_alloc( input, inputSize, out, true );
83  size_t i = 0;
84  for( i=0;i<outSize; i++ ) {
85  if( out[i]==0 ) break;
86  }
87 
88  char* sout = reinterpret_cast<char*>(out);
89  // the output can still contain one or more \0 chars...
90  //size_t soutSize = strlen( sout );
91  size_t soutSize = 0;
92  for( soutSize=0; soutSize<outSize; soutSize++) if( out[soutSize]==0 ) break;
93 
94  if( soutSize < outSize ){
95  outSize = soutSize;
96  }
97 
98  std::string ret("");
99  if( outSize )
100  ret = std::string( sout, outSize );
101  free (out );
102  return ret;
103 }
int i
Definition: DBlmapReader.cc:9
static std::string const input
Definition: EdmProvDump.cc:43
size_t bf_process_alloc(const unsigned char *input, size_t input_size, unsigned char *&output, bool decrypt=false)
Definition: Cipher.cc:20
tuple out
Definition: dbtoconf.py:99
size_t cond::Cipher::encrypt ( const std::string &  input,
unsigned char *&  output 
)

Definition at line 71 of file Cipher.cc.

References edm::false, and convertSQLitetoXML_cfg::output.

Referenced by cond::DecodingKey::flush().

71  {
72  if( input.empty() ) {
73  output = 0;
74  return 0;
75  }
76  return bf_process_alloc( reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), output, false );;
77 }
static std::string const input
Definition: EdmProvDump.cc:43
size_t bf_process_alloc(const unsigned char *input, size_t input_size, unsigned char *&output, bool decrypt=false)
Definition: Cipher.cc:20
volatile std::atomic< bool > shutdown_flag false

Member Data Documentation

BLOWFISH_CTX* cond::Cipher::m_ctx
private

Definition at line 32 of file Cipher.h.

Referenced by Cipher().