CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
cond::auth::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 13 of file Cipher.h.

Constructor & Destructor Documentation

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

Definition at line 10 of file Cipher.cc.

References Blowfish_Init(), gen::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 }
void Blowfish_Init(BLOWFISH_CTX *ctx, unsigned char *key, int keyLen)
Definition: blowfish.cc:355
int k[5][pyjets_maxn]
BLOWFISH_CTX * m_ctx
Definition: Cipher.h:34
cond::auth::Cipher::~Cipher ( )

Definition at line 16 of file Cipher.cc.

References m_ctx.

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

Member Function Documentation

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

Definition at line 117 of file Cipher.cc.

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

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

117  {
118  if( b64in.empty() ) return "";
119  char* input = nullptr;
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 }
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:21
static std::string const input
Definition: EdmProvDump.cc:48
#define base64_decode_alloc(in, inlen, out, outlen)
Definition: base64.h:60
std::string decrypt(const unsigned char *input, size_t inputSize)
Definition: Cipher.cc:79
std::string cond::auth::Cipher::b64encrypt ( const std::string &  input)

Definition at line 105 of file Cipher.cc.

References base64_encode_alloc(), bf_process_alloc(), funct::false, MillePedeFileConverter_cfg::out, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by cond::setPermissionData(), cond::updateConnectionData(), and cond::updatePrincipalData().

105  {
106  if( input.empty() ) return "";
107  unsigned char* out = nullptr;
108  size_t outSize = bf_process_alloc( reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), out, false );
109  char* b64out = nullptr;
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:48
size_t base64_encode_alloc(const char *in, size_t inlen, char **out)
Definition: base64.cc:117
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 cond::auth::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 Blowfish_Decrypt(), Blowfish_Encrypt(), mps_fire::i, dttmaxenums::L, m_ctx, and dttmaxenums::R.

Referenced by b64encrypt(), decrypt(), and encrypt().

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 = nullptr;
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 }
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
static std::string const input
Definition: EdmProvDump.cc:48
std::string decrypt(const unsigned char *input, size_t inputSize)
Definition: Cipher.cc:79
BLOWFISH_CTX * m_ctx
Definition: Cipher.h:34
std::string cond::auth::Cipher::decrypt ( const unsigned char *  input,
size_t  inputSize 
)

Definition at line 79 of file Cipher.cc.

References bf_process_alloc(), mps_fire::i, MillePedeFileConverter_cfg::out, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by b64decrypt(), and cond::auth::DecodingKey::init().

79  {
80  if( !inputSize ) return "";
81  unsigned char* out = nullptr;
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 }
static std::string const input
Definition: EdmProvDump.cc:48
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 cond::auth::Cipher::encrypt ( const std::string &  input,
unsigned char *&  output 
)

Definition at line 71 of file Cipher.cc.

References bf_process_alloc(), funct::false, and convertSQLitetoXML_cfg::output.

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

71  {
72  if( input.empty() ) {
73  output = nullptr;
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:48
size_t bf_process_alloc(const unsigned char *input, size_t input_size, unsigned char *&output, bool decrypt=false)
Definition: Cipher.cc:20

Member Data Documentation

BLOWFISH_CTX* cond::auth::Cipher::m_ctx
private

Definition at line 34 of file Cipher.h.

Referenced by bf_process_alloc(), Cipher(), and ~Cipher().