• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkStream.h"
12 #include "include/private/base/SkTo.h"
13 
14 #include <cstdint>
15 #include <cstring>
16 
17 /* Calculate a 128-bit MD5 message-digest of the bytes sent to this stream. */
18 class SkMD5 : public SkWStream {
19 public:
20     SkMD5();
21 
22     /** Processes input, adding it to the digest.
23         Calling this after finish is undefined.  */
24     bool write(const void* buffer, size_t size) final;
25 
bytesWritten()26     size_t bytesWritten() const final { return SkToSizeT(this->byteCount); }
27 
28     struct Digest {
29         uint8_t data[16];
30         bool operator ==(Digest const& other) const {
31             return 0 == memcmp(data, other.data, sizeof(data));
32         }
33         bool operator !=(Digest const& other) const { return !(*this == other); }
34     };
35 
36     /** Computes and returns the digest. */
37     Digest finish();
38 
39 private:
40     uint64_t byteCount;  // number of bytes, modulo 2^64
41     uint32_t state[4];   // state (ABCD)
42     uint8_t buffer[64];  // input buffer
43 };
44 
45 #endif
46