ResourceCryptographicDigest.h [plain text]
#pragma once
#include <type_traits>
#include <wtf/HashFunctions.h>
#include <wtf/HashTraits.h>
#include <wtf/Hasher.h>
#include <wtf/Optional.h>
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
class CachedResource;
struct ResourceCryptographicDigest {
enum class Algorithm {
SHA256 = 1 << 0,
SHA384 = 1 << 1,
SHA512 = 1 << 2,
};
static constexpr size_t maximumDigestLength = 64;
Algorithm algorithm;
Vector<uint8_t> value;
bool operator==(const ResourceCryptographicDigest& other) const
{
return algorithm == other.algorithm && value == other.value;
}
bool operator!=(const ResourceCryptographicDigest& other) const
{
return !(*this == other);
}
};
struct EncodedResourceCryptographicDigest {
using Algorithm = ResourceCryptographicDigest::Algorithm;
Algorithm algorithm;
String digest;
};
Optional<ResourceCryptographicDigest> parseCryptographicDigest(const UChar*& begin, const UChar* end);
Optional<ResourceCryptographicDigest> parseCryptographicDigest(const LChar*& begin, const LChar* end);
Optional<EncodedResourceCryptographicDigest> parseEncodedCryptographicDigest(const UChar*& begin, const UChar* end);
Optional<EncodedResourceCryptographicDigest> parseEncodedCryptographicDigest(const LChar*& begin, const LChar* end);
Optional<ResourceCryptographicDigest> decodeEncodedResourceCryptographicDigest(const EncodedResourceCryptographicDigest&);
ResourceCryptographicDigest cryptographicDigestForBytes(ResourceCryptographicDigest::Algorithm, const void* bytes, size_t length);
}
namespace WTF {
template<> struct DefaultHash<WebCore::ResourceCryptographicDigest> {
struct Hash {
static unsigned hash(const WebCore::ResourceCryptographicDigest& digest)
{
return pairIntHash(intHash(static_cast<unsigned>(digest.algorithm)), StringHasher::computeHash(digest.value.data(), digest.value.size()));
}
static bool equal(const WebCore::ResourceCryptographicDigest& a, const WebCore::ResourceCryptographicDigest& b)
{
return a == b;
}
static const bool safeToCompareToEmptyOrDeleted = true;
};
};
template<> struct HashTraits<WebCore::ResourceCryptographicDigest> : GenericHashTraits<WebCore::ResourceCryptographicDigest> {
using Algorithm = WebCore::ResourceCryptographicDigest::Algorithm;
using AlgorithmUnderlyingType = typename std::underlying_type<Algorithm>::type;
static constexpr auto emptyAlgorithmValue = static_cast<Algorithm>(std::numeric_limits<AlgorithmUnderlyingType>::max());
static constexpr auto deletedAlgorithmValue = static_cast<Algorithm>(std::numeric_limits<AlgorithmUnderlyingType>::max() - 1);
static const bool emptyValueIsZero = false;
static WebCore::ResourceCryptographicDigest emptyValue()
{
return { emptyAlgorithmValue, { } };
}
static void constructDeletedValue(WebCore::ResourceCryptographicDigest& slot)
{
slot.algorithm = deletedAlgorithmValue;
}
static bool isDeletedValue(const WebCore::ResourceCryptographicDigest& slot)
{
return slot.algorithm == deletedAlgorithmValue;
}
};
}