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
33 namespace de
34 {
35
36 class Sha1
37 {
38 public:
Sha1(const deSha1 & hash)39 Sha1(const deSha1 &hash) : m_hash(hash)
40 {
41 }
42
43 static Sha1 parse(const std::string &str);
44 static Sha1 compute(size_t size, const void *data);
45
operator ==(const Sha1 & other) const46 bool operator==(const Sha1 &other) const
47 {
48 return deSha1_equal(&m_hash, &other.m_hash) == true;
49 }
operator !=(const Sha1 & other) const50 bool operator!=(const Sha1 &other) const
51 {
52 return !(*this == other);
53 }
54
55 private:
56 deSha1 m_hash;
57 };
58
59 class Sha1Stream
60 {
61 public:
62 Sha1Stream(void);
63 void process(size_t size, const void *data);
64 Sha1 finalize(void);
65
66 private:
67 deSha1Stream m_stream;
68 };
69
70 // Utility functions for building hash from values.
71 // \note This is not same as serializing the values and computing hash from the data.
72 // Some extra care is required when dealing with types that have platform
73 // specific size.
74 // All vectors and strings will include their size in the hash. Following codes
75 // produce different results:
76 // stream << "Hi" << "Hello";
77 // and
78 // stream << "HiHello";
79
operator <<(Sha1Stream & stream,bool b)80 inline Sha1Stream &operator<<(Sha1Stream &stream, bool b)
81 {
82 const uint8_t value = b ? 1 : 0;
83 stream.process(sizeof(value), &value);
84 return stream;
85 }
86
operator <<(Sha1Stream & stream,uint32_t value)87 inline Sha1Stream &operator<<(Sha1Stream &stream, uint32_t value)
88 {
89 const uint8_t data[] = {(uint8_t)(0xFFu & (value >> 24)), (uint8_t)(0xFFu & (value >> 16)),
90 (uint8_t)(0xFFu & (value >> 8)), (uint8_t)(0xFFu & (value >> 0))};
91
92 stream.process(sizeof(data), data);
93 return stream;
94 }
95
operator <<(Sha1Stream & stream,int32_t value)96 inline Sha1Stream &operator<<(Sha1Stream &stream, int32_t value)
97 {
98 return stream << (uint32_t)value;
99 }
100
operator <<(Sha1Stream & stream,uint64_t value)101 inline Sha1Stream &operator<<(Sha1Stream &stream, uint64_t value)
102 {
103 const uint8_t data[] = {(uint8_t)(0xFFull & (value >> 56)), (uint8_t)(0xFFull & (value >> 48)),
104 (uint8_t)(0xFFull & (value >> 40)), (uint8_t)(0xFFull & (value >> 32)),
105 (uint8_t)(0xFFull & (value >> 24)), (uint8_t)(0xFFull & (value >> 16)),
106 (uint8_t)(0xFFull & (value >> 8)), (uint8_t)(0xFFull & (value >> 0))};
107
108 stream.process(sizeof(data), data);
109 return stream;
110 }
111
operator <<(Sha1Stream & stream,int64_t value)112 inline Sha1Stream &operator<<(Sha1Stream &stream, int64_t value)
113 {
114 return stream << (uint64_t)value;
115 }
116
117 template <typename T>
operator <<(Sha1Stream & stream,const std::vector<T> & values)118 inline Sha1Stream &operator<<(Sha1Stream &stream, const std::vector<T> &values)
119 {
120 stream << (uint64_t)values.size();
121
122 for (size_t ndx = 0; ndx < values.size(); ndx++)
123 stream << values[ndx];
124
125 return stream;
126 }
127
operator <<(Sha1Stream & stream,const std::string & str)128 inline Sha1Stream &operator<<(Sha1Stream &stream, const std::string &str)
129 {
130 stream << (uint64_t)str.size();
131 stream.process(str.size(), str.c_str());
132 return stream;
133 }
134
135 } // namespace de
136
137 #endif // _DESHA1_HPP
138