1cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com/*
2cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com * Copyright 2013 Google Inc.
3cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com *
4cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com * Use of this source code is governed by a BSD-style license that can be
5cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com * found in the LICENSE file.
6cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com */
7cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
8cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#ifndef SkSHA1_DEFINED
9cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#define SkSHA1_DEFINED
10cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
11cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#include "SkTypes.h"
12cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#include "SkEndian.h"
13cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#include "SkStream.h"
14cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
15cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com//The following macros can be defined to affect the SHA1 code generated.
16cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com//SK_SHA1_CLEAR_DATA causes all intermediate state to be overwritten with 0's.
17cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com//SK_CPU_BENDIAN allows 32 bit <=> 8 bit conversions without copies (if alligned).
18cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com//SK_CPU_FAST_UNALIGNED_ACCESS allows 32 bit <=> 8 bit conversions without copies if SK_CPU_BENDIAN.
19cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
208c6a4f24d331503b3eb9a5c918d5876772b9a5eebungeman@google.comclass SkSHA1 : public SkWStream {
21cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.compublic:
22cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    SkSHA1();
23cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
24cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    /** Processes input, adding it to the digest.
25cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com     *  Note that this treats the buffer as a series of uint8_t values.
26cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com     */
27cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    virtual bool write(const void* buffer, size_t size) SK_OVERRIDE {
28cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com        update(reinterpret_cast<const uint8_t*>(buffer), size);
29cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com        return true;
30cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    }
31cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
32490fb6b4713463954cc0283a9c30e754c45c6004commit-bot@chromium.org    virtual size_t bytesWritten() const SK_OVERRIDE { return SkToSizeT(this->byteCount); }
33490fb6b4713463954cc0283a9c30e754c45c6004commit-bot@chromium.org
34cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    /** Processes input, adding it to the digest. Calling this after finish is undefined. */
35cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    void update(const uint8_t* input, size_t length);
36cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
37cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    struct Digest {
38cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com        uint8_t data[20];
39cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    };
40cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
41cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    /** Computes and returns the digest. */
42cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    void finish(Digest& digest);
43cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
44cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comprivate:
45cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    // number of bytes, modulo 2^64
46cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    uint64_t byteCount;
47cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
48cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    // state (ABCDE)
49cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    uint32_t state[5];
50cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
51cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    // input buffer
52cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com    uint8_t buffer[64];
53cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com};
54cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com
55cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#endif
56