aboutsummaryrefslogtreecommitdiff
path: root/dep/g3dlite/include/G3D/Crypto.h
blob: 56c816a4977be39b3b61374f0b0adf29bb96e869 (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
/** 
  @file Crypto.h
 
  @maintainer Morgan McGuire, http://graphics.cs.williams.edu
 

  @created 2006-03-29
  @edited  2006-04-06
 */

#ifndef G3D_CRYPTO_H
#define G3D_CRYPTO_H

#include "G3D/platform.h"
#include "G3D/g3dmath.h"
#include <string>

namespace G3D {

/** See G3D::Crypto::md5 */
class MD5Hash {
private:

    uint8   value[16];

public:

    MD5Hash() {
        for (int i = 0; i < 16; ++i) {
            value[i] = 0;
        }
    }

    explicit MD5Hash(class BinaryInput& b);

    uint8& operator[](int i) {
        return value[i];
    }

    const uint8& operator[](int i) const {
        return value[i];
    }

    bool operator==(const MD5Hash& other) const {
        bool match = true;
        for (int i = 0; i < 16; ++i) {
            match = match && (other.value[i] == value[i]);
        }
        return match;
    }

    inline bool operator!=(const MD5Hash& other) const {
        return !(*this == other);
    }

    void deserialize(class BinaryInput& b);

    void serialize(class BinaryOutput& b) const;
};


/** Cryptography and hashing helper functions */
class Crypto {
public:

    /**
     Computes the CRC32 value of a byte array.  CRC32 is designed to be a hash
     function that produces different values for similar strings.

     This implementation is compatible with PKZIP and GZIP.

     Based on http://www.gamedev.net/reference/programming/features/crc32/
    */
    static uint32 crc32(const void* bytes, size_t numBytes);

    /**
     Computes the MD5 hash (message digest) of a byte stream, as defined by
     http://www.ietf.org/rfc/rfc1321.txt.

     @cite Based on implementation by L. Peter Deutsch, ghost@aladdin.com
     */
    MD5Hash md5(const void* bytes, size_t numBytes);

    /**
     Returns the nth prime less than 2000 in constant time.  The first prime has index
     0 and is the number 2.
     */
    static int smallPrime(int n);

    /** Returns 1 + the largest value that can be passed to smallPrime. */
    static int numSmallPrimes();
};

}

#endif