6375
|
1 /* |
|
2 |
|
3 Copyright (C) 2007 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 2, or (at your option) any |
|
10 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, write to the Free |
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #include <string> |
|
25 #include <vector> |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include "config.h" |
|
29 #endif |
|
30 |
6383
|
31 #include "lo-error.h" |
6375
|
32 #include "oct-md5.h" |
|
33 #include "md5.h" |
|
34 |
|
35 std::string |
|
36 oct_md5 (const std::string str) |
|
37 { |
|
38 md5_state_t state; |
|
39 |
|
40 OCTAVE_LOCAL_BUFFER (md5_byte_t, digest, 16); |
|
41 md5_init (&state); |
|
42 md5_append (&state, reinterpret_cast<const md5_byte_t *>(str.c_str()), |
|
43 str.length()); |
|
44 md5_finish (&state, digest); |
|
45 |
|
46 OCTAVE_LOCAL_BUFFER (char, tmp, 33); |
|
47 for (octave_idx_type i = 0; i < 16; i++) |
|
48 sprintf (&tmp[2*i], "%02x", digest[i]); |
|
49 tmp[32] = 0; |
|
50 return std::string (tmp); |
|
51 } |
|
52 |
6383
|
53 std::string |
|
54 oct_md5_file (const std::string file) |
|
55 { |
|
56 FILE *ifile = fopen (file.c_str (), "rb"); |
|
57 |
|
58 if (! ifile) |
|
59 { |
|
60 (*current_liboctave_error_handler) ("unable to open file `%s' for writing", |
|
61 file.c_str()); |
|
62 return std::string(); |
|
63 } |
|
64 else |
|
65 { |
|
66 md5_state_t state; |
|
67 size_t nel; |
|
68 |
|
69 OCTAVE_LOCAL_BUFFER (md5_byte_t, digest, 16); |
|
70 OCTAVE_LOCAL_BUFFER (md5_byte_t, buf, 1024); |
|
71 |
|
72 md5_init (&state); |
|
73 |
|
74 while ((nel = fread (buf, 1, 1024, ifile))) |
|
75 md5_append (&state, buf, nel); |
|
76 |
|
77 fclose (ifile); |
|
78 |
|
79 md5_finish (&state, digest); |
|
80 |
|
81 OCTAVE_LOCAL_BUFFER (char, tmp, 33); |
|
82 for (octave_idx_type i = 0; i < 16; i++) |
|
83 sprintf (&tmp[2*i], "%02x", digest[i]); |
|
84 tmp[32] = 0; |
|
85 return std::string (tmp); |
|
86 } |
|
87 } |
|
88 |
6375
|
89 /* |
|
90 ;;; Local Variables: *** |
|
91 ;;; mode: C++ *** |
|
92 ;;; End: *** |
|
93 */ |