1 /* rep-md5.c -- wrap some md5 functions
2 
3    Copyright (C) 2001 John Harper <jsh@pixelslut.com>
4 
5    $Id$
6 
7    This file is part of librep.
8 
9    librep is free software; you can redistribute it and/or modify it
10    under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2, or (at your option)
12    any later version.
13 
14    librep is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with librep; see the file COPYING.	If not, write to
21    the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
22 
23 #define _GNU_SOURCE
24 
25 #include <config.h>
26 #include "repint.h"
27 
28 #include "md5.h"
29 
30 static repv
digest_to_repv(char digest[16])31 digest_to_repv (char digest[16])
32 {
33     static const char hex_digits[16] = "0123456789abcdef";
34     char hex_digest[32];
35     int i;
36 
37     /* Currently rep has no interface to create bignums directly,
38        so format to a hex-encoded string, then reparse it.
39 
40        XXX This loses if rep was compiled without GMP support.. */
41 
42     for (i = 0; i < 16; i++)
43     {
44 	hex_digest[i*2] = hex_digits[digest[i] & 15];
45 	hex_digest[i*2+1] = hex_digits[digest[i] >> 4];
46     }
47 
48     return rep_parse_number (hex_digest, 32, 16, 1, 0);
49 }
50 
51 DEFUN ("md5-string", Fmd5_string, Smd5_string, (repv data), rep_Subr1) /*
52 ::doc:rep.util.md5#md5-string::
53 md5-string STRING
54 
55 Return the integer representing the MD5 message digest of the bytes
56 stored in STRING. This integer will have no more than 128 significant
57 bits.
58 ::end:: */
59 {
60     char digest[16];
61 
62     rep_DECLARE1 (data, rep_STRINGP);
63 
64     md5_buffer (rep_STR (data), rep_STRING_LEN (data), digest);
65 
66     return digest_to_repv (digest);
67 }
68 
69 DEFUN ("md5-local-file", Fmd5_local_file,
70        Smd5_local_file, (repv file), rep_Subr1) /*
71 ::doc:rep.util.md5#md5-local-file::
72 md5-local-file LOCAL-FILE-NAME
73 
74 Return the integer representing the MD5 message digest of the bytes
75 stored in the file called LOCAL-FILE-NAME (which must name a file in
76 the local filing system). The returned integer will have no more than
77 128 significant bits.
78 ::end:: */
79 {
80     FILE *fh;
81     char digest[16];
82 
83     rep_DECLARE1 (file, rep_STRINGP);
84 
85     fh = fopen (rep_STR (file), "r");
86     if (fh == 0)
87 	return rep_signal_file_error (file);
88 
89     md5_stream (fh, digest);
90     fclose (fh);
91 
92     return digest_to_repv (digest);
93 }
94 
95 repv
rep_dl_init(void)96 rep_dl_init (void)
97 {
98     repv tem = rep_push_structure ("rep.util.md5");
99     rep_ADD_SUBR(Smd5_string);
100     rep_ADD_SUBR(Smd5_local_file);
101     return rep_pop_structure (tem);
102 }
103