oct-md5.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2007-2012 David Bateman
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include "config.h"
00025 #endif
00026 
00027 #include <cstdio>
00028 
00029 #include <string>
00030 #include <vector>
00031 
00032 #include "lo-error.h"
00033 #include "oct-md5.h"
00034 #include "md5.h"
00035 
00036 static std::string
00037 oct_md5_result_to_str (const unsigned char *buf)
00038 {
00039   char tmp [33];
00040 
00041   sprintf (tmp,
00042            "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
00043            buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
00044            buf[8],  buf[9], buf[10], buf[11], buf[12], buf[13], buf[14],
00045            buf[15]);
00046 
00047   return std::string (tmp, 32);
00048 }
00049 
00050 std::string
00051 oct_md5 (const std::string str)
00052 {
00053   unsigned char buf[16];
00054 
00055   md5_buffer (str.data (), str.length (), buf);
00056 
00057   return oct_md5_result_to_str (buf);
00058 }
00059 
00060 std::string
00061 oct_md5_file (const std::string file)
00062 {
00063   std::string retval;
00064 
00065   FILE *ifile = gnulib::fopen (file.c_str (), "rb");
00066 
00067   if (ifile)
00068     {
00069       unsigned char buf[16];
00070 
00071       int errflag = md5_stream (ifile, buf);
00072 
00073       gnulib::fclose (ifile);
00074 
00075       if (! errflag)
00076         retval = oct_md5_result_to_str (buf);
00077       else
00078         (*current_liboctave_error_handler) ("internal error in md5_stream");
00079     }
00080   else
00081     (*current_liboctave_error_handler) ("unable to open file '%s' for reading",
00082                                         file.c_str());
00083 
00084   return retval;
00085 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines