1#ifndef _DESHA1_HPP
2#define _DESHA1_HPP
3/*-------------------------------------------------------------------------
4 * drawElements C++ Base Library
5 * -----------------------------
6 *
7 * Copyright 2015 The Android Open Source Project
8 *
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
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
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.
20 *
21 *//*!
22 * \file
23 * \brief SHA1 hash functions
24 *//*--------------------------------------------------------------------*/
25
26#include "deDefs.hpp"
27
28#include "deSha1.h"
29
30#include <string>
31#include <vector>
32
33namespace de
34{
35
36class Sha1
37{
38public:
39				Sha1		(const deSha1& hash) : m_hash(hash) {}
40
41	static Sha1	parse		(const std::string& str);
42	static Sha1	compute		(size_t size, const void* data);
43
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); }
46
47private:
48	deSha1		m_hash;
49};
50
51class Sha1Stream
52{
53public:
54					Sha1Stream	(void);
55	void			process		(size_t size, const void* data);
56	Sha1			finalize	(void);
57
58private:
59	deSha1Stream	m_stream;
60};
61
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
65//       specific size.
66//       All vectors and strings will include their size in the hash. Following codes
67//       produce different results:
68//           stream << "Hi" << "Hello";
69//       and
70//           stream << "HiHello";
71
72inline Sha1Stream& operator<< (Sha1Stream& stream, bool b)
73{
74	const deUint8 value = b ? 1 : 0;
75	stream.process(sizeof(value), &value);
76	return stream;
77}
78
79inline Sha1Stream& operator<< (Sha1Stream& stream, deUint32 value)
80{
81	const deUint8 data[] =
82	{
83		(deUint8)(0xFFu & (value >> 24)),
84		(deUint8)(0xFFu & (value >> 16)),
85		(deUint8)(0xFFu & (value >> 8)),
86		(deUint8)(0xFFu & (value >> 0))
87	};
88
89	stream.process(sizeof(data), data);
90	return stream;
91}
92
93inline Sha1Stream& operator<< (Sha1Stream& stream, deInt32 value)
94{
95	return stream << (deUint32)value;
96}
97
98inline Sha1Stream& operator<< (Sha1Stream& stream, deUint64 value)
99{
100	const deUint8 data[] =
101	{
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))
110	};
111
112	stream.process(sizeof(data), data);
113	return stream;
114}
115
116inline Sha1Stream& operator<< (Sha1Stream& stream, deInt64 value)
117{
118	return stream << (deUint64)value;
119}
120
121template<typename T>
122inline Sha1Stream& operator<< (Sha1Stream& stream, const std::vector<T>& values)
123{
124	stream << (deUint64)values.size();
125
126	for (size_t ndx = 0; ndx < values.size(); ndx++)
127		stream << values[ndx];
128
129	return stream;
130}
131
132inline Sha1Stream& operator<< (Sha1Stream& stream, const std::string& str)
133{
134	stream << (deUint64)str.size();
135	stream.process(str.size(), str.c_str());
136	return stream;
137}
138
139} // de
140
141#endif // _DESHA1_HPP
142