From: Mika Isojärvi Date: Tue, 3 Nov 2015 20:15:10 +0000 (-0800) Subject: Add SHA1 functions to decpp. X-Git-Tag: upstream/0.1.0~1351 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0256f8c86ae9a1211a82462ecda36af2b5c9876c;p=platform%2Fupstream%2FVK-GL-CTS.git Add SHA1 functions to decpp. Change-Id: I1bfdf817debf8cfc84bbc6fbdae2b0e58c4fa975 --- diff --git a/Android.mk b/Android.mk index ddac6d3..1619790 100644 --- a/Android.mk +++ b/Android.mk @@ -107,6 +107,7 @@ LOCAL_SRC_FILES := \ framework/delibs/decpp/deThreadLocal.cpp \ framework/delibs/decpp/deThreadSafeRingBuffer.cpp \ framework/delibs/decpp/deUniquePtr.cpp \ + framework/delibs/decpp/deSha1.cpp \ framework/delibs/deimage/deImage.c \ framework/delibs/deimage/deTarga.c \ framework/delibs/depool/deMemPool.c \ diff --git a/framework/delibs/decpp/CMakeLists.txt b/framework/delibs/decpp/CMakeLists.txt index 1d8bbf1..4994b2d 100644 --- a/framework/delibs/decpp/CMakeLists.txt +++ b/framework/delibs/decpp/CMakeLists.txt @@ -57,6 +57,8 @@ set(DECPP_SRCS deUniquePtr.hpp deSpinBarrier.cpp deSpinBarrier.hpp + deSha1.cpp + deSha1.hpp ) set(DECPP_LIBS diff --git a/framework/delibs/decpp/deSha1.cpp b/framework/delibs/decpp/deSha1.cpp new file mode 100644 index 0000000..6aacbb6 --- /dev/null +++ b/framework/delibs/decpp/deSha1.cpp @@ -0,0 +1,65 @@ +/*------------------------------------------------------------------------- + * drawElements C++ Base Library + * ----------------------------- + * + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *//*! + * \file + * \brief SHA1 hash functions + *//*--------------------------------------------------------------------*/ + +#include "deSha1.hpp" + +namespace de +{ + +Sha1 Sha1::parse (const std::string& str) +{ + deSha1 hash; + + DE_CHECK_RUNTIME_ERR_MSG(str.size() >= 40, "Failed to parse SHA1. String is too short."); + DE_CHECK_RUNTIME_ERR_MSG(deSha1_parse(&hash, str.c_str()), "Failed to parse SHA1. Invalid characters.."); + + return Sha1(hash); +} + +Sha1 Sha1::compute (size_t size, const void* data) +{ + deSha1 hash; + + deSha1_compute(&hash, size, data); + return Sha1(hash); +} + +Sha1Stream::Sha1Stream (void) +{ + deSha1Stream_init(&m_stream); +} + +void Sha1Stream::process (size_t size, const void* data) +{ + deSha1Stream_process(&m_stream, size, data); +} + +Sha1 Sha1Stream::finalize (void) +{ + deSha1 hash; + deSha1Stream_finalize(&m_stream, &hash); + + return Sha1(hash); +} + +} // de diff --git a/framework/delibs/decpp/deSha1.hpp b/framework/delibs/decpp/deSha1.hpp new file mode 100644 index 0000000..5b06220 --- /dev/null +++ b/framework/delibs/decpp/deSha1.hpp @@ -0,0 +1,141 @@ +#ifndef _DESHA1_HPP +#define _DESHA1_HPP +/*------------------------------------------------------------------------- + * drawElements C++ Base Library + * ----------------------------- + * + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *//*! + * \file + * \brief SHA1 hash functions + *//*--------------------------------------------------------------------*/ + +#include "deDefs.hpp" + +#include "deSha1.h" + +#include +#include + +namespace de +{ + +class Sha1 +{ +public: + Sha1 (const deSha1& hash) : m_hash(hash) {} + + static Sha1 parse (const std::string& str); + static Sha1 compute (size_t size, const void* data); + + bool operator== (const Sha1& other) const { return deSha1_equal(&m_hash, &other.m_hash); } + bool operator!= (const Sha1& other) const { return !(*this == other); } + +private: + deSha1 m_hash; +}; + +class Sha1Stream +{ +public: + Sha1Stream (void); + void process (size_t size, const void* data); + Sha1 finalize (void); + +private: + deSha1Stream m_stream; +}; + +// Utility functions for building hash from values. +// \note This is not same as serializing the values and computing hash from the data. +// Some extra care is required when dealing with types that have platform +// specific size. +// All vectors and strings will include their size in the hash. Following codes +// produce different results: +// stream << "Hi" << "Hello"; +// and +// stream << "HiHello"; + +inline Sha1Stream& operator<< (Sha1Stream& stream, bool b) +{ + const deUint8 value = b ? 1 : 0; + stream.process(sizeof(value), &value); + return stream; +} + +inline Sha1Stream& operator<< (Sha1Stream& stream, deUint32 value) +{ + const deUint8 data[] = + { + (deUint8)(0xFFu & (value >> 24)), + (deUint8)(0xFFu & (value >> 16)), + (deUint8)(0xFFu & (value >> 8)), + (deUint8)(0xFFu & (value >> 0)) + }; + + stream.process(sizeof(data), data); + return stream; +} + +inline Sha1Stream& operator<< (Sha1Stream& stream, deInt32 value) +{ + return stream << (deUint32)value; +} + +inline Sha1Stream& operator<< (Sha1Stream& stream, deUint64 value) +{ + const deUint8 data[] = + { + (deUint8)(0xFFull & (value >> 56)), + (deUint8)(0xFFull & (value >> 48)), + (deUint8)(0xFFull & (value >> 40)), + (deUint8)(0xFFull & (value >> 32)), + (deUint8)(0xFFull & (value >> 24)), + (deUint8)(0xFFull & (value >> 16)), + (deUint8)(0xFFull & (value >> 8)), + (deUint8)(0xFFull & (value >> 0)) + }; + + stream.process(sizeof(data), data); + return stream; +} + +inline Sha1Stream& operator<< (Sha1Stream& stream, deInt64 value) +{ + return stream << (deUint64)value; +} + +template +inline Sha1Stream& operator<< (Sha1Stream& stream, const std::vector& values) +{ + stream << (deUint64)values.size(); + + for (size_t ndx = 0; ndx < values.size(); ndx++) + stream << values[ndx]; + + return stream; +} + +inline Sha1Stream& operator<< (Sha1Stream& stream, const std::string& str) +{ + stream << (deUint64)str.size(); + stream.process(str.size(), str.c_str()); + return stream; +} + +} // de + +#endif // _DESHA1_HPP