2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2019-01-07 08:47:17 +00:00
|
|
|
Copyright (c) 2011-2019 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2012-08-08 13:27:06 +00:00
|
|
|
#ifndef SPROUT_UUID_MD5_NAME_GENERATOR_HPP
|
|
|
|
#define SPROUT_UUID_MD5_NAME_GENERATOR_HPP
|
|
|
|
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/string.hpp>
|
|
|
|
#include <sprout/uuid/uuid.hpp>
|
2013-04-14 10:42:34 +00:00
|
|
|
#include <sprout/uuid/namespaces.hpp>
|
2012-08-08 13:27:06 +00:00
|
|
|
#include <sprout/checksum/md5.hpp>
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace uuids {
|
|
|
|
//
|
|
|
|
// md5_name_generator
|
|
|
|
//
|
|
|
|
class md5_name_generator {
|
|
|
|
public:
|
|
|
|
typedef sprout::uuids::uuid result_type;
|
|
|
|
private:
|
2016-01-24 09:54:16 +00:00
|
|
|
typedef result_type::value_type value_type;
|
2012-08-08 13:27:06 +00:00
|
|
|
private:
|
|
|
|
sprout::md5 sum_;
|
|
|
|
private:
|
|
|
|
SPROUT_CONSTEXPR result_type sha_to_uuid_1(sprout::md5::value_type const& value) const {
|
|
|
|
return result_type{{
|
|
|
|
value[0],
|
|
|
|
value[1],
|
|
|
|
value[2],
|
|
|
|
value[3],
|
|
|
|
value[4],
|
|
|
|
value[5],
|
2013-04-15 10:13:14 +00:00
|
|
|
static_cast<value_type>((value[6] & 0x3F) | 0x30),
|
2012-08-08 13:27:06 +00:00
|
|
|
value[7],
|
|
|
|
static_cast<value_type>((value[8] & 0xBF) | 0x80),
|
|
|
|
value[9],
|
|
|
|
value[10],
|
|
|
|
value[11],
|
|
|
|
value[12],
|
|
|
|
value[13],
|
|
|
|
value[14],
|
|
|
|
value[15]
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR result_type sha_to_uuid(sprout::md5 const& sha) const {
|
|
|
|
return sha_to_uuid_1(sha.checksum());
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
SPROUT_CONSTEXPR md5_name_generator()
|
2016-01-24 09:54:16 +00:00
|
|
|
: sum_(sprout::md5().c_process_range(sprout::uuids::uuid{{0}}))
|
2012-08-08 13:27:06 +00:00
|
|
|
{}
|
|
|
|
explicit SPROUT_CONSTEXPR md5_name_generator(sprout::uuids::uuid const& namespace_uuid)
|
2016-01-24 09:54:16 +00:00
|
|
|
: sum_(sprout::md5().c_process_range(namespace_uuid))
|
2012-08-08 13:27:06 +00:00
|
|
|
{}
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
|
|
|
SPROUT_CONSTEXPR result_type operator()(sprout::basic_string<Elem, N, Traits> const& name) const {
|
2016-01-24 09:54:16 +00:00
|
|
|
return sha_to_uuid(sum_.c_process_range(name));
|
2012-08-08 13:27:06 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR result_type operator()(char const* name) const {
|
2016-01-24 09:54:16 +00:00
|
|
|
return sha_to_uuid(sum_.c_process_bytes(name, sprout::char_traits<char>::length(name)));
|
2012-08-08 13:27:06 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR result_type operator()(wchar_t const* name) const {
|
2016-01-24 09:54:16 +00:00
|
|
|
return sha_to_uuid(sum_.c_process_bytes(name, sprout::char_traits<wchar_t>::length(name)));
|
2012-08-08 13:27:06 +00:00
|
|
|
}
|
2016-03-10 10:17:22 +00:00
|
|
|
#if SPROUT_USE_UNICODE_LITERALS
|
2012-08-08 13:27:06 +00:00
|
|
|
SPROUT_CONSTEXPR result_type operator()(char16_t const* name) const {
|
2016-01-24 09:54:16 +00:00
|
|
|
return sha_to_uuid(sum_.c_process_bytes(name, sprout::char_traits<char16_t>::length(name)));
|
2012-08-08 13:27:06 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR result_type operator()(char32_t const* name) const {
|
2016-01-24 09:54:16 +00:00
|
|
|
return sha_to_uuid(sum_.c_process_bytes(name, sprout::char_traits<char32_t>::length(name)));
|
2012-08-08 13:27:06 +00:00
|
|
|
}
|
2016-03-10 10:17:22 +00:00
|
|
|
#endif
|
2012-08-08 13:27:06 +00:00
|
|
|
};
|
2013-04-14 10:42:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// make_uuid3
|
|
|
|
//
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid3(sprout::basic_string<Elem, N, Traits> const& name) {
|
|
|
|
return sprout::uuids::md5_name_generator()(name);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid3(char const* name) {
|
|
|
|
return sprout::uuids::md5_name_generator()(name);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid3(wchar_t const* name) {
|
|
|
|
return sprout::uuids::md5_name_generator()(name);
|
|
|
|
}
|
2016-03-10 10:17:22 +00:00
|
|
|
#if SPROUT_USE_UNICODE_LITERALS
|
2013-04-14 10:42:34 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid3(char16_t const* name) {
|
|
|
|
return sprout::uuids::md5_name_generator()(name);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid3(char32_t const* name) {
|
|
|
|
return sprout::uuids::md5_name_generator()(name);
|
|
|
|
}
|
2016-03-10 10:17:22 +00:00
|
|
|
#endif
|
2013-04-14 10:42:34 +00:00
|
|
|
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid3(sprout::uuids::uuid const& namespace_uuid, sprout::basic_string<Elem, N, Traits> const& name) {
|
|
|
|
return sprout::uuids::md5_name_generator(namespace_uuid)(name);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid3(sprout::uuids::uuid const& namespace_uuid, char const* name) {
|
|
|
|
return sprout::uuids::md5_name_generator(namespace_uuid)(name);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid3(sprout::uuids::uuid const& namespace_uuid, wchar_t const* name) {
|
|
|
|
return sprout::uuids::md5_name_generator(namespace_uuid)(name);
|
|
|
|
}
|
2016-03-10 10:17:22 +00:00
|
|
|
#if SPROUT_USE_UNICODE_LITERALS
|
2013-04-14 10:42:34 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid3(sprout::uuids::uuid const& namespace_uuid, char16_t const* name) {
|
|
|
|
return sprout::uuids::md5_name_generator(namespace_uuid)(name);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid3(sprout::uuids::uuid const& namespace_uuid, char32_t const* name) {
|
|
|
|
return sprout::uuids::md5_name_generator(namespace_uuid)(name);
|
|
|
|
}
|
2016-03-10 10:17:22 +00:00
|
|
|
#endif
|
2013-04-14 10:42:34 +00:00
|
|
|
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::md5_name_generator
|
|
|
|
make_uuid3() {
|
|
|
|
return sprout::uuids::md5_name_generator();
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::md5_name_generator
|
|
|
|
make_uuid3(sprout::uuids::uuid const& namespace_uuid) {
|
|
|
|
return sprout::uuids::md5_name_generator(namespace_uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// make_uuid3_dns
|
|
|
|
// make_uuid3_url
|
|
|
|
// make_uuid3_oid
|
|
|
|
// make_uuid3_x500
|
|
|
|
//
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::md5_name_generator
|
|
|
|
make_uuid3_dns() {
|
|
|
|
return sprout::uuids::md5_name_generator(sprout::uuids::namespace_dns_uuid());
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::md5_name_generator
|
|
|
|
make_uuid3_url() {
|
|
|
|
return sprout::uuids::md5_name_generator(sprout::uuids::namespace_url_uuid());
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::md5_name_generator
|
|
|
|
make_uuid3_oid() {
|
|
|
|
return sprout::uuids::md5_name_generator(sprout::uuids::namespace_oid_uuid());
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::md5_name_generator
|
|
|
|
make_uuid3_x500() {
|
|
|
|
return sprout::uuids::md5_name_generator(sprout::uuids::namespace_x500_uuid());
|
|
|
|
}
|
2012-08-08 13:27:06 +00:00
|
|
|
} // namespace uuids
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_UUID_MD5_NAME_GENERATOR_HPP
|