summaryrefslogtreecommitdiff
path: root/sid/sidplay-libs-2.1.0/libsidutils/src/MD5/MD5.h
blob: 7ced54f7b1193bfe18011abf2d3875c2d53bc234 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * This code has been derived by Michael Schwendt <mschwendt@yahoo.com>
 * from original work by L. Peter Deutsch <ghost@aladdin.com>.
 * 
 * The original C code (md5.c, md5.h) is available here:
 * ftp://ftp.cs.wisc.edu/ghost/packages/md5.tar.gz
 */

/*
  Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  L. Peter Deutsch
  ghost@aladdin.com
 */

#ifndef MD5_H
#define MD5_H

#include "MD5_Defs.h"

typedef unsigned char md5_byte_t;  // 8-bit byte
typedef unsigned int md5_word_t;   // 32-bit word

class MD5
{
 public:
    // Initialize the algorithm. Reset starting values.
    MD5();
    
    // Append a string to the message.
    void append(const void* data, int nbytes);

    // Finish the message.
    void finish();

    // Return pointer to 16-byte fingerprint.
    const md5_byte_t* getDigest();
    
    // Initialize the algorithm. Reset starting values.
    void reset();

 private:

    /* Define the state of the MD5 Algorithm. */
    md5_word_t count[2];	/* message length in bits, lsw first */
    md5_word_t abcd[4];		/* digest buffer */
    md5_byte_t buf[64];		/* accumulate block */

    md5_byte_t digest[16];
        
    md5_word_t tmpBuf[16];
    const md5_word_t* X;
    
    void
    process(const md5_byte_t data[64]);

    md5_word_t
    ROTATE_LEFT(const md5_word_t x, const int n);
    
    md5_word_t
    F(const md5_word_t x, const md5_word_t y, const md5_word_t z);
        
    md5_word_t
    G(const md5_word_t x, const md5_word_t y, const md5_word_t z);

    md5_word_t
    H(const md5_word_t x, const md5_word_t y, const md5_word_t z);

    md5_word_t
    I(const md5_word_t x, const md5_word_t y, const md5_word_t z);

    typedef md5_word_t (MD5::*md5func)(const md5_word_t x, const md5_word_t y, const md5_word_t z);
        
    void
    SET(md5func func, md5_word_t& a, md5_word_t& b, md5_word_t& c,
        md5_word_t& d, const int k, const int s,
        const md5_word_t Ti);
};

inline md5_word_t
MD5::ROTATE_LEFT(const md5_word_t x, const int n)
{
    return ( (x<<n) | (x>>(32-n)) );
}

inline md5_word_t
MD5::F(const md5_word_t x, const md5_word_t y, const md5_word_t z)
{
    return ( (x&y) | (~x&z) );
}

inline md5_word_t
MD5::G(const md5_word_t x, const md5_word_t y, const md5_word_t z)
{
    return ( (x&z) | (y&~z) );
}

inline md5_word_t
MD5::H(const md5_word_t x, const md5_word_t y, const md5_word_t z)
{
    return ( x^y^z );
}

inline md5_word_t
MD5::I(const md5_word_t x, const md5_word_t y, const md5_word_t z)
{
    return ( y ^ (x|~z) );
}

inline void
MD5::SET(md5func func, md5_word_t& a, md5_word_t& b, md5_word_t& c,
          md5_word_t& d, const int k, const int s,
          const md5_word_t Ti)
{
    md5_word_t t = a + (this->*func)(b,c,d) + X[k] + Ti;
    a = ROTATE_LEFT(t, s) + b;
}

#endif  /* MD5_H */