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