| Index: content/browser/renderer_host/media/media_source_id_factory.cc
|
| diff --git a/content/browser/renderer_host/media/media_source_id_factory.cc b/content/browser/renderer_host/media/media_source_id_factory.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c2d452ecc02b67a794dd43993a8e06c1a34f9d82
|
| --- /dev/null
|
| +++ b/content/browser/renderer_host/media/media_source_id_factory.cc
|
| @@ -0,0 +1,62 @@
|
| +// Copyright 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "content/browser/renderer_host/media/media_source_id_factory.h"
|
| +
|
| +#include "base/strings/string_number_conversions.h"
|
| +#include "base/strings/string_util.h"
|
| +#include "crypto/hmac.h"
|
| +
|
| +namespace content {
|
| +
|
| +bool MediaSourceIdFactory::DoesSourceIdMatchDeviceId(
|
| + const GURL& security_origin,
|
| + const std::string& source_id,
|
| + const std::string& device_id) {
|
| + crypto::HMAC hmac(crypto::HMAC::SHA256);
|
| + bool result = hmac.Init(security_origin.spec());
|
| + DCHECK(result);
|
| + std::vector<uint8> converted_guid;
|
| + if (!base::HexStringToBytes(source_id, &converted_guid))
|
| + return false;
|
| + return hmac.Verify(
|
| + device_id,
|
| + base::StringPiece(reinterpret_cast<const char*>(&converted_guid[0]),
|
| + converted_guid.size()));
|
| +}
|
| +
|
| +void MediaSourceIdFactory::GenerateSourceId(const GURL& origin,
|
| + const std::string& device_id,
|
| + std::string* source_id) {
|
| + // TODO(perkj): Add a random salt to this HMAC that is dependent on current
|
| + // user profile. This salt should be regenerated if the cache is cleared.
|
| + crypto::HMAC hmac(crypto::HMAC::SHA256);
|
| + const size_t digest_length = hmac.DigestLength();
|
| + std::vector<uint8> digest(digest_length);
|
| + bool result = hmac.Init(origin.spec()) &&
|
| + hmac.Sign(device_id, &digest[0], digest.size());
|
| + CHECK(result);
|
| +
|
| + *source_id = StringToLowerASCII(base::HexEncode(&digest[0], digest.size()));
|
| +}
|
| +
|
| +void MediaSourceIdFactory::GenerateSourceIds(
|
| + const GURL& origin,
|
| + const StreamDeviceInfoArray& raw_devices,
|
| + StreamDeviceInfoArray* devices_with_sourceid) {
|
| + DCHECK(devices_with_sourceid);
|
| +
|
| + // Replace raw ids with hmac'd ids.
|
| + for (StreamDeviceInfoArray::const_iterator device_itr = raw_devices.begin();
|
| + device_itr != raw_devices.end();
|
| + ++device_itr) {
|
| + StreamDeviceInfo current_device_info = *device_itr;
|
| + GenerateSourceId(origin,
|
| + device_itr->device.id,
|
| + ¤t_device_info.device.id);
|
| + devices_with_sourceid->push_back(current_device_info);
|
| + }
|
| +}
|
| +
|
| +} // namespace content
|
|
|