Mercurial > hg > octave-lyh
annotate liboctave/oct-md5.cc @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 93cf10950334 |
children | acd5e9df38f8 |
rev | line source |
---|---|
6375 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2007, 2008, 2009 David Bateman |
6375 | 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 #ifdef HAVE_CONFIG_H | |
24 #include "config.h" | |
25 #endif | |
26 | |
8521 | 27 #include <cstdio> |
28 | |
29 #include <string> | |
30 #include <vector> | |
31 | |
6383 | 32 #include "lo-error.h" |
6375 | 33 #include "oct-md5.h" |
34 #include "md5.h" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
7016
diff
changeset
|
35 #include "oct-locbuf.h" |
6375 | 36 |
37 std::string | |
38 oct_md5 (const std::string str) | |
39 { | |
40 md5_state_t state; | |
41 | |
42 OCTAVE_LOCAL_BUFFER (md5_byte_t, digest, 16); | |
43 md5_init (&state); | |
44 md5_append (&state, reinterpret_cast<const md5_byte_t *>(str.c_str()), | |
45 str.length()); | |
46 md5_finish (&state, digest); | |
47 | |
48 OCTAVE_LOCAL_BUFFER (char, tmp, 33); | |
49 for (octave_idx_type i = 0; i < 16; i++) | |
50 sprintf (&tmp[2*i], "%02x", digest[i]); | |
51 tmp[32] = 0; | |
52 return std::string (tmp); | |
53 } | |
54 | |
6383 | 55 std::string |
56 oct_md5_file (const std::string file) | |
57 { | |
58 FILE *ifile = fopen (file.c_str (), "rb"); | |
59 | |
60 if (! ifile) | |
61 { | |
62 (*current_liboctave_error_handler) ("unable to open file `%s' for writing", | |
63 file.c_str()); | |
64 return std::string(); | |
65 } | |
66 else | |
67 { | |
68 md5_state_t state; | |
69 size_t nel; | |
70 | |
71 OCTAVE_LOCAL_BUFFER (md5_byte_t, digest, 16); | |
72 OCTAVE_LOCAL_BUFFER (md5_byte_t, buf, 1024); | |
73 | |
74 md5_init (&state); | |
75 | |
76 while ((nel = fread (buf, 1, 1024, ifile))) | |
77 md5_append (&state, buf, nel); | |
78 | |
79 fclose (ifile); | |
80 | |
81 md5_finish (&state, digest); | |
82 | |
83 OCTAVE_LOCAL_BUFFER (char, tmp, 33); | |
84 for (octave_idx_type i = 0; i < 16; i++) | |
85 sprintf (&tmp[2*i], "%02x", digest[i]); | |
86 tmp[32] = 0; | |
87 return std::string (tmp); | |
88 } | |
89 } | |
90 | |
6375 | 91 /* |
92 ;;; Local Variables: *** | |
93 ;;; mode: C++ *** | |
94 ;;; End: *** | |
95 */ |