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