aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkMD5.h
blob: 5ae14e10c363059bb07a5e566ae4316d8aa628df (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
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkMD5_DEFINED
#define SkMD5_DEFINED

#include "SkStream.h"
#include "SkTo.h"

/* Calculate a 128-bit MD5 message-digest of the bytes sent to this stream. */
class SkMD5 : public SkWStream {
public:
    SkMD5();

    /** Processes input, adding it to the digest.
        Calling this after finish is undefined.  */
    bool write(const void* buffer, size_t size) final;

    size_t bytesWritten() const final { return SkToSizeT(this->byteCount); }

    struct Digest {
        uint8_t data[16];
        bool operator ==(Digest const& other) const {
            return 0 == memcmp(data, other.data, sizeof(data));
        }
        bool operator !=(Digest const& other) const { return !(*this == other); }
    };

    /** Computes and returns the digest. */
    void finish(Digest& digest);

private:
    uint64_t byteCount;  // number of bytes, modulo 2^64
    uint32_t state[4];   // state (ABCD)
    uint8_t buffer[64];  // input buffer
};

#endif