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