1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkMD5_DEFINED
9 #define SkMD5_DEFINED
10 
11 #include "SkStream.h"
12 #include "SkTo.h"
13 
14 /* Calculate a 128-bit MD5 message-digest of the bytes sent to this stream. */
15 class SkMD5 : public SkWStream {
16 public:
17     SkMD5();
18 
19     /** Processes input, adding it to the digest.
20         Calling this after finish is undefined.  */
21     bool write(const void* buffer, size_t size) final;
22 
bytesWritten()23     size_t bytesWritten() const final { return SkToSizeT(this->byteCount); }
24 
25     struct Digest {
26         uint8_t data[16];
27         bool operator ==(Digest const& other) const {
28             return 0 == memcmp(data, other.data, sizeof(data));
29         }
30         bool operator !=(Digest const& other) const { return !(*this == other); }
31     };
32 
33     /** Computes and returns the digest. */
34     void finish(Digest& digest);
35 
36 private:
37     uint64_t byteCount;  // number of bytes, modulo 2^64
38     uint32_t state[4];   // state (ABCD)
39     uint8_t buffer[64];  // input buffer
40 };
41 
42 #endif
43