GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-md5.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2007-2015 David Bateman
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <cstdio>
28 
29 #include <string>
30 #include <vector>
31 
32 #include "lo-error.h"
33 #include "oct-md5.h"
34 #include "md5.h"
35 
36 static std::string
37 oct_md5_result_to_str (const unsigned char *buf)
38 {
39  char tmp[33];
40 
41  sprintf (tmp,
42  "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
43  buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
44  buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14],
45  buf[15]);
46 
47  return std::string (tmp, 32);
48 }
49 
50 std::string
51 oct_md5 (const std::string str)
52 {
53  unsigned char buf[16];
54 
55  md5_buffer (str.data (), str.length (), buf);
56 
57  return oct_md5_result_to_str (buf);
58 }
59 
60 std::string
61 oct_md5_file (const std::string file)
62 {
63  std::string retval;
64 
65  FILE *ifile = gnulib::fopen (file.c_str (), "rb");
66 
67  if (ifile)
68  {
69  unsigned char buf[16];
70 
71  int errflag = md5_stream (ifile, buf);
72 
73  gnulib::fclose (ifile);
74 
75  if (! errflag)
76  retval = oct_md5_result_to_str (buf);
77  else
78  (*current_liboctave_error_handler) ("internal error in md5_stream");
79  }
80  else
81  (*current_liboctave_error_handler) ("unable to open file '%s' for reading",
82  file.c_str ());
83 
84  return retval;
85 }
liboctave_error_handler current_liboctave_error_handler
Definition: lo-error.c:38
std::string oct_md5(const std::string str)
Definition: oct-md5.cc:51
std::string oct_md5_file(const std::string file)
Definition: oct-md5.cc:61
static std::string oct_md5_result_to_str(const unsigned char *buf)
Definition: oct-md5.cc:37