bungeman@google.com | cfcb1be | 2013-01-31 19:47:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 SkSHA1_DEFINED |
| 9 | #define SkSHA1_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 SHA1 code generated. |
| 16 | //SK_SHA1_CLEAR_DATA causes all intermediate state to be overwritten with 0's. |
| 17 | //SK_CPU_BENDIAN 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_BENDIAN. |
| 19 | |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 20 | class SkSHA1 : public SkWStream { |
bungeman@google.com | cfcb1be | 2013-01-31 19:47:48 +0000 | [diff] [blame] | 21 | public: |
| 22 | SkSHA1(); |
| 23 | |
| 24 | /** Processes input, adding it to the digest. |
| 25 | * Note that this treats the buffer as a series of uint8_t values. |
| 26 | */ |
| 27 | virtual bool write(const void* buffer, size_t size) SK_OVERRIDE { |
| 28 | update(reinterpret_cast<const uint8_t*>(buffer), size); |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | /** Processes input, adding it to the digest. Calling this after finish is undefined. */ |
| 33 | void update(const uint8_t* input, size_t length); |
| 34 | |
| 35 | struct Digest { |
| 36 | uint8_t data[20]; |
| 37 | }; |
| 38 | |
| 39 | /** Computes and returns the digest. */ |
| 40 | void finish(Digest& digest); |
| 41 | |
| 42 | private: |
| 43 | // number of bytes, modulo 2^64 |
| 44 | uint64_t byteCount; |
| 45 | |
| 46 | // state (ABCDE) |
| 47 | uint32_t state[5]; |
| 48 | |
| 49 | // input buffer |
| 50 | uint8_t buffer[64]; |
| 51 | }; |
| 52 | |
| 53 | #endif |