halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 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 | |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 11 | #include "SkStream.h" |
| 12 | |
halcanary | 272aa12 | 2016-04-22 10:40:49 -0700 | [diff] [blame] | 13 | /* Calculate a 128-bit MD5 message-digest of the bytes sent to this stream. */ |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 14 | class SkMD5 : public SkWStream { |
| 15 | public: |
| 16 | SkMD5(); |
| 17 | |
| 18 | /** Processes input, adding it to the digest. |
halcanary | 272aa12 | 2016-04-22 10:40:49 -0700 | [diff] [blame] | 19 | Calling this after finish is undefined. */ |
| 20 | bool write(const void* buffer, size_t size) final; |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 21 | |
halcanary | 272aa12 | 2016-04-22 10:40:49 -0700 | [diff] [blame] | 22 | size_t bytesWritten() const final { return SkToSizeT(this->byteCount); } |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 23 | |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 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 | } |
halcanary | 272aa12 | 2016-04-22 10:40:49 -0700 | [diff] [blame] | 29 | bool operator !=(Digest const& other) const { return !(*this == other); } |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | /** Computes and returns the digest. */ |
| 33 | void finish(Digest& digest); |
| 34 | |
| 35 | private: |
halcanary | 272aa12 | 2016-04-22 10:40:49 -0700 | [diff] [blame] | 36 | uint64_t byteCount; // number of bytes, modulo 2^64 |
| 37 | uint32_t state[4]; // state (ABCD) |
| 38 | uint8_t buffer[64]; // input buffer |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | #endif |