~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to src/md5.h

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* md5.c - Functions to compute MD5 message digest of files or memory blocks
 
2
 * according to the definition of MD5 in RFC 1321 from April 1992.
 
3
 *
 
4
 * Thanks to Ulrich Drepper for the md5sum example code
 
5
 *
 
6
 * Copyright (C) 2002 by the Widelands Development Team
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License
 
10
 * as published by the Free Software Foundation; either version 2
 
11
 * of the License, or (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
21
 *
 
22
 */
 
23
 
 
24
#ifndef __S__MD5_H
 
25
#define __S__MD5_H
 
26
 
 
27
#include <iostream>
 
28
#include "types.h"
 
29
 
 
30
/**
 
31
 *
 
32
 * This class is responsible of creating a streamind md5 checksum.
 
33
 * You simply pass it the data (using pass_data()) and if you want
 
34
 * to read the checksum, you do a finish_chksum() and a get_chksum()
 
35
 *
 
36
 * Pros:        Checksum can be generated by streaming: simply call pass_data()
 
37
 * with every junk of data you've read from a file
 
38
 *
 
39
 * Cons: 16 byte checksum is to big for network transport
 
40
 *
 
41
 * ... sending checksums on every single packet is pointless when it's so
 
42
 * trivial to just fake them anyway.
 
43
 * The checksums are really most useful to verify that the game data
 
44
 * file(s) used on each host haven't accidently been modified.
 
45
 *
 
46
 * Let's face it, certain kinds of cheats can only be fought by security
 
47
 * through obscurity, and that's impossible in open-source (unless we write
 
48
 * absolutely horrible spaghetti code ...)
 
49
 */
 
50
class ChkSum {
 
51
                  ChkSum(const ChkSum&);
 
52
                  ChkSum operator=(const ChkSum&);
 
53
 
 
54
                  public:
 
55
                                         ChkSum(void);
 
56
                                         ~ChkSum(void);
 
57
                                         void pass_data(const void*, uint);
 
58
                                         void finish_chksum(void);
 
59
                                         ulong* get_chksum(void) const { if(can_handle_data) return 0; return (ulong*)sum; }
 
60
 
 
61
 
 
62
                  private:
 
63
                          enum { BLOCKSIZE = 4096 };
 
64
                                         static const unsigned char fillbuf[64];
 
65
 
 
66
                                         /* Structure to save state of computation between the single steps.  */
 
67
                                         struct md5_ctx
 
68
                                         {
 
69
                                                                ulong A;
 
70
                                                                ulong B;
 
71
                                                                ulong C;
 
72
                                                                ulong D;
 
73
 
 
74
                                                                ulong total[2];
 
75
                                                                ulong buflen;
 
76
                                                                char buffer[128];
 
77
                                         } ctx;
 
78
 
 
79
                                         void md5_process_block (const void*, ulong, md5_ctx*);
 
80
                                         void md5_process_bytes (const void*, ulong, md5_ctx*);
 
81
                                         void* md5_finish_ctx (md5_ctx*, void*);
 
82
 
 
83
                                         char buf[BLOCKSIZE+72];
 
84
                                         uint nread;
 
85
                                         char sum[16];
 
86
                                         bool can_handle_data;
 
87
 
 
88
 
 
89
};
 
90
 
 
91
// operator overloading
 
92
bool operator==(ChkSum&, ChkSum&);
 
93
bool operator==(ChkSum&, const void*);
 
94
std::ostream& operator<<(std::ostream&, ChkSum&);
 
95
 
 
96
#endif /* __S__MD5_H */