Home | History | Annotate | Download | only in uploader
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "uploader/metrics_hashes.h"
     18 
     19 #include "base/logging.h"
     20 #include "base/md5.h"
     21 #include "base/sys_byteorder.h"
     22 
     23 namespace metrics {
     24 
     25 namespace {
     26 
     27 // Converts the 8-byte prefix of an MD5 hash into a uint64 value.
     28 inline uint64_t HashToUInt64(const std::string& hash) {
     29   uint64_t value;
     30   DCHECK_GE(hash.size(), sizeof(value));
     31   memcpy(&value, hash.data(), sizeof(value));
     32   return base::HostToNet64(value);
     33 }
     34 
     35 }  // namespace
     36 
     37 uint64_t HashMetricName(const std::string& name) {
     38   // Create an MD5 hash of the given |name|, represented as a byte buffer
     39   // encoded as an std::string.
     40   base::MD5Context context;
     41   base::MD5Init(&context);
     42   base::MD5Update(&context, name);
     43 
     44   base::MD5Digest digest;
     45   base::MD5Final(&digest, &context);
     46 
     47   std::string hash_str(reinterpret_cast<char*>(digest.a), arraysize(digest.a));
     48   return HashToUInt64(hash_str);
     49 }
     50 
     51 }  // namespace metrics
     52