michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkSHA1_DEFINED michael@0: #define SkSHA1_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: #include "SkEndian.h" michael@0: #include "SkStream.h" michael@0: michael@0: //The following macros can be defined to affect the SHA1 code generated. michael@0: //SK_SHA1_CLEAR_DATA causes all intermediate state to be overwritten with 0's. michael@0: //SK_CPU_BENDIAN allows 32 bit <=> 8 bit conversions without copies (if alligned). michael@0: //SK_CPU_FAST_UNALIGNED_ACCESS allows 32 bit <=> 8 bit conversions without copies if SK_CPU_BENDIAN. michael@0: michael@0: class SkSHA1 : public SkWStream { michael@0: public: michael@0: SkSHA1(); michael@0: michael@0: /** Processes input, adding it to the digest. michael@0: * Note that this treats the buffer as a series of uint8_t values. michael@0: */ michael@0: virtual bool write(const void* buffer, size_t size) SK_OVERRIDE { michael@0: update(reinterpret_cast(buffer), size); michael@0: return true; michael@0: } michael@0: michael@0: virtual size_t bytesWritten() const SK_OVERRIDE { return SkToSizeT(this->byteCount); } michael@0: michael@0: /** Processes input, adding it to the digest. Calling this after finish is undefined. */ michael@0: void update(const uint8_t* input, size_t length); michael@0: michael@0: struct Digest { michael@0: uint8_t data[20]; michael@0: }; michael@0: michael@0: /** Computes and returns the digest. */ michael@0: void finish(Digest& digest); michael@0: michael@0: private: michael@0: // number of bytes, modulo 2^64 michael@0: uint64_t byteCount; michael@0: michael@0: // state (ABCDE) michael@0: uint32_t state[5]; michael@0: michael@0: // input buffer michael@0: uint8_t buffer[64]; michael@0: }; michael@0: michael@0: #endif