1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 #include "content/public/browser/media_device_id.h" 5 6 #include "base/logging.h" 7 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_util.h" 9 #include "crypto/hmac.h" 10 11 namespace content { 12 13 std::string GetHMACForMediaDeviceID(const ResourceContext::SaltCallback& sc, 14 const GURL& security_origin, 15 const std::string& raw_unique_id) { 16 DCHECK(security_origin.is_valid()); 17 DCHECK(!raw_unique_id.empty()); 18 crypto::HMAC hmac(crypto::HMAC::SHA256); 19 const size_t digest_length = hmac.DigestLength(); 20 std::vector<uint8> digest(digest_length); 21 std::string salt = sc.Run(); 22 bool result = hmac.Init(security_origin.spec()) && 23 hmac.Sign(raw_unique_id + salt, &digest[0], digest.size()); 24 DCHECK(result); 25 return StringToLowerASCII(base::HexEncode(&digest[0], digest.size())); 26 } 27 28 bool DoesMediaDeviceIDMatchHMAC(const ResourceContext::SaltCallback& sc, 29 const GURL& security_origin, 30 const std::string& device_guid, 31 const std::string& raw_unique_id) { 32 DCHECK(security_origin.is_valid()); 33 DCHECK(!raw_unique_id.empty()); 34 std::string guid_from_raw_device_id = 35 GetHMACForMediaDeviceID(sc, security_origin, raw_unique_id); 36 return guid_from_raw_device_id == device_guid; 37 } 38 39 } // namespace content 40