blob: 8838d47ac07d04ce224ada019114d496ea29c6e6 [file] [log] [blame]
halcanary488165e2016-04-22 06:10:21 -07001/*
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
halcanary488165e2016-04-22 06:10:21 -070011#include "SkStream.h"
12
halcanary272aa122016-04-22 10:40:49 -070013/* Calculate a 128-bit MD5 message-digest of the bytes sent to this stream. */
halcanary488165e2016-04-22 06:10:21 -070014class SkMD5 : public SkWStream {
15public:
16 SkMD5();
17
18 /** Processes input, adding it to the digest.
halcanary272aa122016-04-22 10:40:49 -070019 Calling this after finish is undefined. */
20 bool write(const void* buffer, size_t size) final;
halcanary488165e2016-04-22 06:10:21 -070021
halcanary272aa122016-04-22 10:40:49 -070022 size_t bytesWritten() const final { return SkToSizeT(this->byteCount); }
halcanary488165e2016-04-22 06:10:21 -070023
halcanary488165e2016-04-22 06:10:21 -070024 struct Digest {
25 uint8_t data[16];
26 bool operator ==(Digest const& other) const {
27 return 0 == memcmp(data, other.data, sizeof(data));
28 }
halcanary272aa122016-04-22 10:40:49 -070029 bool operator !=(Digest const& other) const { return !(*this == other); }
halcanary488165e2016-04-22 06:10:21 -070030 };
31
32 /** Computes and returns the digest. */
33 void finish(Digest& digest);
34
35private:
halcanary272aa122016-04-22 10:40:49 -070036 uint64_t byteCount; // number of bytes, modulo 2^64
37 uint32_t state[4]; // state (ABCD)
38 uint8_t buffer[64]; // input buffer
halcanary488165e2016-04-22 06:10:21 -070039};
40
41#endif