CMS 3D CMS Logo

Public Member Functions | Static Public Member Functions | Private Attributes

cond::DecodingKey Class Reference

#include <DecodingKey.h>

List of all members.

Public Member Functions

const std::string & dataSource () const
 DecodingKey ()
const std::string & key () const
bool readFromFile (const std::string &password, const std::string &keyFileName)
bool readFromString (const std::string &password, const std::string &content)
bool readUserKey (const std::string &keyFileName)
bool readUserKeyString (const std::string &content)
virtual ~DecodingKey ()

Static Public Member Functions

static bool createFile (const std::string &password, const std::string &key, const std::string &dataSource, const std::string &keyFileName)
static std::string getUserName ()
static bool validateKey (const std::string &key)

Private Attributes

std::string m_dataSource
std::string m_key

Detailed Description

Definition at line 7 of file DecodingKey.h.


Constructor & Destructor Documentation

cond::DecodingKey::DecodingKey ( ) [inline]

Definition at line 44 of file DecodingKey.h.

                            :m_key(""),m_dataSource(""){
}
virtual cond::DecodingKey::~DecodingKey ( ) [inline, virtual]

Definition at line 13 of file DecodingKey.h.

{}

Member Function Documentation

bool cond::DecodingKey::createFile ( const std::string &  password,
const std::string &  key,
const std::string &  dataSource,
const std::string &  keyFileName 
) [static]

Definition at line 66 of file DecodingKey.cc.

References DecodingKeySeparator(), alcaDQMUpload::encode(), Exception, keyFile(), and MatrixRunner::msg.

                                                                                             {
  if(password.empty()){
    std::stringstream msg;
    msg << "Provided password is empty.";
    throw cond::Exception(msg.str());    
  }
  std::string content("");
  validateKey(key);
  if(dataSource.find(DecodingKeySeparator)!=std::string::npos){
    std::stringstream msg;
    msg << "Invalid character ';' found in data file name string.";
    throw cond::Exception(msg.str());    
  }
  content.append(key).append(1,DecodingKeySeparator).append(dataSource);
  std::string encodedContent = coral::Cipher::encode(content,password);
  std::ofstream keyFile;
  keyFile.open(keyFileName.c_str());
  if(!keyFile.good()){
    keyFile.close();
    std::stringstream msg;
    msg << "Cannot open the key file \""<<keyFileName<<"\"";
    throw cond::Exception(msg.str());
  }
  keyFile << encodedContent;
  keyFile.flush();
  keyFile.close();
  return true;
}
const std::string & cond::DecodingKey::dataSource ( ) const [inline]
std::string cond::DecodingKey::getUserName ( void  ) [static]

Definition at line 49 of file DecodingKey.cc.

References Exception, and MatrixRunner::msg.

Referenced by readUserKey().

                                      {
  std::string userName("");
  struct passwd* userp = ::getpwuid(::getuid());
  if(userp) {
    char* uName = userp->pw_name;
    if(uName){
      userName += uName;
    }
  }
  if(userName.empty()){
    std::stringstream msg;
    msg << "Cannot determine login name.";
    throw cond::Exception(msg.str());     
  }
  return userName;
}
const std::string & cond::DecodingKey::key ( ) const [inline]
bool cond::DecodingKey::readFromFile ( const std::string &  password,
const std::string &  keyFileName 
)

Definition at line 20 of file DecodingKey.cc.

References cond::FileReader::content(), and cond::FileReader::read().

Referenced by readUserKey().

                                                                                         {
  cond::FileReader reader;
  reader.read(keyFileName);
  return readFromString(password,reader.content());
}
bool cond::DecodingKey::readFromString ( const std::string &  password,
const std::string &  content 
)

Definition at line 26 of file DecodingKey.cc.

References edm::decode(), DecodingKeySeparator(), Exception, MatrixRunner::msg, and pos.

                                                                                       {
  std::string decodedContent = coral::Cipher::decode(content,password);
  size_t pos = decodedContent.find(DecodingKeySeparator);
  if(pos==std::string::npos || pos==0){
    std::stringstream msg;
    msg << "Provided Key is invalid.";
    throw cond::Exception(msg.str());    
  }
  m_key = decodedContent.substr(0,pos);
  m_dataSource = decodedContent.substr(pos+1);
  return true;
}
bool cond::DecodingKey::readUserKey ( const std::string &  keyFileName)

Definition at line 12 of file DecodingKey.cc.

References getUserName(), and readFromFile().

                                                             {
  return readFromFile(getUserName(), keyFileName);
}
bool cond::DecodingKey::readUserKeyString ( const std::string &  content)
bool cond::DecodingKey::validateKey ( const std::string &  key) [static]

Definition at line 39 of file DecodingKey.cc.

References DecodingKeySeparator(), Exception, and MatrixRunner::msg.

                                                     {
  if(key.find(DecodingKeySeparator)!=std::string::npos){
    std::stringstream msg;
    msg << "Invalid character ';' found in key string.";
    throw cond::Exception(msg.str());    
  }
  return true;
}

Member Data Documentation

std::string cond::DecodingKey::m_dataSource [private]

Definition at line 38 of file DecodingKey.h.

std::string cond::DecodingKey::m_key [private]

Definition at line 36 of file DecodingKey.h.