blob: ed557931c242cdb888eefdd57439e0727de26636 [file] [log] [blame]
bungeman@google.comcfcb1be2013-01-31 19:47:48 +00001/*
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 "SkTypes.h"
12#include "SkEndian.h"
13#include "SkStream.h"
14
15//The following macros can be defined to affect the MD5 code generated.
16//SK_MD5_CLEAR_DATA causes all intermediate state to be overwritten with 0's.
17//SK_CPU_LENDIAN allows 32 bit <=> 8 bit conversions without copies (if alligned).
18//SK_CPU_FAST_UNALIGNED_ACCESS allows 32 bit <=> 8 bit conversions without copies if SK_CPU_LENDIAN.
19
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000020class SkMD5 : public SkWStream {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000021public:
22 SkMD5();
23
24 /** Processes input, adding it to the digest.
25 * Note that this treats the buffer as a series of uint8_t values.
26 */
mtklein36352bf2015-03-25 18:17:31 -070027 bool write(const void* buffer, size_t size) override {
commit-bot@chromium.orgdcb8e542014-03-05 18:25:20 +000028 this->update(reinterpret_cast<const uint8_t*>(buffer), size);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000029 return true;
30 }
31
mtklein36352bf2015-03-25 18:17:31 -070032 size_t bytesWritten() const override { return SkToSizeT(this->byteCount); }
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +000033
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000034 /** Processes input, adding it to the digest. Calling this after finish is undefined. */
35 void update(const uint8_t* input, size_t length);
36
37 struct Digest {
38 uint8_t data[16];
halcanarya096d7a2015-03-27 12:16:53 -070039 bool operator ==(Digest const& other) const {
40 return 0 == memcmp(data, other.data, sizeof(data));
41 }
42 bool operator !=(Digest const& other) const {
43 return 0 != memcmp(data, other.data, sizeof(data));
44 }
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000045 };
46
47 /** Computes and returns the digest. */
48 void finish(Digest& digest);
49
50private:
51 // number of bytes, modulo 2^64
52 uint64_t byteCount;
53
54 // state (ABCD)
55 uint32_t state[4];
56
57 // input buffer
58 uint8_t buffer[64];
59};
60
61#endif