OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #ifndef WTF_SHA1_h | |
32 #define WTF_SHA1_h | |
33 | |
34 #include <wtf/Vector.h> | |
35 #include <wtf/text/CString.h> | |
36 | |
37 namespace WTF { | |
38 | |
39 class SHA1 { | |
40 public: | |
41 WTF_EXPORT_PRIVATE SHA1(); | |
42 | |
43 void addBytes(const Vector<uint8_t>& input) | |
44 { | |
45 addBytes(input.data(), input.size()); | |
46 } | |
47 void addBytes(const CString& input) | |
48 { | |
49 const char* string = input.data(); | |
50 // Make sure that the creator of the CString didn't make the mistake | |
51 // of forcing length() to be the size of the buffer used to create the | |
52 // string, prior to inserting the null terminator earlier in the | |
53 // sequence. | |
54 ASSERT(input.length() == strlen(string)); | |
55 addBytes(reinterpret_cast<const uint8_t*>(string), input.length()); | |
56 } | |
57 WTF_EXPORT_PRIVATE void addBytes(const uint8_t* input, size_t length); | |
58 | |
59 // computeHash has a side effect of resetting the state of the object. | |
60 WTF_EXPORT_PRIVATE void computeHash(Vector<uint8_t, 20>&); | |
61 | |
62 // Get a hex hash from the digest. Pass a limit less than 40 if you want a s
horter digest. | |
63 WTF_EXPORT_PRIVATE static CString hexDigest(const Vector<uint8_t, 20>&); | |
64 | |
65 // Compute the hex digest directly. Pass a limit less than 40 if you want a
shorter digest. | |
66 WTF_EXPORT_PRIVATE CString computeHexDigest(); | |
67 | |
68 private: | |
69 void finalize(); | |
70 void processBlock(); | |
71 void reset(); | |
72 | |
73 uint8_t m_buffer[64]; | |
74 size_t m_cursor; // Number of bytes filled in m_buffer (0-64). | |
75 uint64_t m_totalBytes; // Number of bytes added so far. | |
76 uint32_t m_hash[5]; | |
77 }; | |
78 | |
79 } // namespace WTF | |
80 | |
81 using WTF::SHA1; | |
82 | |
83 #endif // WTF_SHA1_h | |
OLD | NEW |