hashapp.cpp

// LICENSE: (Please see the file COPYING for details)
//
// NUS - Nemesis Utilities System: A C++ application development framework 
// Copyright (C) 2006, 2008 Otavio Rodolfo Piske
//
//  This file is part of NUS
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation version 2.1
//  of the License.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
#ifndef HASHAPP_H
#define HASHAPP_H

#include "ndefs.h"
#include "nobject.h"
#include "nopt.h"
#include "nabstracthash.h"
#include "nmd5hash.h"
#include "nmd4hash.h"
#include "nmd2hash.h"
#include "nshahash.h"
#include "nsha1hash.h"

class HashApp: public NObject {
      enum Hashes {
            MD2,
            MD4,
            MD5,
            SHA,
            SHA1
      };
      
      enum Object {
            FILE_OBJ,
            STRING_OBJ
      };
            
      enum Actions {
            CALCULATE,
            COMPARE
      };
            
      public:
            HashApp(void);
            ~HashApp(void);
            
            bool setHash(const NOpt<HashApp> &opt);
            bool compare(const NOpt<HashApp> &opt);
            bool calculate(const NOpt<HashApp> &opt);
            
            bool setFileObj(const NOpt<HashApp> &opt);
            bool setStringObj(const NOpt<HashApp> &opt);
            
            void run(void);
            
      private:
            NAbstractHash *m_hash_obj;
            Hashes m_hash_type;
            Actions m_action;
            Object m_object;
            NString m_object_name;
            NString m_object_hash;
            
            void execCompare(void);
            void execCalculate(void);
            
            HashApp(const HashApp &other);
            HashApp &operator=(const HashApp &rhs);
};

#endif // HASHAPP_H
// LICENSE: (Please see the file COPYING for details)
//
// NUS - Nemesis Utilities System: A C++ application development framework 
// Copyright (C) 2006, 2008 Otavio Rodolfo Piske
//
//  This file is part of NUS
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation version 2.1
//  of the License.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
#include "ndefs.h"
#include "nhash.h"
#include "nmd5hash.h"
#include "ngetopt.h"
#include "noutput.h"
#include "hashapp.h"

void setup_opts(NGetOpt<HashApp> *opts) { 
      opts->add("h", "help", false, "Display the help for this program", NULL);
      opts->add("n", "name", true, "The name of the hash to use", 
                &HashApp::setHash);
      opts->add("c", "calculate", false, "Calculate the hash of a given object", 
                &HashApp::calculate);
      opts->add("C", "compare", true, "Compare a hash with an object",
                  &HashApp::compare);
      opts->add("f", "file", true, "Computes/compare the hash of a file",
                  &HashApp::setFileObj); 
      opts->add("s", "string", true, "Computes/compare the hash of a string",
                  &HashApp::setStringObj);
}



int main(int argc, char **argv) {
      HashApp app;
      NGetOpt<HashApp> opts(argc, argv, &app, 1);

      // Setup the command line flags (see NGetOpt documentation for details)
      setup_opts(&opts);
      try { 
            // Run the program, if given command line flags or show the help
            if (argc > 1) { 
                  if (opts.proccess()) { 
                        app.run();
                  }
            }
            else { 
                  opts.help();
            }
      }
      catch (NException &e) { 
            NWarning::print() << e.getDescription();
            exit(1);          
      }

      return 0;
}

// LICENSE: (Please see the file COPYING for details)
//
// NUS - Nemesis Utilities System: A C++ application development framework 
// Copyright (C) 2006, 2007 Otavio Rodolfo Piske
//
//  This file is part of NUS
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation version 2.1
//  of the License.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
#include "hashapp.h"

HashApp::HashApp(void)
      : m_hash_obj(NULL),
      m_hash_type(HashApp::MD5),
      m_action(HashApp::CALCULATE),
      m_object(HashApp::FILE_OBJ),
      m_object_name(),
      m_object_hash()
{

}


HashApp::~HashApp(void) {
      delete m_hash_obj;
}


bool HashApp::setHash(const NOpt<HashApp> &opt) {
      NString name;
      
      // Gets the hash type informed in the command line
      name = opt.getValues().at(0);
      if (name == "md2") {
            m_hash_type = HashApp::MD2;
            m_hash_obj = new NMd2Hash;
            return true;
      }
      
      if (name == "md4") {
            m_hash_type = HashApp::MD4;
            m_hash_obj = new NMd4Hash;
            return true;
      }
      
      if (name == "md5") {
            m_hash_type = HashApp::MD5;
            m_hash_obj = new NMd5Hash;
            return true;
      }
      
      if (name == "sha") {
            m_hash_type = HashApp::SHA;
            m_hash_obj = new NShaHash;
            return true;
      }
      
      if (name == "sha1") {
            m_hash_type = HashApp::SHA1;
            m_hash_obj = new NSha1Hash;
            return true;
      }
      
      NWarning::print() << name << " is not a valid hash name.";
      NMessage::print() << "Acceptable names: md2, md4, md5, sha, sha1, rmd160";
      return false;
}


bool HashApp::compare(const NOpt<HashApp> &opt) {
      m_action = HashApp::COMPARE;
      
      m_object_hash = opt.getValues().at(0);
      
      return true;
}


bool HashApp::calculate(const NOpt<HashApp> &) {
      m_action = HashApp::CALCULATE;
      
      return true;
}


bool HashApp::setFileObj(const NOpt<HashApp> &opt) {
      m_object = HashApp::FILE_OBJ;
      m_object_name = opt.getValues().at(0);
      
      return true;
}


bool HashApp::setStringObj(const NOpt<HashApp> &opt) {
      m_object = HashApp::STRING_OBJ;
      m_object_name = opt.getValues().at(0);
      
      return true;
}


void HashApp::execCalculate(void) {
      if (m_object == HashApp::FILE_OBJ) {
            // Calculates the hash for a file
            NMessage::print() << "Hash is " 
                  << m_hash_obj->calculateFile(m_object_name).getValue();
      }
      else {
            // Calculates the hash for a string
            NMessage::print() << "Hash is " 
                  <<    m_hash_obj->calculate(m_object_name).getValue();
      }
}


// Compares the hash
void HashApp::execCompare(void) {
      if (m_object == HashApp::FILE_OBJ) {
            NMessage::print() << "Not implemented";
      }
      else {
            if (m_hash_obj->match(m_object_name, m_object_hash)) {
                  NMessage::print() << "Matches!";
            }
            else {
                  NMessage::print() << "Do not match";
            }
      }

}


void HashApp::run(void) {
      if (!m_hash_obj) {
            NWarning::print() << "You must provide a hash type";
            return;
      }
      
      if (m_object_name == "") {
            NWarning::print() << "You must provide an object name";
            return;
      }
      
      if (m_action == HashApp::COMPARE) {
            execCompare();
      }
      else {
            execCalculate();
      }
}

Generated on Wed Mar 5 23:10:35 2008 for NemesisUtilitiesSystem by  doxygen 1.5.4