#include "config.h"
#include "CryptoKey.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmDescriptionBuilder.h"
#include "CryptoAlgorithmRegistry.h"
#include <wtf/CryptographicallyRandomNumber.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
CryptoKey::CryptoKey(CryptoAlgorithmIdentifier algorithm, Type type, bool extractable, CryptoKeyUsage usages)
: m_algorithm(algorithm)
, m_type(type)
, m_extractable(extractable)
, m_usages(usages)
{
}
CryptoKey::~CryptoKey()
{
}
void CryptoKey::buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder& builder) const
{
builder.add("name", CryptoAlgorithmRegistry::singleton().nameForIdentifier(m_algorithm));
}
auto CryptoKey::usages() const -> Vector<Usage>
{
Vector<Usage> result;
if (m_usages & CryptoKeyUsageDecrypt)
result.append(Usage::Decrypt);
if (m_usages & CryptoKeyUsageDeriveBits)
result.append(Usage::DeriveBits);
if (m_usages & CryptoKeyUsageDeriveKey)
result.append(Usage::DeriveKey);
if (m_usages & CryptoKeyUsageEncrypt)
result.append(Usage::Encrypt);
if (m_usages & CryptoKeyUsageSign)
result.append(Usage::Sign);
if (m_usages & CryptoKeyUsageUnwrapKey)
result.append(Usage::UnwrapKey);
if (m_usages & CryptoKeyUsageVerify)
result.append(Usage::Verify);
if (m_usages & CryptoKeyUsageWrapKey)
result.append(Usage::WrapKey);
return result;
}
#if !OS(DARWIN) || PLATFORM(EFL) || PLATFORM(GTK)
Vector<uint8_t> CryptoKey::randomData(size_t size)
{
Vector<uint8_t> result(size);
cryptographicallyRandomValues(result.data(), result.size());
return result;
}
#endif
}
#endif // ENABLE(SUBTLE_CRYPTO)