3 /*-------------------------------------------------------------------------
4 * drawElements C++ Base Library
5 * -----------------------------
7 * Copyright 2015 The Android Open Source Project
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * \brief SHA1 hash functions
24 *//*--------------------------------------------------------------------*/
39 Sha1 (const deSha1& hash) : m_hash(hash) {}
41 static Sha1 parse (const std::string& str);
42 static Sha1 compute (size_t size, const void* data);
44 bool operator== (const Sha1& other) const { return deSha1_equal(&m_hash, &other.m_hash) == DE_TRUE; }
45 bool operator!= (const Sha1& other) const { return !(*this == other); }
55 void process (size_t size, const void* data);
59 deSha1Stream m_stream;
62 // Utility functions for building hash from values.
63 // \note This is not same as serializing the values and computing hash from the data.
64 // Some extra care is required when dealing with types that have platform
66 // All vectors and strings will include their size in the hash. Following codes
67 // produce different results:
68 // stream << "Hi" << "Hello";
70 // stream << "HiHello";
72 inline Sha1Stream& operator<< (Sha1Stream& stream, bool b)
74 const deUint8 value = b ? 1 : 0;
75 stream.process(sizeof(value), &value);
79 inline Sha1Stream& operator<< (Sha1Stream& stream, deUint32 value)
81 const deUint8 data[] =
83 (deUint8)(0xFFu & (value >> 24)),
84 (deUint8)(0xFFu & (value >> 16)),
85 (deUint8)(0xFFu & (value >> 8)),
86 (deUint8)(0xFFu & (value >> 0))
89 stream.process(sizeof(data), data);
93 inline Sha1Stream& operator<< (Sha1Stream& stream, deInt32 value)
95 return stream << (deUint32)value;
98 inline Sha1Stream& operator<< (Sha1Stream& stream, deUint64 value)
100 const deUint8 data[] =
102 (deUint8)(0xFFull & (value >> 56)),
103 (deUint8)(0xFFull & (value >> 48)),
104 (deUint8)(0xFFull & (value >> 40)),
105 (deUint8)(0xFFull & (value >> 32)),
106 (deUint8)(0xFFull & (value >> 24)),
107 (deUint8)(0xFFull & (value >> 16)),
108 (deUint8)(0xFFull & (value >> 8)),
109 (deUint8)(0xFFull & (value >> 0))
112 stream.process(sizeof(data), data);
116 inline Sha1Stream& operator<< (Sha1Stream& stream, deInt64 value)
118 return stream << (deUint64)value;
122 inline Sha1Stream& operator<< (Sha1Stream& stream, const std::vector<T>& values)
124 stream << (deUint64)values.size();
126 for (size_t ndx = 0; ndx < values.size(); ndx++)
127 stream << values[ndx];
132 inline Sha1Stream& operator<< (Sha1Stream& stream, const std::string& str)
134 stream << (deUint64)str.size();
135 stream.process(str.size(), str.c_str());
141 #endif // _DESHA1_HPP